In the end after some research I succeeded in answering your question.
Assume your USB-to-serial dongle is connected and available as /dev/USB0
. Then you first have to find out some information for configuring udev.rules
:
udevadm info -n ttyUSB0 -a
will walk you through the information tree. If, for example, you want the rule to create to match only this specific dongle, you may refer to vendor, product and serial Id:
$ udevadm info -n ttyUSB0 -a
.
.
SUBSYSTEMS=="usb"
.
ATTRS{idProduct}=="6001"
ATTRS{idVendor}=="0403"
.
ATTRS{serial}=="FTHL8XKY"
.
.
then these are the attributes to respect in your rule.
Next, create a udev rule in /etc/udev/rules.d
for this device:
$ sudo vi /etc/udev/rules.d/99-ttyUSB.rules
ACTION=="add", SUBSYSTEMS="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTHL8XKY", RUN+="/bin/stty -F /dev/%k -crtscts"
(Enter the values for your device rsp.). The line reads:
Only when the dongle is added (ACTION="add"
), and when vendor, product and serial match the given values, then run the action, which is defined as you wanted, where '%k' is the kernel name
of the device, in this case ttyUSB0
(but may be different next time).
The name of your rules file is arbitrary, but you should respect the naming conventions used by udev. To see what is going to be executed, you may test by udevadm test -a add /devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4.1/2-4.1:1.0/ttyUSB0/tty/ttyUSB0
(the device path is shown in the udev info, first line).
To see if this works, you may have a look at /var/log/syslog when plugging in the dongle. First configure udev
to show not only error messages by modifying /etc/udev/udev.conf
to read udev_log="debug"
; otherwise you will only see error messages. Restart the udev
service (sudo systemctl restart udev
), then:
$ tail -f /var/log/syslog | grep udev
.
.
Feb 13 14:47:42 desk systemd-udevd[16013]: starting '/bin/stty -F /dev/ttyUSB0 -crtscts'
Feb 13 14:47:42 desk systemd-udevd[15995]: Process '/bin/stty -F /dev/ttyUSB0 -crtscts' succeeded.
.
.
This should also show errors if it does not succeed. udev
tries to run the command different times in different stages, so there will be some errors, but in the end it should succeed.
If you want to have a fixed device symlink created whenever you plug in the dongle, this is also possible. Just add the action to your rule:
ACTION=="add", SUBSYSTEMS="usb", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="FTHL8XKY", SYMLINK+="mynewserial", RUN+="/bin/stty -F /dev/%k -crtscts"
This will create a symlink /dev/mynewserial
you can use in your programs, without regards to dynanmically created devices.
There are a lot more possibilities; this should only give some impression on how to proceed.
udev
rule (see http://www.crashcourse.ca/wiki/index.php/Udev) to set the serial lines when the dongle is activated. You will have to get familiar withudev
rules.. – ridgy Feb 11 '17 at 17:40udev rules RUN
. – ridgy Feb 13 '17 at 10:26