How to Forward Serial Port Over TCP/IP in Linux for Remote Arduino Programming


4 views

When working with embedded devices like Arduino boards connected to remote Linux servers, accessing the serial port over LAN becomes essential for debugging and programming. Traditional solutions often require physical access or complex setups. Here's a comprehensive guide to solving this efficiently.

After evaluating several options, remserial emerges as the most promising open-source solution. Unlike proprietary alternatives, it's specifically designed for Linux environments and provides reliable TCP/IP forwarding of serial ports.

# Installation on Ubuntu/Debian
sudo apt-get install build-essential
wget http://lpccomp.bc.ca/remserial/remserial-1.4.tar.gz
tar -xzf remserial-1.4.tar.gz
cd remserial-1.4
make
sudo cp remserial /usr/local/bin/

On your Ubuntu 10.04 machine with the connected Arduino:

# Start remserial server (typically /dev/ttyACM0 or /dev/ttyUSB0 for Arduino)
remserial -d -p 1234 /dev/ttyACM0 57600

# For persistent operation, create a systemd service:
[Unit]
Description=Serial Port Forwarding
After=network.target

[Service]
ExecStart=/usr/local/bin/remserial -d -p 1234 /dev/ttyACM0 57600
Restart=always
User=root

[Install]
WantedBy=multi-user.target

For platform-independent access, you can use several approaches:

# Linux client using netcat:
nc server_ip 1234 | tee received_data.txt

# Windows alternatives:
# 1. PuTTY (Raw connection to server_ip:1234)
# 2. Termite (https://www.compuphase.com/software_termite.htm)

For more flexibility, socat provides additional features:

# Server side:
socat TCP-LISTEN:1234,fork,reuseaddr FILE:/dev/ttyACM0,nonblock,raw,echo=0,b57600

# Client side:
socat - TCP:server_ip:1234

For production environments, consider adding:

  • Firewall rules limiting access to specific IPs
  • SSH tunneling for encryption (ssh -L 1234:localhost:1234 user@server)
  • VPN solutions for remote access

When working with embedded devices like Arduino boards connected to Linux servers, physical access constraints often necessitate remote serial port access. The fundamental requirement is to redirect serial communication (typically /dev/ttyUSB0 or /dev/ttyACM0) through TCP/IP networks while maintaining low latency and stable connections.

The most robust open-source tool for this scenario is socat (Socket Cat), available in Ubuntu repositories:

sudo apt-get install socat

Server-side setup (on Ubuntu machine):

socat TCP-LISTEN:54321,reuseaddr,fork FILE:/dev/ttyACM0,raw,nonblock,waitlock=/var/run/ttyACM0.lock

This creates a TCP server on port 54321 that forwards all data to/from the Arduino's serial port.

For dedicated serial-to-TCP forwarding, remserial provides a lighter solution:

# Installation
wget http://lpccomp.bc.ca/remserial/remserial-1.4.tar.gz
tar xvf remserial-1.4.tar.gz
cd remserial-1.4
make
sudo cp remserial /usr/local/bin/

# Usage
remserial -d -p /dev/ttyACM0 -s "9600 raw" -l 54321

Linux/Mac clients:

socat - TCP4:server-ip:54321

Windows clients:
Use PuTTY or Tera Term connecting to server-ip:54321

For persistent connections, create a systemd service:

[Unit]
Description=Serial over TCP
After=network.target

[Service]
ExecStart=/usr/bin/socat TCP-LISTEN:54321,reuseaddr,fork FILE:/dev/ttyACM0,raw,nonblock,waitlock=/var/run/ttyACM0.lock
Restart=always

[Install]
WantedBy=multi-user.target

Always restrict access to your serial port server:

# Using iptables
sudo iptables -A INPUT -p tcp --dport 54321 -s 192.168.1.0/24 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 54321 -j DROP
  • Check serial port permissions: sudo usermod -aG dialout $USER
  • Verify port availability: netstat -tulnp | grep 54321
  • Test raw connection: nc server-ip 54321