Serial over IP is the easiest solution for transferring Serial Protocol Data (either at TTL level, or RS232/RS485 Specifications) over long distances. Devices responsible for this type of conversion are often called Serial to Ethernet Converters.
Security Notice
Before proceeding further, it’s good to know that all the data transferred, after the conversion, through the TCP packets are in clear-form. Other forms of protection may be implemented for securing the transfer (SSH tunneling, VPN etc)
How it works
The two most common implementations for transferring Serial data over IP are Raw transfer and the (still) experimental RFC2217 – Telnet Com Port Control Option. Serial Proxy Ser2net support both of them, and in combination with a Raspberry PI’s GPIO UART Connection or a USB to Serial Converter can be used as a Serial To Ethernet Converter.
Requirements
A Raspberry PI with network connectivity (either wired or wireless) or any Computer with a Debian Linux distribution.
A Serial Device for the tests. [Here an Arduino Pro Mini – 3.3V 8 MHz is being used]
A USB to Serial converter for RS232/RS485 signals or a TTL to RS232/RS485 converter.
The Serial Device
For serial device use the following script on an Arduino Pro Mini 3.3v 8Mhz. This script counts the seconds passed since boot and print them to the Serial. Additionally is checking for available incoming data and print them too.
Communication Using TTL
TTL Communication is accomplished by connecting the Arduino TX/RX pins to Raspberry’s UART RXD/TXD pins. Pay special attention because most Arduino Boards are using 5v logic which can damage your Raspberry, double check that you are using a 3.3v Arduino. For all Raspberry PI boards, GPIO 14 act as UART Tx and GPIO 15 as UART Rx. Notice that to establish communication Arduino’s TX must connect to Raspberry’s RX and Arduino’s RX to Raspberry’s TX. In addition, have in mind that Arduino Boards from different manufacturers may have different pinouts.
At the following picture, Arduino is both powered from Raspberry PI and connected for TTL Serial Communication.
After the connection, enable the UART port and Disable Serial Console using the following commands:
echo enable_uart=1 | sudo tee -a /boot/config.txt
sudo sed -i 's/console=serial0,115200 //' /boot/cmdline.txt
Warning: If a Raspberry PI 3b or newer is used, Bluetooth module must disabled because is using the primary UART port for communication with the Raspberry. Disable it by using:
echo dtoverlay=pi-disable-bt | sudo tee -a /boot/config.txt
Communication Using RS232
Using a Max232 Logic Converter, we can convert Arduino’s TTL to RS232 and receive it to the Raspberry PI using a USB to Serial Device.
Serial Ports on Raspberry PI
Now that the serial device is connected, to the Raspberry PI it can be accessed under the /dev/ directory. List all devices using:
ls /dev/
In the following screenshot ttyAMA0 is the UART device on Raspberry PI (if you followed the above configuration) and ttyUSB0 is the USB to Serial Converter.
Transmitting Serial Over Ethernet
Install ser2net to Raspberry using the command:
sudo apt-get install ser2net -y
After the installation, edit the configuration file and add the Serial device configuration which will be used. In the following picture you can see that the default port redirections were comment out (using # at the beginning of each line), and a USB to serial device (ttyUSB0) (baud rate=9600,8 data bits, no parity bit, 1 stop bit and the default banner) will be redirected to port 2000.
2000:raw:0:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT banner
If you want to redirect the TTL port then instead of ttyUSB0 use ttyAMA0.
Other implementations
Similar results can be accomplished using netcat, socat or pyserial (Python implementation).
Receiving Serial Over Ethernet
The next step is to setup the receiving part of the communication. With the appropriate port forwarding on your ISP’s Modem the serial device could be accessible over the internet.
Receiving using Virtual Ports
Linux – socat
Install socat (for Debian you can use apt):
sudo apt install socat
sudo socat pty,link=/dev/ttyVirt,raw tcp:192.168.1.44:2000&
Now the /dev/ttyVirt port is available for any other software. (For example minicom)
Windows – com0com & RealTerm
- Download and install com0com – Null modem emulator to create a virtual COM port pair. Using com0com all the data received on the first port will automatically redirected to the second port and vice versa. This data exchange will happen in the background without locking the second port, so it will be free for use in any other software.
- Download and install RealTerm
- Run Realterm app
- Select "Port" tab.
- Set your port options (Baud rate, Parity bits, etc)
- At the port field type the IP Address followed by port in the form IP:PORT
- Finally press the "Open" button to establish the connection
- From Real Term, select the "Echo Port" tab.
- Configure your COM port’s settings. (Baud Rate, Parity bits etc)
- At the "Port" field, select one of the two virtual ports created using the com0com.
- Select the "Echo On" option.
- Click the "Change" button
In the following pictures you can see both "Port" and "Echo Port" tabs configuration as well as the com0com configured and a Putty terminal running on the Virtual Com Port.