Linux – differentiate between identical USB devices on Linux

linuxudevusb

I'm using two identical USB to serial adapter on my Linux system. So depends on who's inserted first, one of them will be represented with name /dev/ttyUSB0 and the other with /dev/ttyUSB1. Is there a trick I can make the name persistent? I have three USB port. Is it possible to link the name to the physical port?

Thanks,

Best Answer

If you run udevadm monitor --properties you should see the various properties you can use to create rules from. The ID_PATH property indicates the port the device was plugged into. Insert the serial to usb device to see what udev sees.

So it should be straightforward to create a udev rule that creates a symlink when the device is plugged in, something like:

SUBSYSTEM=="usb_device", SYSFS{idVendor}=="xxxx", SYSFS{idProduct}=="xxxx", ENV{ID_PATH}=="pci....usb-0:1:0",SYMLINK+="ttyUSBport0"
SUBSYSTEM=="usb_device", SYSFS{idVendor}=="xxxx", SYSFS{idProduct}=="xxxx", ENV{ID_PATH}=="pci....usb-0:1:1",SYMLINK+="ttyUSBport1"

This is done from memory so you'll want to confirm the syntax. Change the idvendor and idproduct to the USB VID and PID, and change the ID_PATH to whatever you found in udevadm monitor.

This should leave the normal /dev/ttyUSB0 device there, but also create a symlink to it depending on which port it was plugged into. If you use the symlink in minicom or whatever you are using, you'll always get the right adapter based on the port you plug it into.