You are not logged in.
Pages: 1
I'm working on a small home automation project, and I need to use socat to forward a serial port over TCP. As I have two devices, I used an udev rule to put an symlink to my serial port, so the name will always be the same. I have then done some manual testing using socat and I can access it over TCP as I want to. But I want a more permanent solution, so I was thinking to execute socat from my udev rule every time the serial device appears like so:
SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", SYMLINK+="ttyMarantz", RUN+="socat -d -d file:/dev/ttyMarantz,raw,echo=0,b9600,crtscts=0 tcp-listen:1234,reuseaddr,fork"
However, it doesn't work at all, either socat doesn't run or it terminates directly, not sure because I don't know if there is a log for this. Do you have any hints for me how to debug or maybe even a better solution? I just want the port to be always open, it would be nice if it can handle reinserting the cable with an udev rule, but it is ok if a reboot is needed in case that doesn't work.
Edit: As the reason I'm doing this is that the home automation system doesn't seem to support serial ports WITHOUT flow control, I tried using socat to just create a virtual com port (with flow control) like this:
socat -d -d file:/dev/ttyMarantz,raw,echo=0,b9600,crtscts=0 pty,raw,echo=0,crtscts=1
and it actually seem to work. But then we are back to my original problem, how to autorun this without knowing if my device (/dev/ttyMarantz) exists or not? As before, it would be nice if socat were run by the udev rule, when the port is set up, but I don't know how to make that work...
Last edited by UglyBob (2017-04-17 19:12:07)
Offline
Does this help?
Offline
Ah, that could probably work! I will try it out... Thanks
Offline
If it works and the matter is solved always mark the thread as [Solved] then others will know...
“Great minds discuss ideas; average minds discuss events; small minds discuss people.” -Eleanor Roosevelt
“It is our choices, that show what we truly are, far more than our abilities.” -J. K Rowling
-----
How to Ask Questions the Smart Way
Offline
Ok, took some time to test (as I am not super familiar with this stuff), but I think I got it working. This is what I have done (please correct me if something is wrong):
/etc/udev/rules.d/99-usb-serial.rules
SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", ACTION=="add", SYMLINK+="ttyMarantz", TAG+="systemd", ENV{SYSTEMD_WANTS}="socat.service"
/lib/systemd/system/socat.service
[Unit]
Description=Socat System Service
Documentation=man:socat(1)
BindsTo=dev-ttyMarantz.device
After=dev-ttyMarantz.device
[Service]
Type=oneshot
User=openhab
Group=openhab
ExecStart=/usr/bin/socat -d -d file:/dev/ttyMarantz,raw,echo=0,b9600 pty,raw,echo=0,link=/opt/openhab2/ttyMarantz
[Install]
WantedBy=multi-user.target
Then I just enabled the service:
sudo systemctl enable socat.service
In the service I made it easier for myself to avoid permission problems by linking the virtual port inside openhab's home directory and by using its own user. I guess I could solve it by setting permissions on the port too, but that works for me. Thanks again for the help!
Offline
Pages: 1