I want to use my Raspberry Pi as a serial-to-TCP converter. The Raspberry Pi should be a TCP-server that accept one TCP client (because more connections would end in a complete mess on the serial port!).

I want to connect to the Raspberry Pi from a computer that reads out data from a device that is connected to the Raspberry Pi with a serial converter. The serial converter already works, and I've also set up a WLAN access point.

Are there built-in tools in Debian that help me doing this? I don't want that the Raspberry Pi connects to another computer and redirects the serial port to a port on the computer. I want the Raspberry Pi to act as a server where clients can connect to the port and talk to the device that is connected to the serial port.

  • 2
    The ser2net application can do this. It's probably in Debian. – Ronny Nilsson Jun 30 '14 at 21:03
  • socat also could be used, and do not forget about ppp package. – tovis Jan 27 '15 at 15:43

For basic network communication nc or netcat, as suggested by joan, is sufficient. However, if you for example want to forward a serial console or encrypt the connection socat is probably more convenient. For inspiration here are two working examples of the mentioned applications.

Serial console via TCP

To connect directly to the serial port with socat the following command can be used:

socat -,raw,echo=0,escape=0x1d /dev/ttyUSB0,b115200,echo=0,raw

The escape char is set to Ctrl-].

This can be expanded into a tcp-server like this:

socat tcp-listen:54321,reuseaddr /dev/ttyUSB0,b115200,raw,echo=0

This will allow only one client to connect, as per the OPs request. Wrap it with a while-loop to keep the server going between reconnects:

while true; do
  socat tcp-listen:54321,reuseaddr /dev/ttyUSB0,b115200,raw,echo=0

Now you can connect to the serial port through the tcp-server like this:

socat -,raw,echo=0,escape=0x1d tcp:server-host:54321

Serial console via SSL

To encrypt the connection, you first need to generate two key-pairs, see Example for openssl connection using socat for the details, here is a summary:

# Generate self signed server certificate
openssl genrsa -out server.key 1024
openssl req -new -key server.key -x509 -days 3653 -out server.crt
cat server.key server.crt > server.pem
chmod 600 server.key server.pem

# Generate self signed client certificate
openssl genrsa -out client.key 1024
openssl req -new -key client.key -x509 -days 3653 -out client.crt
cat client.key client.crt > client.pem
chmod 600 client.key client.pem

Copy the pem-files to their respective hosts, do the opposite with the crt-files, e.g.:

scp server.pem client.crt server-host:
scp client.pem server.crt client-host:

Now the server can be run like this:

socat openssl-listen:1443,reuseaddr,cert=server.pem,cafile=client.crt /dev/ttyUSB0,b115200,echo=0,raw

Connect to the serial console like this:

socat -,raw,echo=0,escape=0x1d openssl:server-host:1443,cert=client.pem,cafile=server.crt

It is indeed possible with netcat. What you do is you have netcat listen to a port that you choose, and have it write to, and take input from, the serial port. The command itself will be something similar to

nc -l $PORT < /dev/ttyS0 > /dev/ttyS0
  • 3
    The trick will be getting this to know when to give up on an open (but abandoned) connection and loop back to run again and potentially accept a new connection. Otherwise various client/network failures can leave it stuck for days. Netcat does have a timeout option. Also something like stty will need to configure the port. – Chris Stratton Jan 27 '15 at 15:18

The netcat package may do what you want.

You should be able to redirect the serial link to and from a network address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.