Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify which USB device is /dev/ttyUSB0

I'm working with a Linux distribution (Raspbian) and I have two USB-Serial adapters which I'm connecting to the microcontroller. Whenever I connect both adapters, the serial ports "/dev/ttyUSB0" and "/dev/ttyUSB1" appear; here "/dev/ttyUSB0" is the adapter that was physically connected first (identified first by the system).

Now, I'm looking for a way to figure out which adapter is which in the event of a microcontroller restart. I.e., both adapters remained plugged into their USB ports and I can't physically unplug/replug to force which adapter is "USB0".

Basic research led me to these commands (pictures for reference):

ls /dev/ttyUSB* To list out the USB-serial ports that are active.

Enter image description here

lsusb To get more information about the USB buses and connected devices.

Enter image description here

Is there a way to relate these two results (or an alternative) to figure out what I need? For instance, in the pictures above "/dev/ttyUSB0" is "Bus 001 Device 008: ID 1a86:...", but how can I find that out through software (preferably using Python, but a shell script could work too)?

like image 650
DanMicroS Avatar asked Jul 16 '20 22:07

DanMicroS


People also ask

Which USB port is ttyUSB0?

ttyUSB means "USB serial port adapter" and the "0" (or "1" or whatever) is the device number. ttyUSB0 is the first one found, ttyUSB1 is the second etc. (Note that if you have two similar devices, then the ports that they are plugged into may affected the order they are detected in, and so the names).

How do I know my USB developer name?

To find out the device name assigned to a USB drive or USB stick, you can use lsblk command, which shows information about all available block devices. The lsblk command gathers information about existing block devices from sysfs filesystem, and show them in a tree-like format.


1 Answers

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.

like image 189
Ben Voigt Avatar answered Sep 18 '22 09:09

Ben Voigt