Assigning persistent symbolic names for USB serial devices in Linux - brennanyama/RobotOperatingSystem GitHub Wiki

Assigning persistent symbolic names for USB serial devices in Linux

In this walkthrough, we assign persistent driver filenames to USB serial devices plugged into a Linux computer. Normally when plugging in a serial device, it is automatically assigned a driver in the /dev directory, e.g. /dev/ttyUSB0, /dev/ttyACM1, /dev/video2 etc. This can be problematic with multiple USB devices, as the driver filenames are assigned in the order the device is detected by the OS; however, this ordering cannot always assumed to be the same, especially after a reboot. Thus, it is desirable to have persistent names assigned to the individual serial (the word 'serial' meaning a manufacturer part number and not a serial-type communication standard) number of a USB device.

Thank you to the following references:

1. Finding the Vendor ID, Product ID, and serial number of your USB-Serial device

First, unplug all of the USB-serial devices you wish to assign persistent names to. Open a new terminal window, and type:

lsusb

This lists all of the USB devices on your computer, with a Vendor ID and Product ID (in the form VendorID:ProductID). Next, plug in the device you're interested in assigning a persistent name to, wait a few seconds, and type lsusb again. Look for the device that changed from the last list of USB devices, and you now have the Vendor ID, and Product ID of the device you're interested in. Hold on to this information.

If all of the USB devices you use are different, then this is enough information to uniquely identify all of your USB devices; however, if you are using multiple of the same USB device (e.g. multiple Arduinos, cameras, FTDI breakouts (even if the devices plugged into the breakout are different!), then you also need a serial number to distinguish these. To find this information, unplug the device you're interested in, open a new terminal window, and type:

ls /dev

This displays all of the active devices in your /dev directory. Next, plug in the device you're interested in back in, wait a few seconds, and type ls /dev again. Look for the device directory name that changed from the last list of device directories. Open a new terminal window and type:

udevadm info -a -n /dev/[DIERCTORY NAME] | grep '{serial}' | head -n1

Note that you must change [DIRECTORY NAME] to the name of your device directory name. As an example, let's say that this is device video1 (for a USB camera). You should then type:

udevadm info -a -n /dev/video1 | grep '{serial}' | head -n1

The terminal should then display the serial number of your USB-serial device. Hold on to this information.

2. Assigning persistent names

At this point, you should have the Vendor ID, Product ID, and (optinally) the serial number of the device(s) you're interested in assigning a persistent name to. To do this, we will create a UDEV ruleset for each of these new devices. Your UDEV rules are usually scattered in many files in /etc/udev/rules.d (yes rules.d is a directory, not a file).

We want to navigate to this directory and create a new file. The easiest way to do this is by opening a terminal window and typing:

cd /etc/udev/rules.d
sudo gedit

This will open a gedit window. In file, you can add new symbolic links by adding new lines containing information about the symbolic link.

If the device you're interested in is a tty serial device, use this format to create a symbolic link:

SUBSYSTEM=="tty", ATTRS{idVendor}=="[VENDOR_ID]", ATTRS{idProduct}=="[PRODUCT_ID]", ATTRS{serial}=="[SERIAL_NUMBER]", SYMLINK+="[DESIRED_SYMBOLIC_NAME]",MODE="666"

If the device you're interested in is a video serial device, use this format to create a symbolic link:

KERNEL=="video*", ATTRS{idVendor}=="[VENDOR_ID]", ATTRS{idProduct}=="[PRODUCT_ID]", ATTRS{serial}=="[SERIAL_NUMBER]", SYMLINK+="[DESIRED_SYMBOLIC_NAME]",MODE="666"

Note that you must change the bracketed fields as appropriate to your device:

  • [VENDOR_ID] should be the four digit vendor ID you found earlier.
  • [PRODUCT_ID] should be the four digit product ID you found earlier.
  • [SERIAL_NUMBER] should be the serial number you found earlier (omit if not necessary).
  • [DESIRED_SYMBOLIC_NAME] should be the persistent name you wish to assign to the device.

An example for two video devices given the names 'cameraLeft' and 'cameraRight' is:

KERNEL=="video*", ATTRS{idVendor}=="12ab", ATTRS{idProduct}=="34cd", ATTRS{serial}=="1234ABCD", SYMLINK+="cameraLeft",MODE="666"
KERNEL=="video*", ATTRS{idVendor}=="12ab", ATTRS{idProduct}=="34cd", ATTRS{serial}=="5678EFGH", SYMLINK+="cameraRight",MODE="666"

Save and close your file. To commit your changes, go back to your terminal and type:

sudo udevadm trigger

This will commit your new rules. To check that your rules worked, you can use ls /dev. When you plug in your device, it will still generate a generic tty or video directory in /dev; however, it will also generate a symbolic link (matching your desired name), which points to the correct generic directory. Instead of referencing a generic link, you can now reference the symbolic link you created to always reference the correct USB device (as long as it's plugged in and detected).

You can explicitly check that your symbolic link works by typing into your terminal window:

ls -l /dev/[DESIRED_SYMBOLIC_NAME]

Which should output the generic directory associated with your persistent symbolic link.