How to identify multiple USB-serial adapters under Ubuntu 10.1

user589888 picture user589888 · Jan 26, 2011 · Viewed 23.2k times · Source

I am reading data from multiple identical USB-serial adapters under Ubuntu 10.1.

On occasion, their /dev/tty path changes (eg if other USB devices are connected on startup).

I need a way of repeatedly referring to the same adapter through any such changes.

The devices all have the same serial numbers, according to udevadm.

I think the most likely option is to identify an adapter by which port it is connected to (they don't get moved around).

I can find all sorts of interesting /dev paths that might work, but despite all the discussion about udev online, I can't locate a definitive statement about whether some of these paths are static if the device is plugged into a static port.

Answer

Ilya Matveychikov picture Ilya Matveychikov · May 11, 2011

There is a solution. It's better late then never ;)

Use the following udev rule to map /dev/ttyUSB{?} devices into the /dev/usb-ports/%bus_id-%port_id link.

Here is my /etc/udev/rules.d/usb-parse-devpath.rules:

ACTION=="add", KERNEL=="ttyUSB[0-9]*", PROGRAM="/etc/udev/rules.d/usb-parse-devpath.pm %p", SYMLINK+="usb-ports/%c"

And the usb-parse-devpath.pm script:

#!/usr/bin/perl -w

@items = split("/", $ARGV[0]);
for ($i = 0; $i < @items; $i++) {
    if ($items[$i] =~ m/^usb[0-9]+$/) {
        print $items[$i + 1] . "\n";
        last;
    }
}

As you can see this helps us to create named links to /dev/ttyUSB{?} devices and place them at /dev/usb-ports in the following format: bus_id-port_id.

For example, the next command gives me the following:

$ udevadm info --query=path --name=/dev/ttyUSB0
/devices/pci0000:00/0000:00:1d.1/usb3/3-1/3-1:1.0/ttyUSB0/tty/ttyUSB0

So, the bus_id is 3 and port_id is 1 and now I have following in my /dev/usb-ports:

$ ls -al /dev/usb-ports
lrwxrwxrwx  1 root root   10 Май 12 00:26 3-1 -> ../ttyUSB0

Regards.