Tuesday, February 21, 2017

Fixed names for usb devices in linux

usb devices appear as /dev/ttyUSB<somenumber> devices. When you plug it to another USB port <somenumber> is unpredictable and you always wasting time to figure out what is the right port number for your device.
To fix this, you can create "udev" rules, so every time you plug in your device there will be the same sym link created for that particular device.

Check vendor and product id with lsusb command:

~# lsusb
Bus 001 Device 011: ID 0403:6001 FTDI FT232 USB-Serial (UART) IC 
Bus 001 Device 010: ID 0403:6001 FTDI FT232 USB-Serial (UART) IC 

ok, we have 2 FTDI chips and we can't distinguish which chip is from what device.
Let's check device serial number(we know that it is connected to ttyUSB0):

~# udevadm info -a -n /dev/ttyUSB0 | grep '{serial}'
 ATTRS{serial}=="0000:00:14.0"

Then go to /etc/udev/rules.d and create file 99-usb-serial.rules
~#sudo touch 99-usb-serial.rules

open file for edit and add these line:

SUBSYSTEM=="tty", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="0000:00:14.0", SYMLINK+="arduino"

Now everytime when you plug in your board it will be accesible via symlink /dev/arduino

~# ls -l /dev/arduino 
lrwxrwxrwx 1 root root 7 Nov 15 20:11 /dev/arduino -> ttyUSB0

~# ls -l /dev/ttyUSB0
crw-rw---- 1 root uucp 188, 0 Nov 15 20:11 /dev/ttyUSB1