You are not logged in.

#1 2017-04-17 15:01:29

UglyBob
Member
Registered: 2015-11-29
Posts: 54

[Solved] Starting socat from an udev rule

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

#2 2017-04-17 17:08:53

Slithery
Administrator
From: Norfolk, UK
Registered: 2013-12-01
Posts: 5,776

Re: [Solved] Starting socat from an udev rule


No, it didn't "fix" anything. It just shifted the brokeness one space to the right. - jasonwryan
Closing -- for deletion; Banning -- for muppetry. - jasonwryan

aur - dotfiles

Offline

#3 2017-04-17 17:30:13

UglyBob
Member
Registered: 2015-11-29
Posts: 54

Re: [Solved] Starting socat from an udev rule

Ah, that could probably work! I will try it out... smile Thanks

Offline

#4 2017-04-17 17:50:10

Stewart Little
Member
From: England, UK
Registered: 2017-01-18
Posts: 120

Re: [Solved] Starting socat from an udev rule

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

#5 2017-04-17 19:11:30

UglyBob
Member
Registered: 2015-11-29
Posts: 54

Re: [Solved] Starting socat from an udev rule

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

Board footer

Powered by FluxBB