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.
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/
Listing USB Ports on a PC
Thanks for your help! The following command gave me a list of ports in a format that will work in my Node.js code:
chgport
how to read multiple serial ports in realtime in C or Python
In my opinion what you're trying to do, which is if I understood correctly a kind of port sniffer to identify the transactions exchanged on a serial link is not feasible with USB-to-serial converters and a conventional OS, unless you're running at slow baudrates.
The USB port will always introduce a certain latency (probably tens of milliseconds), and you'll have to put the unpredictability of the OS on top of that.
Since you have two ports you could try to run two separate threads and timestamp each chunk of data received. That might help improve things but I'm not sure it will allow you to clearly follow the sequence.
If you had real (legacy) serial ports, and a not very loaded OS maybe you could do it somehow.
But if what you want is a serial port sniffer on the cheap you can try something like this solution. If you do forwarding on your ports you'll know what is coming from where at all times. Of course, you need to have access to either side of the communication.
If you don't have that luxury, I guess it would be quite easy to get what you want with almost any kind of microcontroller.
EDIT: Another idea could be to use a dual serial port to USB converter. Since both ports are served by the same chip, somehow I think it's likely that you can follow the sequence with one of those. I have access to this one if you post a full working snippet of your code I can test it next week, if you're curious to know.
Related Topics
Different File Owner Inside Docker Container and in Host MAChine
Find and Remove a Line in Multiple Files
Bash Script to Calculate Time Elapsed
List Files Over a Specific Size in Current Directory and All Subdirectories
Linux: Set Permission Only to Directories
How to Check If a Process Is in Hang State (Linux)
Iproute2 Commands for Mpls Configuration
Why Would It Be Impossible to Fully Statically Link an Application
Error: "Grep: Argument List Too Long"
How to Append to a File Using X86-64 Linux System Calls
How to Filter the Running Nodes
How to Move All Files Including Hidden Files into Parent Directory via *
I Want to Contribute to the Linux Kernel
How to Use Linux Command Sort to Sort the Text File According to 4Th Column, Numeric Order