[Twisted-Python] Selectable SerialPort Windows/Linux

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Sep 14 08:08:36 EDT 2010


On 09:24 am, mhubig at imko.de wrote:
>Hi @all!
>
>I'm trying to write a python library module for a special
>serial communication protocol called IMPBUS. To use the serial
>interface for sending and receiving packets as for now I'm
>sub-classing pyserial. My code looks like this:
>
>[snip]
>
>But the problem is that I can't use select with pyserial on Windows,
>because it don't provide the fileno() methode. So after some googling
>I found twisted.internet.serialport "A select()able serial device, 
>acting
>as a transport."
>
>I never used twisted before so I'm a little overwhelmed by how I can
>replace pyserial with twisted in the code above ... maybe someone can
>point me to the right direction. It seems I need a "Protocol" and a
>"receiver" ...

See the serialport examples on the website.  A couple are linked from 
http://twistedmatrix.com/documents/current/core/examples/.

You do indeed want a Protocol subclass.  The code from your 
"self.read(7)" line to the end of that loop will probably end up in a 
dataReceived method - but note that you'll have to do some buffering, as 
you can't be assured that dataReceived will get called with exactly the 
number of bytes you want at a time, so you may have to collect data from 
multiple dataReceived calls (likewise you may get more than you want at 
once, and need to split it up).

Jean-Paul



More information about the Twisted-Python mailing list