There are many tutorials explaining how to set up your Raspberry PI as a Serial Device. Almost all of those tutorials explain how to setup your Raspberry as a Serial Console, which will act similar to a SSH connection. The current tutorial takes a different approach. You will find out how to turn your Raspi to a Serial USB Gadget, and how to control what you redirect to the Gadget’s Serial port in order to be available to the machine it will connect to. Using socat, commands we will redirect Serial data received over IP to the Gadget Serial port or data from the on-board UART protocol to the Gadget port.
Requirements
- A Raspberry PI Zero or a Raspberry PI 4. Those are the only devices from the Raspberry family which support USB Gadget mode without any hardware modification.If you plan to use a Raspberry PI Zero, then you will need either an ethernet shield as shown here either, if it’s a Zero W configure the Wireless settings to establish a network connection.
- If you want to redirect serial data received over IP you must have a device with such capability. You can have a look on how to make one such device here
- If you want to redirect data using the UART protocol and you don’t have an available Serial Device, you can make a simple one using an Arduino. Fnd a simple example here. Scroll to "Serial Device"
Preparation
By following these instructions you can make a device which acts similar to MOXA Serial to Ethernet Solutions.
- Download and burn Raspbian Lite (2020-02-13) to a micro SD Card.
- In the boot partition of the sd-card, create an empty file, name it "ssh" and make sure it has no extension.
- Insert the SD card in your Raspi, connect it to your local network and power it up.
- Establish an SSH connection to the raspi using the default credentials (pi/raspberry). If you don’t know how to connect using SSH, use the official guides for Windows or Linux or Mac
- After connecting using SSH, update and upgrade your distribution
sudo apt update sudo apt upgrade -y
- Make your Raspberry PI act as a USB Serial Gadget
# enable device gadget mode echo dtoverlay=dwc2 | sudo tee -a /boot/config.txt echo 'dwc2' | sudo tee -a /etc/modules #enable raspi to act as a Serial Device echo 'g_serial' | sudo tee -a /etc/modules
- Reboot Raspi and connect a micro USB cable (if you are using Raspberry PI Zero) or a USB Type C cable from your computer’s USB port to the USB port of Raspberry PI.
NOTICE: For Raspberry PI ZERO you need to plug the USB cable from your computer to the "USB" connector on the Pi Zero, not the PWR connector. "USB" connector is the one which is closer to the mini HDMI conenctor. - After rebooting, your Raspi should appear in your computer’s Device manager (if your using Windows or in the /dev/ folder if your using Linux) as follows:
[Use Case 1] Setting Up Raspberry as a Serial Device which receives data over IP.
If you have a device (192.168.1.31) which converts Serial Data to IP packets and you want to transform them in order to be received over a COM port then:
- Redirect tcp port traffic to the Raspi Gadget Serial Port using the socat command:
sudo socat gopen:/dev/ttyGS0,raw tcp:192.168.1.31:2000
This command redirects all the data from and to the device with IP 192.168.1.31 to the Raspberry PI’s ttyGS0 serial port. The device with the current IP is a Serial to Ethernet Converter, as seen here.
- On your computer open a COM terminal (i.e Realterm or Putty), and connect to the Raspi port, in my case the the Port Number is 16, as you can see from the screenshot of the Device Manager:
- If you want persistent binding, you can edit rc.local and add the socat command before the "end" keyword:
sudo nano /etc/rc.local
- Add the socat command before "end"
socat gopen:/dev/ttyGS0,raw tcp:192.168.1.31:2000&
[Use Case 2] Setting Up Raspberry to act as a Serial Device redirecting the onboard UART port
- Enable onboard serial port
echo enable_uart=1 | sudo tee -a /boot/config.txt
- Disable onboard bluetooth, because it uses UART for communication.
echo dtoverlay=pi-disable-bt | sudo tee -a /boot/config.txt
- Disable console serial and baudrate
sed -i 's/console=serial0,115200 //' /boot/cmdline.txt
- Turn off your Rapberry and setup the appropriate connections:
sudo poweroff
If you are using a Raspi 4 you can view the connections to the Serial Over IP article
- Forward received data to the USB Host using the socat command:
sudo socat /dev/ttyS0,raw,echo=0 /dev/ttyGS0,raw,echo=0
The current command redirects all the data between port ttyS0, and port ttyGS0.
- On your computer open a COM terminal (i.e Realterm or Putty), and connect to the Raspi port, in my case the the Port Number is 16. (see picture above)
- If you want persistent binding, you can edit rc.local and add the socat command before the "end" keyword:
sudo nano /etc/rc.local
- Add the socat command before "end". (see picture above)
sudo socat /dev/ttyS0,raw,echo=0 /dev/ttyGS0,raw,echo=0