How to Know Which Device Is Connected in Which /Dev/Ttyusb Port

How to know which device is connected in which /dev/ttyUSB port

You can get this information from the sys filesystem. It is easy to check from the shell, and then do a program that does the same:

  1. cd /sys/devices
  2. Find the directory of the first of your ports: find -name "ttyUSB0". It will probably find them in something like ./pci0000:00/0000:00:1d.0/usb2/2-2/2-2.1/2-2.1:1.0/...
  3. The pci* part is the USB controller. The interesting bit is the 2-2.1 which is the USB device. In that directory there are a lot of files that identify your device:

    • serial: The serial number. Probably what you want.
    • idVendor and idProduct: The USB identifier of the device.

An easy alternatively to steps 1 and 2 is:

  1. cd /sys/class/tty/
  2. readlink ttyUSBn will give you the full path of the device directory.

As a footnote, note that some parts of the sysfs are considered API stable and some parts are not. For more information see the official sysfs rules.

Identify which USB device is /dev/ttyUSB0

Since you mentioned that you want to do it from Python, pyudev has the following example code to access everything udev knows about a device identified by a device file:

from pyudev import Context, Device
context = Context()
device = Devices.from_device_file(context, '/dev/sda')

I believe that should work very nicely with /dev/ttyUSB0 as well.

See https://pyudev.readthedocs.io/en/latest/api/pyudev.html#pyudev.Devices.from_device_file

Once you have the device udev instance in Python, you can access device.attributes and device.properties to get a wealth of information including VID, PID, string descriptors, and so on. The documentation says that

all well-known dictionary methods and operators (e.g. .keys(), .items(), in) are available to access device properties.

How to find all serial devices (ttyS, ttyUSB, ..) on Linux without opening them?

The /sys filesystem should contain plenty information for your quest. My system (2.6.32-40-generic #87-Ubuntu) suggests:

/sys/class/tty

Which gives you descriptions of all TTY devices known to the system. A trimmed down example:

# ll /sys/class/tty/ttyUSB*
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.4/2-1.4:1.0/ttyUSB0/tty/ttyUSB0/
lrwxrwxrwx 1 root root 0 2012-03-28 20:44 /sys/class/tty/ttyUSB1 -> ../../devices/pci0000:00/0000:00:1d.0/usb2/2-1/2-1.3/2-1.3:1.0/ttyUSB1/tty/ttyUSB1/

Following one of these links:

# ll /sys/class/tty/ttyUSB0/
insgesamt 0
drwxr-xr-x 3 root root 0 2012-03-28 20:43 ./
drwxr-xr-x 3 root root 0 2012-03-28 20:43 ../
-r--r--r-- 1 root root 4096 2012-03-28 20:49 dev
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 device -> ../../../ttyUSB0/
drwxr-xr-x 2 root root 0 2012-03-28 20:49 power/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 subsystem -> ../../../../../../../../../../class/tty/
-rw-r--r-- 1 root root 4096 2012-03-28 20:43 uevent

Here the dev file contains this information:

# cat /sys/class/tty/ttyUSB0/dev
188:0

This is the major/minor node. These can be searched in the /dev directory to get user-friendly names:

# ll -R /dev |grep "188, *0"
crw-rw---- 1 root dialout 188, 0 2012-03-28 20:44 ttyUSB0

The /sys/class/tty dir contains all TTY devices but you might want to exclude those pesky virtual terminals and pseudo terminals. I suggest you examine only those which have a device/driver entry:

# ll /sys/class/tty/*/device/driver
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS0/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS1/device/driver -> ../../../bus/pnp/drivers/serial/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS2/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 19:07 /sys/class/tty/ttyS3/device/driver -> ../../../bus/platform/drivers/serial8250/
lrwxrwxrwx 1 root root 0 2012-03-28 20:43 /sys/class/tty/ttyUSB0/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/
lrwxrwxrwx 1 root root 0 2012-03-28 21:15 /sys/class/tty/ttyUSB1/device/driver -> ../../../../../../../../bus/usb-serial/drivers/ftdi_sio/

How to find out which USB-RS232 device is on which tty?

Find more info using sysfs:

$ ls /sys/bus/usb-serial/devices/ -ltrah

lrwxrwxrwx 1 root root 0 2012-02-07 22:17 ttyUSB0 -> ../../../devices/pci0000:00/0000:00:0f.4/usb2/2-2/2-2:1.0/ttyUSB0

$ ls -ltrad /sys//devices/pci0000:00/0000:00:0f.4/usb2/2-2/2-2:1.0/ttyUSB0
drwxr-xr-x 4 root root 0 2012-02-07 22:17 /sys//devices/pci0000:00/0000:00:0f.4/usb2/2-2/2-2:1.0/ttyUSB0

$ ls -ltrad /dev/ttyUSB0
crw-rw---- 1 root dialout 188, 0 2012-02-01 00:17 /dev/ttyUSB0

Of course, the linked devices/... node contains a lot of information

Adding information based on the OP's comment:

The device number keeps growing if devices are removed/inserted. lsusb -t can be used to correlate the device numbers with usb bus/port.

Then, 'lsusb -d devID' can determine what device is on which port.
Finally 'ls /sys/bus/usb-serial/devices/ -ltrah' will list (by bus/port) where it was mounted.

Not very convenient, but it 'works'

How can I figure out which tty file points to which USB-to-Serial device?

Check this Using Linux USB page.

/proc/bus/usb/devices lists information about the devices currently attached to the USB bus. This is very useful when trying to figure out if the device is correctly enumerated.

How to write to `/dev/ttyUSB?` in kernel space?

SappyInsane on linuxquestions had the same problem and gave me his solution which worked
https://www.linuxquestions.org/questions/linux-kernel-70/reading-from-arduino-serial-in-a-kernel-module-4175704822/

How to find /dev/ name of USB Device for Serial Reading on Mac OS?

So, I actually found the answer.

To find out what the device name is, I did an ls of the /dev/ directory with the device plugged in and then with it disconnected.

ls -lha /dev/tty* > plugged.txt
ls -lha /dev/tty* > np.txt

Then I compared the files using

vimdiff plugged.txt np.txt

And saw the line

crw-rw-rw- 1 root wheel 19, 30 Jan 16 15:24 /dev/tty.usbmodem145222

Sure enough, the device is named tty.usbmodem145222!



Related Topics



Leave a reply



Submit