Logo Questions Linux Laravel Mysql Ubuntu Git Menu

Bridge Serial-Ports over network

For an application, which uses SerialPorts for communication (Modbus RTU, to be exactly) I need to bridge the SerialPort over the network for testing purposes.

So I would like to have the following Setup:

Device        |     Network      |      PC

SerialPort    |------------------|    SerialPort
Map Serialport|                  | Map network to Serialport
   to Network |                  |   

I already got the first part working with socat.

I opened the TCP Port on PC with

nc -l 8080

On my Device I used

socat  pty,link=/dev/virtualcom0 tcp:PC-IP:8080

To map everything written on /dev/virtualcom0 to PC-IP on port 8080.

But now I have problems to map the socket back to a Serialport.

socat tcp:PC-IP:9123 pty,link=/dev/virtualport0

This got me a Connection Refused, which is obvious because i used TCP and the Port is already used by the Device.
So I tried the same with

socat  pty,link=/dev/virtualcom0,raw  udp:PC-IP:8080

and changed everything else to UDP, too.

But then nothing arrives on my /dev/virtlalcom0/ on my PC.

like image 771
0xAffe Avatar asked Mar 17 '23 16:03


1 Answers

One side of the tcp connection needs to be listening on the port (the first one you launch), and the second side connects to it.

For the first side do:

socat tcp-listen:8080 pty,link=/dev/virtualport0

And for the second side do:

socat pty,link=/dev/virtualcom0 tcp:IP-of-other-machine:8080

Forget the netcat, you do not need it.

like image 115
Bjarke Freund-Hansen Avatar answered Mar 23 '23 04:03

Bjarke Freund-Hansen