Remote serial port with ser2net - nilsburg/connected-pi-car GitHub Wiki

My Setup

I've got the SIM7000E HAT plugged in a Raspberry Pi Zero WH which is located in my office at home.

Since I normally don't have local access to the device I usually connect to the rpi through SSH. The problem is that the port which is used to communicate with the HAT is /dev/ttyS0 which is local. So any script that wants to use the serial port needs to be launched locally.

Fortunately there is a way to use the serial port remotely: ser2net.
Install ser2net and edit the following file /etc/ser2net.conf and at the end of the file add (if it is not already there):

2000:telnet:600:/dev/ttyS0:9600 8DATABITS NONE 1STOPBIT banner

then launch:

ser2net -n

After this you can connect remotely to the serial port using telnet (let say the IP address of the rpi is 192.168.1.2):

telnet 192.168.1.2 2000

How to use with python and pyserial

To connect to the remote serial port with pyserial, instead of using:

ser = serial.Serial('/dev/ttyS0', 9600)

Use the following code

ser = serial.serial_for_url('socket://192.168.1.2:2000',9600)

Now you can launch the script locally and communicate with the HAT .

ser2net reference: https://networklessons.com/network-management/raspberry-pi-as-cisco-console-server/