Raspberry PI as a Serial Device

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

  1. 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.
  2. 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
  3. 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.

  1. Download and burn Raspbian Lite (2020-02-13) to a micro SD Card.
  2. In the boot partition of the sd-card, create an empty file, name it "ssh" and make sure it has no extension.
  3. Insert the SD card in your Raspi, connect it to your local network and power it up.
  4. 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
  5. After connecting using SSH, update and upgrade your distribution
    sudo apt update
    sudo apt upgrade -y
  6. 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
  7. 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.
  8. 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:
    Raspi Serial Device Usb Gadget
    Notice the “PI USB to serial (COM 16)”

[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:

  1. 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.

  2. 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:
    Raspi Serial Gadget over RealTerm
    Notice the Port Number and the baudRate, those must correspond to your Serial Device options
  3. If you want persistent binding, you can edit rc.local and add the socat command before the "end" keyword:
    sudo nano /etc/rc.local
  4. Add the socat command before "end"
    socat gopen:/dev/ttyGS0,raw tcp:192.168.1.31:2000&

    Persistent Serial Connection using Socat on Raspi

[Use Case 2] Setting Up Raspberry to act as a Serial Device redirecting the onboard UART port

  1. Enable onboard serial port
    echo enable_uart=1 | sudo tee -a /boot/config.txt
  2. Disable onboard bluetooth, because it uses UART for communication.
    echo dtoverlay=pi-disable-bt | sudo tee -a /boot/config.txt
  3. Disable console serial and baudrate
    sed -i 's/console=serial0,115200 //' /boot/cmdline.txt
  4. Turn off your Rapberry and setup the appropriate connections:
    sudo poweroff
    Raspi Arduino UART Schematic
    Double check that your Arduino is 3.3V and not 5V because you may damage your Raspberry!

If you are using a Raspi 4 you can view the connections to the Serial Over IP article

  1. 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.

  2. 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)
  3. If you want persistent binding, you can edit rc.local and add the socat command before the "end" keyword:
    sudo nano /etc/rc.local
  4. Add the socat command before "end". (see picture above)
    sudo socat /dev/ttyS0,raw,echo=0 /dev/ttyGS0,raw,echo=0

Leave a Reply