I concluded that using Pyserial RFC2217 with Twisted Python support is non-trivial. The Pyserial implementation of RFC2217, in addition to being currently experimental , uses threads to control socket connections, which they claim to be a problem for select applications:
The current implementation starts a thread that continues reading from the (internal) socket. The thread is automatically controlled using the rfc2217.Serial port object on open () / close (). However, this can be a problem for custom applications that prefer to use select instead of threads.
It is fairly easy to subclass tiserialport.SerialPort and overwrite the _serialFactory method (which creates the pyserial object that will be used to access the serial port)
class SerialPort(serialport.SerialPort): def _serialFactory(self, dev, *args, **kwargs): " pyserial recommends the following for supporting serial urls " try: return serial.serial_for_url(dev) except AttributeError: return serial.Serial(dev, *args, **kwargs)
However, there is no file descriptor in the resulting object, so the fileno() method (used inside ti_posixserialport ) throws an exception.
--- <exception caught here> --- File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/internet/base.py", line 1204, in mainLoop self.doIteration(t) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/internet/selectreactor.py", line 105, in doSelect [], timeout) File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/twisted/internet/_posixserialport.py", line 48, in fileno return self._serial.fd exceptions.AttributeError: 'Serial' object has no attribute 'fd'
The current workarounds are either to use socat as described in the question, or for the serial network server that I am using (Brainboxes ES-842), you can configure it in "Raw TCP" mode instead of "Telnet / RFC2217" and just use the existing protocol over a TCP connection (if you are not dependent on flow control or other serial control lines and can use a predefined baud rate).
Peter Gibson
source share