Can't use forwarded (socat) serial port in lgTVSerial Binding - IOException

Hi,

I have openHAB 2.5.1 running in a docker container on my Synology NAS. I have my LG TV connected via the serial port to a raspberry pi in my network.

I published the serial port from the raspberry pi using ser2net with the following config

3002:raw:0:/dev/ttyUSB0:9600 8DATABITS NONE 1STOPBIT
# alternatively I used 600 as timeout value - no difference

I build a custom docker image based on the openHAB 2.5.1 image in which I install socat and put the following script in /etc/cont-init.d/:

#!/bin/bash

mkdir -p /openhab/virtualdev
chown openhab:openhab /openhab/virtualdev -R

# use while loop to restart socat on connection end
while /bin/true; do
    gosu openhab socat -d -d -s -lf /openhab/userdata/logs/socatLGTV.log pty,link=/openhab/virtualdev/ttyLGTV,cfmakeraw,user=openhab,group=openhab,mode=777 tcp:addressofraspberry.local:3002
    sleep 1
done &> /dev/null &

I also tried several variations of the socat command with various combinations of options without any difference.

And providing the serial port in the EXTRA_JAVA_OPTS in my docker-compose file:

EXTRA_JAVA_OPTS: -Dgnu.io.rxtx.SerialPorts=/openhab/virtualdev/ttyLGTV

The created /openhab/virtualdev/ttyLGTV seems to be working. When I login to the container using:
docker exec --user openhab -it openhab /bin/bash
It’s possible to mute/unmute my TV by executing a command such as:
echo "ke 00 00" > /openhab/virtualdev/ttyLGTV.

I have the following thing and item configuration:

# lgTV.things file
Thing lgtvserial:lgtv:LGTV_Wohnzimmer [port="/openhab/virtualdev/ttyLGTV", setId=0]

# lgTV.items file
Switch GF_LivingRoom_TV_Mute    "TV Mute"   (GF_LivingRoom) {channel="lgtvserial:lgtv:LGTV_Wohnzimmer:volume-mute"}

But every time I use the switch I always get the following exception in the openhab.log. (Translation of the message: “Input output error in writeArray”

2020-05-02 22:25:03.498 [WARN ] [l.internal.handler.LgTvSerialHandler] - Serial port write error
java.io.IOException: Eingabe-/Ausgabefehler in writeArray
	at gnu.io.RXTXPort.writeArray(Native Method) ~[bundleFile:3.15.0.OH2]
	at gnu.io.RXTXPort$SerialOutputStream.write(RXTXPort.java:1171) ~[bundleFile:3.15.0.OH2]
	at org.openhab.binding.lgtvserial.internal.protocol.serial.LGSerialCommunicator.write(LGSerialCommunicator.java:72) ~[bundleFile:?]
	at org.openhab.binding.lgtvserial.internal.protocol.serial.commands.BaseLGSerialCommand.execute(BaseLGSerialCommand.java:56) ~[bundleFile:?]
	at org.openhab.binding.lgtvserial.internal.handler.LgTvSerialHandler.handleCommand(LgTvSerialHandler.java:182) [bundleFile:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_232]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_232]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_232]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_232]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.InvocationHandlerSync.invoke(InvocationHandlerSync.java:59) [bundleFile:?]
	at com.sun.proxy.$Proxy164.handleCommand(Unknown Source) [?:?]
	at org.eclipse.smarthome.core.thing.internal.profiles.ProfileCallbackImpl.handleCommand(ProfileCallbackImpl.java:74) [bundleFile:?]
	at org.eclipse.smarthome.core.thing.internal.profiles.SystemDefaultProfile.onCommandFromItem(SystemDefaultProfile.java:48) [bundleFile:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_232]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_232]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_232]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_232]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_232]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_232]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_232]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]

I spent the last few days trying to get this to work, but at this step I don’t know what’s the problem. Is it the socat configuration? Some missing setting? The way the binding uses the port? Some dependency that must be installed in the docker container? Something else?

Edit
Found out another detail. I tried to use the SerialBinding (not the LGTV specific one) with the following item:

Switch GF_LivingRoom_TV_Mute_Test   "TV Mute test"  (GF_LivingRoom) {serial="/openhab/virtualdev/ttyLGTV@9600,ON(ke 00 01\n),OFF(ke 00 00\n)"}

And this sends the command to the TV and it works exactly once. After that, the log shows a very similar error just without the stack trace:

2020-05-03 01:24:01.665 [WARN ] [binding.serial.internal.SerialDevice] - Error writing 'ke 00 00
' to serial port /openhab/virtualdev/ttyLGTV: Eingabe-/Ausgabefehler in writeArray

Looks like this has something to do with socat terminating after each connection.

Edit 2
I think ser2net might be the problem. If I run it with the -d flag, I get the following error after each command:

ser2net[5191]: Select exception on device for port 3002

And after that the socat process on the other machine terminates.
… will investigate further
Can you give me a hint, please?
Thanks for your help.

After i removed this, i can successfully use socat inside the docker.

my initd-script is:

#!/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

apt-get update
apt-get install socat -y -q

# use while loop to restart socat on connection end
while /bin/true; do
    socat -d -d -s -lf /openhab/userdata/logs/socat.log pty,link=/dev/ttyNET0,raw,user=openhab,group=openhab,mode=777 tcp:192.168.188.17:20108,forever,intervall=10
    sleep 1
done &> /dev/null &

and i mount this file along with the docker run:

docker run \
        --name openhab \
        -v <<LOCALPATH>>/initd.sh:/etc/cont-init.d/install-socat.sh \
        openhab/openhab:latest
2 Likes