fruggy83 / openocean

27 stars 11 forks source link

Serial Connection fails to reconnect #107

Closed DrRSatzteil closed 3 years ago

DrRSatzteil commented 3 years ago

I'm using socat to create a virtual serial device that in fact connects to my gateway via a TCP connection.

During night time I turn off WiFi so the connection of socat fails and the serial device disappears. In the morning when WiFi is turned on again socat connects to the gateway just fine (it's retrying to connect infinitely) but the gateway thing in OH did not come up. I have to restart OH to make the gateway work again. I could not test this with an actual cable connection but I guess this would lead to the same results of you disconnected and reconnected the cable. For example the OH serial binding handles disconnects/reconnects pretty well, maybe it is worth having a look how this problem has been solved there.

This is there command I am using to setup the virtual device:

socat -d -d -s -lf /openhab/userdata/logs/socat.log pty,link=/dev/ttyNET0,raw,user=openhab,group=openhab,mode=777 tcp:10.9.0.195:9999,keepalive,forever,intervall=10

fruggy83 commented 3 years ago

Thanks for this hint, I will have a look at the serial binding.

DrRSatzteil commented 3 years ago

Please let me know if I can help by testing some more or by anything else. The daily restart routine is not something I want to live with forever 😉

DrRSatzteil commented 3 years ago

For some reason today it seemed to have reconnected just fine in the morning. So a reconnect already works if only in rare cases.

DrRSatzteil commented 3 years ago

Another update: reconnects are working since my last post. There was no obvious change in my setup however so I don't really know if this is fixed for good now. I'll keep on observing for a couple of days and will close this issue if I don't have any more problems.

DrRSatzteil commented 3 years ago

I never really managed to track down what really caused this issue. For now I consider it closed since my setup is rather special anyway. I tweaked my initial socat setup to prevent the "disconnects" of the virtual USB devices by creating two socat instances: This way the serial device will always remain available for OH (and thus OH does not have to deal with reconnects) and use the second socat instance to pipe the received ser2net output to the serial device OH is listening on (/dev/ttyNET0):

socat -d -d -s -T 600 -lf /openhab/userdata/logs/socat.log /dev/ttyNET1,raw tcp:10.9.50.202:9999,connect-timeout=30,forever,intervall=10

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

Note that both commands are executed in a loop in case the timeout (-T) applies.