[Twisted-Python] Selectable SerialPort Windows/Linux

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Tue Sep 14 09:46:03 EDT 2010


On 01:32 pm, mhubig at imko.de wrote:
>On Tue, Sep 14, 2010 at 2:08 PM, <exarkun at twistedmatrix.com> wrote:
>>See the serialport examples on the website.  A couple are linked from
>>http://twistedmatrix.com/documents/current/core/examples/.
>
>The examples I found are mouse.py and gpsfix.py and there only 
>listening on
>the serial line. It seems that with twisted I have some sort of a 
>thread
>running
>which will trigger some methods (e.g. dataRecieved) as it reads 
>something
>from the serial line. How could I send data then?

Call the method for sending data.  Generally, the protocol's transport's 
write method.  Serial ports are not special in this regard, it's how 
almost all protocol implementations in Twisted work.  See some of the 
other documentation (for example the client or server howtos linked from 
<http://twistedmatrix.com/documents/current/core/howto/>) for more 
details on this.
>
>>You do indeed want a Protocol subclass.
>
>
>Ok I see, and the Protocol subclass provides the dataReceived method!?

Typically.
>
>>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).
>
>Hmm ok so I have to design dataReceived() to buffer the received data 
>until
>it get's
>the whole answer packet and than let it call e.g. packetRecieved() to
>process the
>packet ... I'm mot sure if this asynchronous approach is what I need,
>because the
>protocol I'm implementing is strict Master-Slave.

I'm not sure I understand this.  What is a "Master-Slave" protocol?  Why 
would someone not benefit from a Twisted-based implementation of such a 
protocol?

Jean-Paul



More information about the Twisted-Python mailing list