Sending uart/serial data to a TCP server - socat script - howto

I need to forward the uart / serial port data to a TCP server:
data --> uart --> router --> internet --> a TCP server

My openwrt router has installed socat pkg, and I tested the following cmd works fine on terminal:

root@OpenWrt:~# socat -d -d tcp4-connect:,reuseaddr,fork /dev/ttyS0,nonblock,raw,echo=0,ignoreeof

now, I need to install this function into the router, and Non-Stop forever. i.e.:

  1. auto connect to the server on router power-up.
  2. If server is down/reboot/dead, auto-reconnect after the server is active.

or you have any other solutions
many thanks for any help.

create a looping script ?

it could start through rc.local, if the connection dies it'd loop until the
communication is reestablished (assuming the remove host actually kills
the active connection on reboot / shutdown).

You could give ser2net a shot, it even has a LuCI interface (luci-app-ser2net). I use in server mode on OpenWrt to serve my Z-Wave adapter to Home Assistant.

1 Like

thanks, yes, I need a script.
The problem is when the server is down and restart, my router could not know, I did not get any info on terminal when the connection is down. Maybe something wrong.

Maybe someone help me to ask chatGPT, to check what we got (I cannot use chatGPT in my local).

Thanks, the TCP server is in server mode only. I have to let my router work in client mode.
So ser2net maybe not...

My bad, I thought ser2net had a client mode as well.

Edit: You could create a PROCD init script that spawns socat and restarts it once it terminates:

from the [-h] help output, use [-d -d] options, I can get the connection status and back to shell when disconnected.
then I could add reconnect process in the loop of the script.

Once you have the script exit on loss of connection, make it a service in init.d so if it loses connection or crashes out for any other reason, the init system will take care of restarting it.

I'm working on a new luci-app-xxx to setup the function.