Using Python, How to create a virtual serial port in Linux?

I have two python programs that need to be read from the same serial port via the pySerial API.

From what I understand, you cannot read two programs from the same serial port. So I am planning to make one Python program read from a physical serial port and then copy the EXACT data received from the serial port to virtual serial ports where other python programs can read it through the pySerial API.

Of the two python runs that will use pySerial, only one of them will answer anything.

Any ideas how to do this?

+4


source to share


1 answer


Creating a virtual serial port can be trivial or complex depending on your requirements.

If you only need to distribute data from real port to a virtual port (and vice versa), you can use tools such as socat

, remserial

, or conserver

. See Usage Examples here: 1 , 2 , 3 .



Such tools create a pty (pseudo-tty) and transfer data between the real port and the pty in both directions. However, they do not distribute other APIs, including various calls termios

and ioctl()

specific to the serial port.

If that's not enough, you need more advanced tools like tty0tty

or ser2net

and sercd

(based on RFC 2217 ) and others. See this post.

+1


source







All Articles