Use RFC2217 serial ports using Twisted Python?

Is there a way to connect to the RFC2217 network serial port using Twisted Python?

Pyserial seems to support it using the serial.serial_for_url("rfc2217://...") function serial.serial_for_url("rfc2217://...") . And they indicate that twisted uses pyserial to control serial connections, however twisted.internet.serialport.SerialPort seems to expect a port name or number, which assumes that it just passes this to the serial.Serial constructor.

I can use socat to create PTY from the outside and pass the dev name to twist, which works fine, but I was wondering if I could get around this step using direct support directly.

 socat PTY,link=/dev/myport TCP:192.168.1.222:9001 

Edit: pyserial faq offers this modification to instantiate serial objects:

 try: s = serial.serial_for_url(...) except AttributeError: s = serial.Serial(...) 

Not sure if this will help though ...

+7
python twisted pyserial rfc2217
source share
1 answer

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

+2
source share

All Articles