Async serial communication/threads sharing data

Jean-Paul Calderone exarkun at divmod.com
Sun Mar 22 14:49:16 CET 2009


On Sun, 22 Mar 2009 03:13:36 -0700 (PDT), Nick Timkovich <prometheus235 at gmail.com> wrote:
>On Mar 21, 9:19 pm, Jean-Paul Calderone <exar... at divmod.com> wrote:
>> On Sat, 21 Mar 2009 13:52:21 -0700 (PDT), Nick Timkovich <prometheus... at gmail.com> wrote:
>> >I've been working on a program that will talk to an embedded device
>> >over the serial port, using some basic binary communications with
>> >messages 4-10 bytes long or so.  Most of the nuts and bolts problems
>> >I've been able to solve, and have learned a little about the threading
>> >library to avoid blocking all action while waiting for responses
>> >(which can take 50 ms to 10 s).  Ultimately, this program will test
>> >the device on the COM port by sending it messages and collecting
>> >responses for 10k-100k cycles; a cycle being:
>> > 1. tell it to switch a relay,
>> > 2. get it's response from the event,
>> > 3. ask it for some measurements,
>> > 4. get measurements,
>> > 5. repeat.
>> >Later I would like to develop a GUI as well, but not a big issue now
>> >(another reason to use threads? not sure).
>>
>> Twisted includes serial port support and will let you integrate with a
>> GUI toolkit.  Since Twisted encourages you to write programs which deal
>> with things asynchronously in a single thread, if you use it, your
>> concerns about data exchange, locking, and timing should be addressed
>> as a simple consequence of your overall program structure.
>>
>> Jean-Paul
>
>I've looked at Twisted a little bit because of some searches on serial
>port comm turning up advice for it.  However, there seems to be no/
>minimal documentation for the serial portions, like they are some old
>relic that nobody uses from this seemingly massive package.  Do you
>have any examples or somewhere in particular you could point me?

It's true that the serial port support in Twisted isn't the most used
feature. :)  These days, serial ports are on the way out, I think.  That
said, much of the way a serial port is used in Twisted is the same as the
way a TCP connection is used.  This means that the Twisted documentation
for TCP connections is largely applicable to using serial ports.  The only
major difference is how you set up the "connection".  You can see examples
of using the serial port support here (one of them seems to suggest that
it won't work on Windows, but I think this is a mistake):

  http://twistedmatrix.com/projects/core/documentation/examples/gpsfix.py
  http://twistedmatrix.com/projects/core/documentation/examples/mouse.py

The 2nd to last line of each example shows how to "connect" to the serial
port.  These basic documents describe how to implement a protocol.  Though
in the context of TCP, the protocol implementation ideas apply to working
with serial ports as well:

  http://twistedmatrix.com/projects/core/documentation/howto/servers.html
  http://twistedmatrix.com/projects/core/documentation/howto/clients.html

You can ignore the parts about factories, since they're not used with serial
ports.

Hope this helps,

Jean-Paul



More information about the Python-list mailing list