Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

Udev and systemd for reliable socat serial-to-TCP redirection?

I need some help as I'm a newbie on systemd and udev. I need to forward a virtual com port (VCP) over TCP to a server when the VCP (that is a USB device) is present, looking on internet I found this solution:

  • setup udev rules that trigger a systemd service
  • setup a systemd service that run socat

I do it and have it works when all is fine, but my problem is have it robust on failures. So to have it work I add those udev rules to trigger tell to systemd when the VCP appear and disaper:

ATTRS{idProduct}=="XXXX",ATTRS{idVendor}=="YYYY", ACTION=="add", SYMLINK+="myserial", TAG+="systemd", ENV{SYSTEMD_WANTS}="socat.service"                                                                                            
ATTRS{idProduct}=="XXXX",ATTRS{idVendor}=="YYYY", ACTION=="remove", SYMLINK+="myserial", TAG+="systemd" 

And this is the systemd service:

[Unit]                                                                                                                
Description=Socat service to forward serial port                                                                      
BindsTo=dev-myserial.device                                                                                         
After=dev-myserial.device                                                                                                                                                                                            
                                                                                                                      
[Service]                                                                                                             
Type=simple                                                                                                           
ExecStart=/usr/bin/socat FILE:/dev/myserial,b115200,raw TCP:www.www.www.www:zzz                                         
Restart=on-failure                                                                                                    
RestartSec=10                                                                                                         
                                                                                                                      
[Install]                                                                                                             
WantedBy=dev-myserial.device

This works when the sever dies (to test I kill it manually) and after 10s there is a new attempt that fail for "Connection refused" error (saw it with journalctl). But if I disconnect the device there are more attempts and error on journalctl become "No such file or directory" for the myserial port. Looks like removing the device has no effect.

Checking with systemctl status dev-myserial.device after I add the second rules the device become inactive.

I think is due to a conflict: the service is stopped after the restart is already set. Is that the problem?

Any idea how to solve it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...