Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 13:13 11 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : (revised) test for linux serial ports

Author Message
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 12:59pm 12 Dec 2024
Copy link to clipboard 
Print this post

looks like with the release of kernel 6.8 the naming convention for fixed serial port driver has changed. this will cause problems for any program that makes use of the driver name being "serial8250" to determine if a port is fixed or removable. the symptom you see is a list of all 32 "/dev/ttySxx" being detected as live ports.

i found this out after moving from 21.3 to Linux Mint 22, which will only run with a 6.8 or later kernel  

attached is a test program that (maybe?) gets around this by (1) accepting a driver path starting with "/sys/bus/serial-base/" as indication that a port is fixed, (2) then making two different tests to see if there is hardware present - first attempting to read the pseudo-file /sys/class/tty/DeviceName/type, and if that fails falling back to using ioctl TIOCGSERIAL to get the port 'type'.

attached is a 64-bit LINUX CONSOLE test program to list the ports using the above 'revised' method. FPC source is included. i'd very much appreciate it if folks with 64-bit linux systems (both the pre-6.8 and later kernels) could try it out and post back to this thread the output generated. note: the test program should produce correct output for both old and new kernels.

ports (inc source).zip


cheers,
rob   :-)
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 10:00pm 12 Dec 2024
Copy link to clipboard 
Print this post

A quick test with kernel 6.6.43

Doesn't list any ports at all, but I'm not aware whether this laptop has any or what kind ./ports would find / look for.

Only has /dev/ttySx (x in 0..7)

John
 
TassyJim

Guru

Joined: 07/08/2011
Location: Australia
Posts: 6269
Posted: 01:04am 13 Dec 2024
Copy link to clipboard 
Print this post

Looks correct on Linux Mint running in a VM



One Picomite and one CH340

Jim
Edited 2024-12-13 11:06 by TassyJim
VK7JH
MMedit
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5056
Posted: 07:38am 13 Dec 2024
Copy link to clipboard 
Print this post

Robert,

I could try to to a test on Ubuntu 24.04 as live distro ? (not installed) ? Not sure if that will work. Connected FTDI RS232 cable, arduino UNO, PicoMite

Ubuntu 20.04


Ubuntu 24.04 live


Volhout
Edited 2024-12-13 18:32 by Volhout
PicomiteVGA PETSCII ROBOTS
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 01:43pm 13 Dec 2024
Copy link to clipboard 
Print this post

Jim: it is interesting that ttyS0 showed up as removable, i am guessing this is bacause of running in a VM. did you do anything special to make the COM1: serial visible to the VM?

John: if the machine has no serial present, then there should be no output. although, some machines do seem to have a single serial port present 'on the motherboard', even if it is not accessible to the outside world.

Volhout: i've never tried with a live distro [... 2 hours later] just tried it and got entirely confusing results! you're using an Dell Optiplex 3020? does it have any serial ports on the motherboard and/or is there a DB9 on the back of the machine? is this enabled in the BIOS?

it is the ttySxx ports that i am particularly interested in, which in theory should be fixed (non-USB) serial ports. here is the output from my own computer as an example:

user@user-DH61BE:~/shared/Serial Watcher, console, revised$ ./ports
test for revised serial port finder method, 13-dec-2024
ttyS0 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyS4 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyS5 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyACM0 is a removable device
ttyUSB0 is a removable device
ttyUSB1 is a removable device

>>> Linux user-DH61BE 6.8.0-50-generic
user@user-DH61BE:~/shared/Serial Watcher, console, revised$



cheers,
rob   :-)
 
Volhout
Guru

Joined: 05/03/2018
Location: Netherlands
Posts: 5056
Posted: 01:54pm 13 Dec 2024
Copy link to clipboard 
Print this post

Hi Rob,

The Dell is not equipped with DB-9 or any fixed serial port. I could try the live distro on another machine that has a fixed serial port. But this one does not have one.

Volhout
PicomiteVGA PETSCII ROBOTS
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 03:20pm 13 Dec 2024
Copy link to clipboard 
Print this post

Volhout: probably not worth doing much more testing with live distros, i'm feeling inclined to think my whole approach is flawed! i've just sent an email off to Soren Holm, the fellow who, in 2012, posted about the method i've been using to detect ports:
https://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them
(his posting is about 1/3 of the way down the page)

hopefully he is still about, and willing reply. clearly in kernel 6.8 some fairly major reorganizing of the serial code has been carried out, but to date i've been unable to find any online discussion on it - or indeed any other useful details.

btw, you can in fact use dmesg | grep "tty" to build a list of serial ports (removing any that are not in /dev/tty* to get rid of ones that have been subsequently unplugged), but the message buffer that dmesg reads from is of limited length and wraps around at some undefined point.


cheers,
rob   :-)
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 03:24pm 13 Dec 2024
Copy link to clipboard 
Print this post

  robert.rozee said  John: if the machine has no serial present, then there should be no output. although, some machines do seem to have a single serial port present 'on the motherboard', even if it is not accessible to the outside world.

That's what I thought.

user@user-DH61BE:~/shared/Serial Watcher, console, revised$ ./ports
test for revised serial port finder method, 13-dec-2024
ttyS0 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyS4 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyS5 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)

That's more like what I expected.

I thought even if not accessible they all had such so am surprised by mine.

dmesg has:
Serial: 8250/16550 driver, 8 ports, IRQ sharing enabled

(and as mentioned I have ttyS0-7)

On an older system, kernel 5.6.14:
test for revised serial port finder method, 13-dec-2024

>>>  4 errors reading /type
>>>  0 errors reading ioctl

so still no apparent serial ports, accessible or not.

dmesg has:
Serial: 8250/16550 driver, 4 ports, IRQ sharing enabled

John
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 03:58pm 13 Dec 2024
Copy link to clipboard 
Print this post

just checked for something similar on mine:

user@user-DH61BE:~$ dmesg | grep "8250"
[    0.233607] Serial: 8250/16550 driver, 32 ports, IRQ sharing enabled
user@user-DH61BE:~$


i am absolutely certain i don't have 32 serial ports here! my guess is that the kernel can accommodate up to 32 ports (or in your case 8, or 4 on the older kernel 5.6.14 system). if you do ls /dev/ttyS* do you see the corresponding number (8 or 4) on your systems?

is nearly 5am here, so i shall think more about this while i sleep!


cheers,
rob   :-)
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 05:55pm 13 Dec 2024
Copy link to clipboard 
Print this post

  robert.rozee said  if you do ls /dev/ttyS* do you see the corresponding number (8 or 4) on your systems?

Yes.

I'm not going to try this but I wonder if it would be possible to solder leads onto the mobo and get one or more serial ports...

John
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 02:31pm 24 Dec 2024
Copy link to clipboard 
Print this post

here is a further evolved test version:

ports2 (inc source).zip

again, this is a 64-bit LINUX CONSOLE program with no versioned GLIBC symbols - so it should run on pretty much anything.


this time the checking method employed is to:

- search through /dev/class/tty/* to get our initial list of potential serial ports. this means we should now also pick up any rfcomm* bluetooth devices.

- perform an 'access' syscall on the corresponding /dev entry to check that a) the entry exists there, and b) we have the necessary permissions to access it as a serial device - one result of this is that if we are not a member of dialout then no ports are returned no matter what - i see this as a good thing.

- make sure the driver path /sys/class/tty/DeviceName/device/driver exists. we no longer need to do any expansion of symlinks or checking of the driver's name.

- check for the existence of the pseudo-file /sys/class/tty/DeviceName/type to distinguish between fixed (.../type file exists) and removable (no .../type file found) serial devices. note: we do not need to actually read the .../type file.

- if .../type exists then we further perform an ioctl/TCGETS call - if the call fails we know the fixed port does not exist.


it would be very much appreciated if folks could check that sensible output is produced, preferably on machines that have one or two fixed serial ports. i'd also be really interested in hearing back if bluetooth serial ports (rfcomm*) are successfully detected.

Jim: the source code is now much easier to follow - if anyone has complained about mmedit finding phantom ttyS* ports, or missing rfcomm* ports, then it it may be worth looking at incorporating this detection method into mmedit.


cheers,
rob   :-)
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 05:56pm 26 Dec 2024
Copy link to clipboard 
Print this post

Nothing plugged in:
2nd revision, serial port finder, 25-dec-2024
no serial ports found


With something:
2nd revision, serial port finder, 25-dec-2024
 ttyACM0


Couldn't try rfcomm stuff.

John
 
tgerbic
Regular Member

Joined: 25/07/2019
Location: United States
Posts: 65
Posted: 08:32am 06 Jan 2025
Copy link to clipboard 
Print this post

Can confirm it works on Fedora 40. Just stick it anywhere and type ./ports2

2nd revision, serial port finder, 25-dec-2024
* ttyS4
* ttyS0
* ttyS5
(3 fixed devices)
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 09:17am 06 Jan 2025
Copy link to clipboard 
Print this post

I'm a bit surprised it doesn't find the ports on various of my computers.

There are (for x = 0,1,2,3)
/dev/ttySx
/sys/class/tty/ttySx/ which are links to
/sys/devices/platform/serial8250/tty/ttySx/
in which are files such as type
(cat type is just a 0)

I don't know what tty-related chips are on each motherboard.

John
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 10:40am 06 Jan 2025
Copy link to clipboard 
Print this post

  JohnS said  I'm a bit surprised it doesn't find the ports on various of my computers.

There are (for x = 0,1,2,3)
/dev/ttySx
/sys/class/tty/ttySx/ which are links to
/sys/devices/platform/serial8250/tty/ttySx/
in which are files such as type
(cat type is just a 0)

I don't know what tty-related chips are on each motherboard.

John


it will only find ports that are associated with actual hardware - i would be surprised if a motherboard had four onboard serial ports without there being any visible DB9 sockets on the back, or any 2x5 pin headers internally.

the Linux kernel creates the ttySxx entries in /dev, /sys/class/tty and /sys/devices/platform/serial8250/tty without doing any checking that there is real serial port hardware present.

the actual number of ttySxx entries created is hard-coded in the kernel. 32 is a common number, as is 16 and, it would seem 4. the actually number is dependent on whims of the person compiling the kernel!

for instance, on a VM (Linux Mint 19.3 i686, kernel 5.4.0-89-generic) i have here that has no serial ports available to it, one finds:

user@user-VirtualBox:~$ ls /dev/ttyS*
/dev/ttyS0   /dev/ttyS14  /dev/ttyS2   /dev/ttyS25  /dev/ttyS30  /dev/ttyS8
/dev/ttyS1   /dev/ttyS15  /dev/ttyS20  /dev/ttyS26  /dev/ttyS31  /dev/ttyS9
/dev/ttyS10  /dev/ttyS16  /dev/ttyS21  /dev/ttyS27  /dev/ttyS4
/dev/ttyS11  /dev/ttyS17  /dev/ttyS22  /dev/ttyS28  /dev/ttyS5
/dev/ttyS12  /dev/ttyS18  /dev/ttyS23  /dev/ttyS29  /dev/ttyS6
/dev/ttyS13  /dev/ttyS19  /dev/ttyS24  /dev/ttyS3   /dev/ttyS7
user@user-VirtualBox:~$ ls /sys/devices/platform/serial8250/tty
ttyS0   ttyS12  ttyS16  ttyS2   ttyS23  ttyS27  ttyS30  ttyS6
ttyS1   ttyS13  ttyS17  ttyS20  ttyS24  ttyS28  ttyS31  ttyS7
ttyS10  ttyS14  ttyS18  ttyS21  ttyS25  ttyS29  ttyS4   ttyS8
ttyS11  ttyS15  ttyS19  ttyS22  ttyS26  ttyS3   ttyS5   ttyS9
user@user-VirtualBox:~$ ls -d /sys/class/tty/ttyS*
/sys/class/tty/ttyS0   /sys/class/tty/ttyS19  /sys/class/tty/ttyS29
/sys/class/tty/ttyS1   /sys/class/tty/ttyS2   /sys/class/tty/ttyS3
/sys/class/tty/ttyS10  /sys/class/tty/ttyS20  /sys/class/tty/ttyS30
/sys/class/tty/ttyS11  /sys/class/tty/ttyS21  /sys/class/tty/ttyS31
/sys/class/tty/ttyS12  /sys/class/tty/ttyS22  /sys/class/tty/ttyS4
/sys/class/tty/ttyS13  /sys/class/tty/ttyS23  /sys/class/tty/ttyS5
/sys/class/tty/ttyS14  /sys/class/tty/ttyS24  /sys/class/tty/ttyS6
/sys/class/tty/ttyS15  /sys/class/tty/ttyS25  /sys/class/tty/ttyS7
/sys/class/tty/ttyS16  /sys/class/tty/ttyS26  /sys/class/tty/ttyS8
/sys/class/tty/ttyS17  /sys/class/tty/ttyS27  /sys/class/tty/ttyS9
/sys/class/tty/ttyS18  /sys/class/tty/ttyS28
user@user-VirtualBox:~$


while on a real machine (Linux Mint 22 x86_64, kernel 6.8.0-51-generic) that has ttyS0, ttyS4, ttyS5 present:

user@user-DH61BE:~$ ls /dev/ttyS*
/dev/ttyS0   /dev/ttyS14  /dev/ttyS2   /dev/ttyS25  /dev/ttyS30  /dev/ttyS8
/dev/ttyS1   /dev/ttyS15  /dev/ttyS20  /dev/ttyS26  /dev/ttyS31  /dev/ttyS9
/dev/ttyS10  /dev/ttyS16  /dev/ttyS21  /dev/ttyS27  /dev/ttyS4
/dev/ttyS11  /dev/ttyS17  /dev/ttyS22  /dev/ttyS28  /dev/ttyS5
/dev/ttyS12  /dev/ttyS18  /dev/ttyS23  /dev/ttyS29  /dev/ttyS6
/dev/ttyS13  /dev/ttyS19  /dev/ttyS24  /dev/ttyS3   /dev/ttyS7
user@user-DH61BE:~$ ls /sys/devices/platform/serial8250/tty
ls: cannot access '/sys/devices/platform/serial8250/tty': No such file or directory
user@user-DH61BE:~$ ls -d /sys/class/tty/ttyS*
/sys/class/tty/ttyS0   /sys/class/tty/ttyS19  /sys/class/tty/ttyS29
/sys/class/tty/ttyS1   /sys/class/tty/ttyS2   /sys/class/tty/ttyS3
/sys/class/tty/ttyS10  /sys/class/tty/ttyS20  /sys/class/tty/ttyS30
/sys/class/tty/ttyS11  /sys/class/tty/ttyS21  /sys/class/tty/ttyS31
/sys/class/tty/ttyS12  /sys/class/tty/ttyS22  /sys/class/tty/ttyS4
/sys/class/tty/ttyS13  /sys/class/tty/ttyS23  /sys/class/tty/ttyS5
/sys/class/tty/ttyS14  /sys/class/tty/ttyS24  /sys/class/tty/ttyS6
/sys/class/tty/ttyS15  /sys/class/tty/ttyS25  /sys/class/tty/ttyS7
/sys/class/tty/ttyS16  /sys/class/tty/ttyS26  /sys/class/tty/ttyS8
/sys/class/tty/ttyS17  /sys/class/tty/ttyS27  /sys/class/tty/ttyS9
/sys/class/tty/ttyS18  /sys/class/tty/ttyS28
user@user-DH61BE:~$


from the above you can see that in both cases there are 32 ttySxx entries, and also that with kernel 6.8 there is no /sys/devices/platform/serial8250/tty directory - this is because the 6.8 kernel uses a different /sys/devices/ directory layout and file naming convention to earlier kernels.


cheers,
rob   :-)
 
tgerbic
Regular Member

Joined: 25/07/2019
Location: United States
Posts: 65
Posted: 06:31am 07 Jan 2025
Copy link to clipboard 
Print this post

Thought I would do one other test and attached three USB to serial adapters to my Fedora 40 system and see what it showed.

 ttyUSB1
* ttyS4
* ttyS0
 ttyUSB2
* ttyS5
 ttyUSB0
(3 fixed devices)

What I have is a motherboard with one real serial port (ttyS0), a two serial port PCIE card (ttyS4 and ttyS5) and three USB to serial converters.


I also tried the older ports utility and got this:

test for revised serial port finder method, 13-dec-2024
ttyS0 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyS4 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyS5 is type 16550A UART    /sys/bus/serial-base/drivers/port    (/type)
ttyUSB1 is a removable device
ttyUSB0 is a removable device
ttyUSB2 is a removable device

>>> Linux orac 6.12.7-100.fc40.x86_64

I like the organized layout on the older ports utility better.
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2436
Posted: 02:48pm 08 Jan 2025
Copy link to clipboard 
Print this post

  tgerbic said  Thought I would do one other test and attached three USB to serial adapters to my Fedora 40 system and see what it showed.
[...]
I like the organized layout on the older ports utility better.


your output looks good - it found everything correctly!

both versions posted earlier are just rough hacks to check the detection method, attached below is something more polished:

Serial Watcher, Linux 64-bit, 9-jan-2025.zip

this is a GUI application, Linux 64-bit. Lazarus/FPC source included. when run you should see a small window containing a list of ports that can be scrolled up and down through with either mouse wheel or up/down arrow keys. as devices are plugged in or unplugged the list updates. you can click on a line to copy it to the clipboard, and there is a right-click menu to clear the list, rescan ports, etc:




cheers,
rob   :-)
 
tgerbic
Regular Member

Joined: 25/07/2019
Location: United States
Posts: 65
Posted: 09:06am 09 Jan 2025
Copy link to clipboard 
Print this post

Works fine on Fedora 40.
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025