hippy
Posts: 6098
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Symlinking /dev/ttyUSB

Thu Nov 22, 2018 11:54 am

If one plugs in a USB-to-serial adapter a /dev/ttyUSB0 entry will be created -

Code: Select all

pi@Pi3B:~$ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Nov 22 11:44 /dev/ttyUSB0
pi@Pi3B:~$
Also a /dev/serial/by-path directory and entry will be created which links to that -

Code: Select all

pi@Pi3B:~$ ls -l /dev/serial/by-path/
total 0
lrwxrwxrwx 1 root root 13 Nov 22 11:44 platform-3f980000.usb-usb-0:1.2:1.0-port0 -> ../../ttyUSB0
pi@Pi3B:~$
That is the top left USB socket of Pi 3B when viewed from the socket end.

One can create a /dev/ttyUSBTL symlink to allow that as a reference to the /dev/ttyUSB plugged into the top-left socket using -

Code: Select all

sudo ln -s /dev/serial/by-path/platform-3f980000.usb-usb-0:1.2:1.0-port0 /dev/ttyUSBTL
And that appears to work -

Code: Select all

pi@Pi3B:~$ ls -l /dev/ttyUSB*
crw-rw---- 1 root dialout 188, 0 Nov 22 11:44 /dev/ttyUSB0
lrwxrwxrwx 1 root root        61 Nov 22 11:50 /dev/ttyUSBTL -> /dev/serial/by-path/platform-3f980000.usb-usb-0:1.2:1.0-port0
pi@Pi3B:~$
Unfortunately, when the USB-to-serial connector is unplugged, /dev/ttyUSB0 disappears, the /dev/serial/by-path/ directory disappears, but the /dev/ttyUSBTL link remains.

Code: Select all

pi@Pi3B:~$ ls -l /dev/ttyUSB*
lrwxrwxrwx 1 root root        61 Nov 22 11:50 /dev/ttyUSBTL -> /dev/serial/by-path/platform-3f980000.usb-usb-0:1.2:1.0-port0
pi@Pi3B:~$

Code: Select all

pi@Pi3B:~$ ls -l /dev/serial/by-path
ls: cannot access '/dev/serial/by-path': No such file or directory
pi@Pi3B:~$
Is there any way to make the symlinked /dev/ttyUSBTL also disappear when /dev/ttyUSB0, and reappear when the cable is plugged back in, or does anyone know a better or proper way this should be done ?

Aydan
Posts: 699
Joined: Fri Apr 13, 2012 11:48 am
Location: Germany, near Lake Constance

Re: Symlinking /dev/ttyUSB

Thu Nov 22, 2018 12:09 pm

You'll have to create a udev rule matching the path information and adding an extra symlink there.

HiassofT
Posts: 220
Joined: Fri Jun 30, 2017 10:07 pm
Location: Salzburg, Austria
Contact: Website

Re: Symlinking /dev/ttyUSB

Thu Nov 22, 2018 12:26 pm

Yes, udev rule is the way to go.

I have several USB serial adapters here in use on my main PC, often plugged in at the same time. Using the manufacturer and serial (-number) attributes is a rather reliable way to distinguish them.

Run "udevadm info -a -n /dev/ttyUSBx" to see the available attributes that you can use in udev rules.

Here's the udev rule I'm using for the /dev/ttyUSB-rpi symlink (serial console for my development RPi) - I put that in /etc/udev/rules.d/70-persistent-serial.rules

Code: Select all

ACTION=="add|change", SUBSYSTEM=="tty", KERNEL=="ttyUSB*", \
  ATTRS{manufacturer}=="ftdi", ATTRS{serial}=="ftEH00H1", \
  SYMLINK+="ttyUSB-rpi"
so long,

Hias

hippy
Posts: 6098
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Symlinking /dev/ttyUSB

Thu Nov 22, 2018 12:51 pm

Many thanks both for the pointer towards udev.

I'm going to have to think more about this as I think I need the symlinks to reflect the sockets a particular cable is plugged into, may have multiple cables with same VID/PID and serial number.

It is quite a challenge and I am still figuring out the various ways it can or could be done. I am aware there cannot be a fully automated way of doing it.

Return to “Advanced users”