Usb serial devices, make symlinks so we dont need to know device.

I had a little problem, I always have a TTY Usb serial adapter plugged in. I use this to program and debug avr micro-controllers. Lately I have started using NodeMCU boards. If I leave these plugged in when I shutdown my computer and boot it back up the usb devices are often swapped.

To solve this, I created some udev rules.

Plug in each device and check the dmesg output for the usib device information for “idVendor”, “idProduct”, For a nodemcu it will be like this (probably identical).

[ 3299.113934] usb 1-7: new full-speed USB device number 5 using xhci_hcd
[ 3299.364185] usb 1-7: New USB device found, idVendor=1a86, idProduct=7523
[ 3299.364189] usb 1-7: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[ 3299.364192] usb 1-7: Product: USB2.0-Serial
[ 3299.368112] ch341 1-7:1.0: ch341-uart converter detected
[ 3299.379429] usb 1-7: ch341-uart converter now attached to ttyUSB0

For the device I always leave plugged in, I also need the serial number. This is also an alternate way of getting the above information.

>udevadm info --name=/dev/ttyUSB1 --attribute-walk | grep idProduct | head -n 1 -
ATTRS{idProduct}=="2303"
>udevadm info --name=/dev/ttyUSB1 --attribute-walk | grep idVendor | head -n 1 -
ATTRS{idVendor}=="067b"
>udevadm info --name=/dev/ttyUSB1 --attribute-walk | grep serial
SUBSYSTEMS=="usb-serial"
ATTRS{serial}=="0000:09:00.3"

Now create a file in “/etc/udev/rules.d” call it “99-usb-serial.rules”

Add lines for each usb device, this is my file, I have highlighted where each value comes from.

SUBSYSTEM=="tty", ATTRS{idVendor}=="067b", ATTRS{idProduct}=="2303", ATTRS{serial}=="0000:09:00.3", SYMLINK+="usbserial"
SUBSYSTEM=="tty", ATTRS{idVendor}=="1a86", ATTRS{idProduct}=="7523", SYMLINK+="nodemcu"

Now, when the usb devices are connected my adapter that is always plugged in has a symlink created called “/dev/usbserial” which will point to the tty device, and any node mcu that I plugin gets the symlink “/dev/nodemcu” which points to its serial device. The symlinks can be used instead of the /dev/ttyUSB* devices without having to know which device it is.

Leave a Reply

Your email address will not be published. Required fields are marked *