Async PySerial (was Re: pySerial Windows write problem)

Peter Hansen peter at engcorp.com
Fri Jul 29 17:32:09 EDT 2005


phil wrote:
> I use PySerial in a 16 line data collection system
> with LOTS of threads, and yes am frustrated by read().
> This sounds excellent, keep us updated.
> 
> BTW, haven't done any event driven Python except Tkinter.
> Would this a class library  which would let you
> define an event and a handler?

Roughly speaking, yes.  The primary parent class happens to be called 
Handler, and is a threading.Thread subclass which in its run() method 
basically sits in a win32 WaitForMultipleObjects() call for various 
things to happen, then calls handler routines based on which event fires.

> Do you have a one line code example?

One line?  No way... this isn't Perl. ;-)


from bent.serial import Driver

class MyDriver(Driver):
     def __init__(self, port, baud=9600):
         Driver.__init__(self, port, baud=baud, name='iodriver')
         self.start()   # start the thread

     def handleSerialRead(self, data):
         print 'read %r' % data

     def handleSerialDsr(self, level):
         print 'DSR', level

     def handleSerialBreak(self):
         print 'break detected, ho hum'


Usage for this would be simply:  d = MyDriver('COM3') and then sit back 
and watch the, uh, fireworks... or at least the print statements.

Anything interesting represents a much more sophisticated subclass which 
parses the data as it arrives, of course, and at least in my case then 
calls a handlePacket() routine where the fun really begins.

-Peter



More information about the Python-list mailing list