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 tcp:pi.oeey.com:8000

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 tcp:pi.oeey.com:8000 ; 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







minicom - friendly serial communication program


minicom

minicom - is a menu driven communications program. It emulates ANSI and VT102 terminals. It has a dialing directory and auto zmodem download.

Installation


# on RedHat based:
yum install minicom

# on Debian based:
sudo apt-get install minicom

Special Keys


# Menu
CTRL-A Z

# To Quit
CTRL-A Q

# To send (via xmodem/ymodem/zmodem)
CTRL-A S

# To receive (via xmodem/ymodem/zmodem)
CTRL-A R

# To configure
CTRL-A O

Command Line


# Bring up minicom configuration
minicom -s

# Connect to ttyUSB0 with 115200 baud rate
minicom -b 115200 -D /dev/ttyUSB0
The default configuration is 115200 8N1 with Hardware Flow Control. I usually disable Hardware Flow Control in my setting and save to the default 'df1'.

Tio


Although minicom describes itself as "friendly", it can be painful to setup and use.  As a suggestion, if you don't need send/receive abilities like xmodem, tio is much easier to use.

References







tio - A simple TTY terminal I/O application

Minicom is painful to use. I found a much easier tool to use called tio - A simple TTY terminal I/O application.

Usage


tio /dev/ttyUSB0
tio -b 115200 /dev/ttyUSB0  # 115200 is the default baud rate

Installing Tio



Using the release tar method:
mkdir ~/.src
cd ~/.src
wget https://github.com/tio/tio/releases/download/v1.20/tio-1.20.tar.xz
tar -avxf tio-1.20.tar.xz
cd tio-1.20/
./configure
make && make install

Using the source source method:
sudo apt-get install autoconf
mkdir ~/.src
cd ~/.src
git clone https://github.com/tio/tio.git
cd tio
sh autogen.sh
./configure
make && make install

Example Usage


# 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

> some serial text

Exiting


To exit tio:
ctrl-t q


Other references