[Lazarus] How to list available serial ports on Linux?

Bo Berglund bo.berglund at gmail.com
Wed Oct 7 10:26:01 CEST 2020


On Wed, 7 Oct 2020 11:43:50 +1300, Wolf via lazarus
<lazarus at lists.lazarus-ide.org> wrote:

>By submitting your question to DuckDuckGo, I found 
>https://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them
>
>To quote:
>
>|To see which tty's are currently in use, you can simply look into the 
>file /proc/tty/drivers: and get a rather short list of devices. |Does it help?

OK, I have read through your link and also tested a lot on an RPi4 and
a standard i386 Ubuntu server.

The suggested file is no help since it contains a lot of irrelevent
stuff.

This is what I have found that is closest to what I need:

Ubuntu 18 server, where I have one USB serial adapter installed:
---------------------------------------------------------------
$ ls -l /dev/tty* | grep 'dialout'
crw-rw---- 1 root dialout   4, 64 Sep 26 23:45 /dev/ttyS0
crw-rw---- 1 root dialout   4, 65 Sep 26 23:45 /dev/ttyS1
crw-rw---- 1 root dialout   4, 74 Sep 26 23:45 /dev/ttyS10
crw-rw---- 1 root dialout   4, 75 Sep 26 23:45 /dev/ttyS11
crw-rw---- 1 root dialout   4, 76 Sep 26 23:45 /dev/ttyS12
crw-rw---- 1 root dialout   4, 77 Sep 26 23:45 /dev/ttyS13
crw-rw---- 1 root dialout   4, 78 Sep 26 23:45 /dev/ttyS14
crw-rw---- 1 root dialout   4, 79 Sep 26 23:45 /dev/ttyS15
crw-rw---- 1 root dialout   4, 80 Sep 26 23:45 /dev/ttyS16
crw-rw---- 1 root dialout   4, 81 Sep 26 23:45 /dev/ttyS17
crw-rw---- 1 root dialout   4, 82 Sep 26 23:45 /dev/ttyS18
crw-rw---- 1 root dialout   4, 83 Sep 26 23:45 /dev/ttyS19
crw-rw---- 1 root dialout   4, 66 Sep 26 23:45 /dev/ttyS2
crw-rw---- 1 root dialout   4, 84 Sep 26 23:45 /dev/ttyS20
crw-rw---- 1 root dialout   4, 85 Sep 26 23:45 /dev/ttyS21
crw-rw---- 1 root dialout   4, 86 Sep 26 23:45 /dev/ttyS22
crw-rw---- 1 root dialout   4, 87 Sep 26 23:45 /dev/ttyS23
crw-rw---- 1 root dialout   4, 88 Sep 26 23:45 /dev/ttyS24
crw-rw---- 1 root dialout   4, 89 Sep 26 23:45 /dev/ttyS25
crw-rw---- 1 root dialout   4, 90 Sep 26 23:45 /dev/ttyS26
crw-rw---- 1 root dialout   4, 91 Sep 26 23:45 /dev/ttyS27
crw-rw---- 1 root dialout   4, 92 Sep 26 23:45 /dev/ttyS28
crw-rw---- 1 root dialout   4, 93 Sep 26 23:45 /dev/ttyS29
crw-rw---- 1 root dialout   4, 67 Sep 26 23:45 /dev/ttyS3
crw-rw---- 1 root dialout   4, 94 Sep 26 23:45 /dev/ttyS30
crw-rw---- 1 root dialout   4, 95 Sep 26 23:45 /dev/ttyS31
crw-rw---- 1 root dialout   4, 68 Sep 26 23:45 /dev/ttyS4
crw-rw---- 1 root dialout   4, 69 Sep 26 23:45 /dev/ttyS5
crw-rw---- 1 root dialout   4, 70 Sep 26 23:45 /dev/ttyS6
crw-rw---- 1 root dialout   4, 71 Sep 26 23:45 /dev/ttyS7
crw-rw---- 1 root dialout   4, 72 Sep 26 23:45 /dev/ttyS8
crw-rw---- 1 root dialout   4, 73 Sep 26 23:45 /dev/ttyS9
crw-rw---- 1 root dialout 188,  0 Oct  7 09:34 /dev/ttyUSB0

RPi4, where I have 2 USB connected serial devices:
--------------------------------------------------
$ ls -l /dev/tty* | grep 'dialout'
crw-rw---- 1 root dialout 204, 64 Sep 23 17:57 /dev/ttyAMA0
crw-rw---- 1 root dialout 188,  0 Oct  6 12:33 /dev/ttyUSB0
crw-rw---- 1 root dialout 188,  1 Oct  6 15:08 /dev/ttyUSB1

Obviously the Ubuntu situation is a non-starter since it lists way too
many ports than can possibly be accessible. In fact on that computer
ONLY the USB adapter is possible to use, I don't know where the others
might be...

But the RPi4 result is more promising since it lists the 3 ports I
know are there and none else.

But concerning the USB connections there is a different problem too,
the port name depends on in which order the adapter is plugged into
the USB socket, the first plugged adapåter gets ttyUSB0 and the next
ttyUSB1 etc.....

So if you want to select the correct port wired to a specific device
you are in a random situation.

I have had to set up the RPi system such that it creates a symlink to
the plugged in usb-serial adapter based on the ID of the USB socket it
is connected to. This is done by editing the file:
/etc/udev/rules.d/99-usb-serial.rules

By entering rules for the USB sockets as follows I have locked down
the tty name for each socket such that it will be a constant.

SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.1:1.0",SYMLINK+="ttyUSB01"
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.2:1.0",SYMLINK+="ttyUSB02"
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.3:1.0",SYMLINK+="ttyUSB03"
SUBSYSTEM=="tty",ENV{ID_PATH}=="platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.4:1.0",SYMLINK+="ttyUSB04"

The result when listing the USB ports on my RPi4 is:

$ ll /dev/ttyU*
crw-rw---- 1 root dialout 188, 0 Oct  6 12:33 /dev/ttyUSB0
lrwxrwxrwx 1 root root         7 Sep 24 10:09 /dev/ttyUSB01 -> ttyUSB0
lrwxrwxrwx 1 root root         7 Oct  6 12:29 /dev/ttyUSB03 -> ttyUSB1
crw-rw---- 1 root dialout 188, 1 Oct  6 15:08 /dev/ttyUSB1

As you can see here ttyUSB01 is the first plugged in adapter and
ttyUSB03 is the second. Had I reversed the order of plugging them the
base name would have changed but the proper one could be accessed via
the socket based name.

But these symlinks do not show up in the command above since the group
of these symlinks is not set to dialout, it is root...

And to get them into the system one has to edit the rules file...
This in itself is a non-obvious task since one has to find the
hardware ID of each USB port in the system...


-- 
Bo Berglund
Developer in Sweden



More information about the lazarus mailing list