[Twisted-Python] Selectable SerialPort Windows/Linux

Markus Hubig mhubig at imko.de
Tue Sep 14 05:24:32 EDT 2010

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:

from serial import Serial, SerialExceptionfrom serial import
SerialDevice(Serial):    def __init__(self, port):
Serial.__init__(self)        self.port = port        self.baudrate =
57600        self.bytesize = EIGHTBITS        self.parity = PARITY_ODD
       self.stopbits = STOPBITS_TWO        self.timeout = 0
self.xonxoff = 0        self.rtscts = 0        self.dsrdtr = 0
def _write(self, packet):        fileno = self.fileno()        while
True:            readable, writeable, excepts = select([], [fileno],
[], 0.1)            if fileno in writeable:                length =
self.write(packet)                break        return length
def _read(self):        fileno = self.fileno()        while True:
      readable, writeable, excepts = select([], [fileno], [], 0.1)
       if fileno in readable:                header = self.read(7)
           length = int(binascii.b2a_hex(header[3]), 16)
 data = self.read(length)                packet = header + data
        break        return packet        def talk(self, packet):
  self._write(packet)        responce = self._read()        return

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" ...

- Markus


IMKO Micromodultechnik GmbH
Markus Hubig
System Administration & Development
Im Stoeck 2
D-76275 Ettlingen / GERMANY

HR: HRB 360936 Amtsgericht Mannheim
President: Dipl.-Ing. (FH) Kurt Koehler

Tel: 0049-(0)7243-5921-26
Fax: 0049-(0)7243-5921-40
e-mail: mhubig at imko.de
internet: www.imko.de
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20100914/337404ea/attachment.htm 

More information about the Twisted-Python mailing list