Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify multiple USB-serial adapters under Ubuntu 10.1

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.

like image 213
user589888 Avatar asked Jan 25 '11 23:01

user589888


3 Answers

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.

like image 200
Ilya Matveychikov Avatar answered Oct 24 '22 04:10

Ilya Matveychikov


Much like Ilya Matvejchikov's answer, a good solution is to add udev rules to do what you want with the device. Like you, I was having a similar problem. I had a UPS on a USB-to-multi-serial adapter and occasionally the system would switch around the /dev/tty numbers.

My solution was to create a rule to match the type of device by driver and port, then create a symbolic link to the port to which my UPS was attached. I used NUT to monitor the UPS, which was always plugged into the same physical port.

# File contents of /etc/udev/rules.d/75-nut-ups.rules
# Create /dev/nut-ups0 to use as a persistent serial device that can be used
# reliably by nut to communicate with a UPS attached to the system.
# The UPS is attached to the first port of a MosSemiconductor dual USB
# serial adapter.
KERNELS=="ttyUSB*", SUBSYSTEMS=="usb-serial", DRIVERS=="moschip7720", ATTRS{port_number}=="0", SYMLINK+="nut-ups0"

Now I configure NUT to always use a constant /dev/nut-ups0, as the serial port and the rule takes care of mapping properly when the usb-serial device is recognized.

You can use the lsusb command to find out the actual device name to use in the rule when it's plugged in.

like image 27
Suncat2000 Avatar answered Oct 24 '22 06:10

Suncat2000


Look with $ udevadm info -n /dev/ttyUSB0 -a which port your USB device is plugged in. The variable KERNELS of one of the parent devices should be something like KERNELS=="1-1.2:1.0".

Create a udev rule:

SUBSYSTEM=="tty", KERNELS=="1-1.2:1.0", SYMLINK+="ttyUSB42"
SUBSYSTEM=="tty", KERNELS=="1-1.3:1.0", SYMLINK+="usb-serial"

and trigger udev

$ udevadm trigger
like image 2
mickeyrourkeske Avatar answered Oct 24 '22 05:10

mickeyrourkeske