Python device communications protocol framework
A framework for rapid implementation of communications protocols in Python focusing on speed, modular flexibility and extensibility.
Motivation
We have a lot of different small electronic devices in the lab (relays, valves, ADCs, data acquisition systems, valves, etc.), each using some simple byte protocol over TCP/IP or serial line.
Most of these protocols are roughly the same: a packet starts by some INIT character, then has a few bytes for protocol info, internal device addressing, then some bytes for actual data and it ends with a control sum and/or a terminating character (very often CR).
The idea was to make the declaration of the protocol as simple as possible and make a catch-all implementation of a Device
class that could be easily extended without the need to deal with byte sending and receiving.
How-to for implementing support for your device
- You start off by implementing the protocol module by defining
RequestPacket
andResponsePacket
classes which inherit from the same classes inpydcpf.protocols.base
- Then you define the protocol structure for each packet using the
register_element
class method. - Then you define the
ResponsePacket.find
instance method that checks if a packet is complete or more data must be received. - If you need to use some special interface for sending and receiving data you have to define a module with an
Interface
class that inherits frompydcpf.interfaces.base.Interface
(study its docstrings to find out what you have to define), but in most cases thepydcpf.interfaces.socket_interface
for TCP/IP andpydcpf.interfaces.serial_interface
modules should provide what you need. - Then you create an instance (or you subclass it first) of
pydcpf.Device
and you supply the modules to its constructor. After that you can use the methods of theDevice
instance to send and receive packets.
Packet interaction
The Device
class instances have methods that either work with packet elements parameters and return just the data contained in them, or the lower-level methods they wrap that operate on *Packet
classes instances.
A *Packet
class instance has attributes (which are properties) of the names specified with register_element
which decode the element only when you access them.
Examples
The pydcpf.appliances
subpackage contains several implementations of Device
classes used at the GOLEM tokamak for communicating with relays, valves, power supplies, ADCs, Data acquisition systems, etc. You will find the corresponding protocol implementations in the pydcpf.protocols
subpackage.
Future plans
test the new serial interface on the AC250Kxxx power supply
create some real documentation
- fix up and extend docstrings
- let Sphinx do the job