Wednesday, November 22, 2017

Proxy serial over TCP with socat

I recently needed to talk to a serial port from a remote system.  I found a Linux tool called socat - Multipurpose relay (SOcket CAT) that worked perfectly.  The target was a Raspberry Pi with a connected serial device.

To install socat on the Raspberry Pi, and the friend Ubuntu system:

sudo apt-get udpate
sudo apt-get install socat

On the Raspberry Pi I started the socat server with:

# socat tcp-listen:8000,reuseaddr,fork file:/dev/ttyUSB0,nonblock,waitlock=/var/run/tty0.lock,b115200,raw,echo=0

This will connect to the serial port, and stay connected after disconnected clients.

On the Ubuntu friend system I started the socat client with:

socat pty,link=/dev/ttyUSB0,waitslave

After each disconnect the socat client will disconnect as well.  To keep it running, throw it in a loop:

# while true ; do socat pty,link=/dev/ttyUSB0,waitslave ; sleep .001 ; done

I then used tio (or minicom) to talk to the remote serial port:

# tio -b 115200 /dev/ttyUSB0
[tio 16:53:30] tio v1.20
[tio 16:53:30] Press ctrl-t q to quit
[tio 16:53:30] Connected

> I sent something to the serial port

No comments: