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/
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 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 to know which ttyS* file is linked to which serial port?
I'm not absolutely sure what is being asked here, but the way /dev/ttyS* are mapped has not changed in ages and the first serial port, sometimes called with DOS name COM1, is still accessible as /dev/ttyS0
, second being /dev/ttyS1
and so on.
From kernel documentation, namely from file Documentation/devices.txt
you can still find some useful information:
4 char TTY devices
0 = /dev/tty0 Current virtual console
1 = /dev/tty1 First virtual console
...
63 = /dev/tty63 63rd virtual console
64 = /dev/ttyS0 First UART serial port
...
255 = /dev/ttyS191 192nd UART serial port
UART serial ports refer to 8250/16450/16550 series devices.
If your question how to find all serial ports on your system, see /dev/serial
which should have (unless you're using a kernel really ancient) entries by-id and by-uuid.
How do I know which serial port to use on Linux?
The command dmesg
will show you the kernel message when the module is plugged in which will give it's device name.
The /proc
file system is like the device manager on windows - somewhere in there will be a list of tty devices
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:
cd /sys/devices
- 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/...
The
pci*
part is the USB controller. The interesting bit is the2-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
andidProduct
: The USB identifier of the device.
An easy alternatively to steps 1 and 2 is:
cd /sys/class/tty/
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.
Related Topics
How to Print the Ld(Linker) Search Path
Use Sudo With Password as Parameter
Automating Running Command on Linux from Windows Using Putty
Why Do X86-64 Linux System Calls Modify Rcx, and What Does the Value Mean
Tool to Trace Local Function Calls in Linux
Shell Command to Tar Directory Excluding Certain Files/Folders
How to Quickly Sum All Numbers in a File
How to Get Docker Linux Container Information from Within the Container Itself
How to Generate New Variable Names on the Fly in a Shell Script
Why Do You Need to Put #!/Bin/Bash At the Beginning of a Script File
Setting Up Ftp on Amazon Cloud Server
How to Find the Last Field Using 'Cut'
Allowed Characters in Linux Environment Variable Names
Static Link of Shared Library Function in Gcc
How to Prompt For Yes/No/Cancel Input in a Linux Shell Script
Linux Command to Print Directory Structure in the Form of a Tree