Create a virtual serial port connection over TCP

Upvote picture Upvote · Mar 25, 2014 · Viewed 59.6k times · Source

I am developing an application that should be able to write to a virtual serial port and receive data through the same port from remote clients over network.

The application runs on a linux server. I am new in using serial ports and I have some questions on this topic.

Clients

The client can establish a TCP connection to a server. When we setup a client, we have to provide the IP address of the server, a tcp port (usually 8080) and a virtual com port.

The client then will automatically try to connect to the server.

Server

The server has a virtual com port, the same we set in the client config (e.g. COM1). When an application on the server writes data to this port, the data should be send to all clients connected via tcp. The response from the clients is send over TCP back to the server which can read it over the virtual serial port.

Question

On windows I used a virtual serial port connector http://www.eterlogic.com/Products.VSPE.html which did most of the work. However I want to solve this problem on linux machines.

My question is, how can I create a TCP server that has a virtual serial port attached and can send/receive data through this port over TCP to listening clients?

Answer

yegorich picture yegorich · Mar 27, 2014

Try socat. Possible scenario:

socat  pty,link=/dev/virtualcom0,raw  tcp:192.168.254.254:8080&

socat creates TCP connection to 192.168.254.254:8080, so that everything, that will be written to /dev/virtualcom0 will be forwarded to 192.168.254.254:8080 and vice versa.

Another approach would be to use RFC2217 via ser2net on Linux sever side and RFC2217 driver on Windows side (for example http://www.hw-group.com/products/hw_vsp/index_en.html single port version). You can also try to get http://pyserial.sourceforge.net/ to work with ser2net.