[Twisted-Python] Guidance needed on serial device interaction

Michael Thompson michaelnt at gmail.com
Wed Feb 2 00:58:18 MST 2011


On 2 February 2011 06:36, Jason Heeris <jason.heeris at gmail.com> wrote:
> This is a bit long, sorry...
>
> I have a PyGTK program that uses threads and pyserial's blocking
> methods to interact with an RS232 connected device. I'd like to throw
> out the threading awfulness and redo it in Twisted, if possible, but
> I'm a little lost.
>
> The real protocol is a bit convoluted, but basically:
>  - You can issue single character commands to the device that give a fixed
>    length response, such as sending 'C' and getting an eight-digit hex
>    string back (the program flash CRC)
>  - You can put the device into "programming mode" (command 'P'), where it
>    takes an arbitrary length sequence of records, verifying each record and
>    stopping when it sees the special "end record"
>  - The device will send back '!' to indicate an error
>  - The device will send back '>' to indicate that it's ready for more
>    commands

The job of the protocol class is to assemble the bytes that you
receive into packets or messages. Ideally the protocol will have
characters that frame a message, and it sounds like you might have
this if > or ! is always sent at the end of every reply.

The protocol dataReceived method will be called as  data arrives at
the serial port once enough data has arrived to complete a message
this method calls stringReceived with the complete message.

There are some protocol that might be suitable in twisted/protocols/basic.py

> <snip>
> I'd also like to structure things so that successive calls to the
> DeviceProtocol object queue up, something like:

You can use a DeferredSemaphore for this.

 <snip>

Michael




More information about the Twisted-Python mailing list