Socat (serial port forwarding) keeps crashing - how to stabilize?

Hi there,

I have a remote RFXCom RFXTRX433 stick on a raspberry pi zero, that I’m forwarding with socat.
Currently I use this command inside screen:

sudo socat -ls -d -d FILE:/dev/ttyUSB0,b38400,raw,echo=0,ignoreeof TCP-LISTEN:10001,reuseaddr

but it still often crashes:

2022/01/05 06:45:09 socat[26982] N opening character device "/dev/ttyUSB0" for reading and writing
2022/01/05 06:45:09 socat[26982] N listening on AF=2 0.0.0.0:10001
2022/01/05 06:45:30 socat[26982] N accepting connection from AF=2 192.168.1.25:50964 on AF=2 192.168.1.30:10001
2022/01/05 06:45:30 socat[26982] N starting data transfer loop with FDs [5,5] and [7,7]
2022/01/05 06:46:31 socat[26982] W read(7, 0x5afc40, 8192): Connection reset by peer
2022/01/05 06:46:31 socat[26982] N socket 2 to socket 1 is in error
2022/01/05 06:46:31 socat[26982] N socket 2 (fd 7) is at EOF
2022/01/05 06:46:31 socat[26982] N exiting with status 0

Does anyone know how to monitor/restart it in order to get more stability?
I tried various scripts but it didn’t work…

Thanks!

Hi,

pls try on the host side (where the RFXTRX433 stick is)

socat tcp-l:10001,fork,keepalive,nodelay,reuseaddr /dev/ttyUSB0,nonblock,raw

and on the client side (with your IP-adress xxx.xxx.xxx.xxx)

socat -v -d -d -d -d PTY,link=/dev/ttyS0,raw,mode=777 TCP:xxx.xxx.xxx.xxx:10001

@rainer300 just to clarify - do you mean for debugging/logging purposes?
The client side is configured in the RFXCOM binding (only host and port).

You may want to have a look at what I use for my enOcean gateway. I pasted the code here:

After some tweaking it has now been running for months without any problems. Feel free to come back to me if you have any questions.

I gave it a quick test with:

#!/bin/bash
# https://community.openhab.org/t/cant-use-forwarded-socat-serial-port-in-lgtvserial-binding-ioexception/97965
# https://community.openhab.org/t/forwarding-of-serial-and-usb-ports-over-the-network-to-openhab/46597

# use while loop to restart socat on connection end
while /bin/true; do
    sudo socat -d -d -d -s FILE:/dev/ttyUSB0,b38400,raw,echo=0,ignoreeof TCP-LISTEN:10001,reuseaddr
    #socat -d -d -s -T 1200 -lf /openhab/userdata/logs/socat_proxy.log pty,link=/dev/ttyNET0,raw,user=openhab,group=openhab,mode=777 pty,link=/dev/ttyNET1,raw,echo=0
    sleep 1
done &> /dev/null &

But now I managed to create an endless loop it seems, that cannot be killed, haha

I thought you we‘re looking for something more… well… stable :crazy_face:

Joking aside I may have to add that I’m using this script in a docker environment and it is meant to run forever (well at least until container is shut down). The whole point of the loop is that you get a failover if anything breaks. The -T Parameter will force a reconnection after a certain time to recover from a potentially undetected timeout. I may loose a package in this short downtime but my window sensors are not really a matter of life and death…

I think it was my mistake, starting it twice without actually killing the process.
So far it’s working, I will report back if it doesn’t :wink:

Thank you!