Managing USB Devices

RoboFlock reads from multiple USB devices and it is important that these do not get mixed up. When a USB device is plugged in, Linux adds an entry for the device to the /dev/ directory and gives it a name such as /dev/ttyUSB0 (which we will use as our example device for this tutorial). The issue here is that we can’t guarantee that our devices will be given the same entry name everytime. The solution is to create a Udev rule that assigns a symbolic link to the device based on details such as the product ID.

To do this, first plug in the device and find its entry name by either manually checking the /dev/ directory, or by running the command dmesg | grep ttyUSB*. Next, we need to grab some identifiers about the USB device. To do this, run the following using the appropriate name for your device:

$ udevadm info --name=/dev/ttyUSB0 --attribute-walk

You can either scroll through the output or use the command grep to find the correct values for ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", and ATTRS{serial}="zzzzzzzz". Now we can write our custom rule for the USB device. Create the rules file:

$ sudo gedit /etc/udev/rules.d/99-my-serial.rules
Note

Use whatever text editor you like, it doesn’t have to be gedit as long as it is run as sudo

and write the following content:

SUBSYSTEM=="tty", ATTRS{idVendor}=="xxxx", ATTRS{idProduct}=="yyyy", ATTRS{serial}=="zzzzzzzz", SYMLINK+="my_usb"

Hint

To add more rules, simply define it on a new line in this file. No need to create a new file for each rule.

After saving the file, reload the udev rules by either logging out and back in, or by running the follow commands:

$ sudo udevadm control --reload-rules && sudo udevadm trigger

To verify that the rule is in effect, run the following command:

$ ls -lF /dev/ttyUSB0 | grep my_usb

The output will indicate whether or not the symbolic link exists. If not, check that all the values in your rule match exactly to the device. You can check for specifc values by running the following command:

$ lsusb -v | grep [token] --after-context 30

where [token] should be SUBSYSTEM, idVendor, idProduct, or iSerial. The main issue to look out for is what subsystem the device is on. It should be tty. If not, this tutorial will not work.

Tip

Other Useful Commands

$ cat /proc/bus/input/devices
$ sudo dmesg
$ udevadm info -a -p $(udevadm info -q path -n /dev/ttyUSB0)
$ lsusb -v -d [idVendor]:[idProduct] 2>/dev/null | grep iSerial | awk '{ print $3 }'