Author Topic: Listing Linx serial ports?  (Read 2314 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Listing Linx serial ports?
« on: April 30, 2023, 06:00:41 pm »
I am porting a program developed using Lazarus 2.2.6/Fpc 3.2.2 on Windows to Linux (Raspberry Pi4), where I use teh same dev IDE setup.

The program uses a serial port to communicate with an external device and I have a combobox for the user to select the port to use.
It uses the Windows Registry to list the available serial ports in order to populate the combobox.
Obviously this does not work on Linux so I need a different method...

I can see the port when I list the /dev/tty* devices but the list contains a lot (like 60+) of such devices and I am pretty sure most are only placeholders not representing actual usable serial ports...

So I am looking for a way to list only real serial ports that exist on the system, which can be used as device names for opening the port when using the fpc built-in serial unit.

Googling has not given me any useful advice only non-working examples like:
using dmesg | grep tty but it shows nothing.

I assume Linux does know if there are available serial ports on the system???

--
Bo Berglund
Sweden

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Listing Linx serial ports?
« Reply #1 on: April 30, 2023, 06:05:11 pm »
Found someone describing exactly the issue you described, https://www.pragmaticlinux.com/2023/01/how-to-list-all-serial-ports-on-linux/

as an additional side note, do realize that when you want to use the serialdevice that either you (the user) or your program must be part of a group that allows you to communicate through serials devices (usually dialout))



edit: Attached an example for Free Pascal. You should be able to use/incorporate the subroutines that matter. Not sure if I did things correct though so, please let me know if it works for you or not.
« Last Edit: April 30, 2023, 07:42:42 pm by TRon »
Today is tomorrow's yesterday.

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: Listing Linx serial ports?
« Reply #2 on: April 30, 2023, 09:42:59 pm »
Thanks a lot!
This seems to work at least on my RPi4 wit a single serial port.
Code: Text  [Select][+][-]
  1.  $ ./cereal
  2. It's life Jim ...
  3. List of 1 serial devices
  4. /dev/ttyAMA0
  5. ... but not as we know it
  6.  
  7. PS: My laptop keyboard has problems, hence the mis-spelling of Linux in the subject... DS
  8.  
I will try to locate a Pi unit with more than 1 and check on that.
Not available now though.
« Last Edit: April 30, 2023, 10:01:22 pm by BosseB »
--
Bo Berglund
Sweden

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Listing Linx serial ports?
« Reply #3 on: April 30, 2023, 09:55:44 pm »
Thanks a lot!
You're welcome.

I did not had the time to work on it before posting my reply so had to do that later.


Quote
This seems to work at least on my RPi4 wit a single serial port.
Great!

Thank you for reporting back.

Quote
I will try to locate a Pi unit with more than 1 and check on that.
Not available now though.
Sorry for being a bit daft but did you mean the ports are not available (program fail) or your raspberry pi to test it on is not available (cannot test/check because of missing hardware) ?
Today is tomorrow's yesterday.

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: Listing Linx serial ports?
« Reply #4 on: April 30, 2023, 10:10:19 pm »
Quote
Sorry for being a bit daft but did you mean the ports are not available (program fail) or your raspberry pi to test it on is not available (cannot test/check because of missing hardware) ?

The latter in principle...
I am visiting my daughter this week and I can only connect to my devices back home via SSH and VPN. And all of the units only have the regular single serial0 port until I plug in an USB-to-Serial converter. So When I am back home I can do this and check that it will list these as well.

Hacking away on the porting anyway since I have the VPN + VNC to reach Lazarus on the dev RPi4 at home, but hard to do the plugging from 5000 km away.
 :D
--
Bo Berglund
Sweden

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Listing Linx serial ports?
« Reply #5 on: April 30, 2023, 10:21:04 pm »
Ok, thank you clearing that up for me BosseB.

No problem regarding the time frame.

In case it does provide issues with your other pi then please report.
Today is tomorrow's yesterday.

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: Listing Linx serial ports?
« Reply #6 on: May 01, 2023, 12:40:26 am »
Well I figured I could check my other devices back home and it turned out that one of my HP Workstation laptops running Ubuntu was on-line so I transferred the file to that and fired up Lazarus 2.2.4 on it (via VNC) and could compile your program.
Then the output when running:

Code: Text  [Select][+][-]
  1. $ ./cereal
  2. It's life Jim ...
  3. List of 1 serial devices
  4. /dev/ttyUSB0
  5. ... but not as we know it
  6.  

So it looks like a USB connected serial device is also discovered and in a different operating system version too.  :D :D
--
Bo Berglund
Sweden

Graham1

  • Jr. Member
  • **
  • Posts: 74
Re: Listing Linx serial ports?
« Reply #7 on: May 02, 2023, 07:25:59 am »
Windows 10/11 Home 64-bit (and Linux because I have to)
Lazarus 2.0.12 / FPC 3.2.0 (because libQt5pas 1.2.6)
Linux Mint 20 (because GLIBC_2.31)

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Listing Linx serial ports?
« Reply #8 on: May 02, 2023, 01:02:27 pm »
That is indeed very helpful Graham. Thank you very much for that.

@BosseB:
Just give word in case you do not know (exactly) how that can be useful for your project.
Today is tomorrow's yesterday.

BosseB

  • Sr. Member
  • ****
  • Posts: 484
Re: Listing Linx serial ports?
« Reply #9 on: May 02, 2023, 04:28:45 pm »
That is indeed very helpful Graham. Thank you very much for that.

@BosseB:
Just give word in case you do not know (exactly) how that can be useful for your project.
Well, I do not know how it affects me..

However now I have used your code to create a unit for handling the listing of serial ports on Linux.
And I had problems with the (for me) unfamiliar data type TStringArray, so I created an overloaded version of the function GetSerialDevices() where a TStringList is used to transfer the list to my main application.
(my unit is attached)

This works fine for now but I have only tested on devices with a single serial port available so far. Next week when I get back home I can plug in a couple of extra USB-RS232 converters and check if it works than as well.

And thanks again for your help! Much appreciated!
--
Bo Berglund
Sweden

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: Listing Linx serial ports?
« Reply #10 on: May 04, 2023, 05:51:28 am »
Well, I do not know how it affects me..
Ofc, if you don't let it it will not affect you (your project) one bit  :P

I created/presented a solution in order to (hopefully) show you that whenever you face such an issue/problem you can make a more generic (Linux related) search and in this particular case be able to find an answer that uses the shell (ls in particular in combination with grep) in order to 'solve' the problem and, how that can be "translated" to Pascal.

Ofc. You could also use TProcess for that and catch the output of the executed shell commands in order to obtain the same results e.g. it would be another implementation but using the same approach.

The solution that Graham1 presents is (beside being another way that is able to lead to Rome) a more programmatic/technical approach (depending on the systems (OS) known characteristics of such devices).
It (should also) result in the very same list with (serial) device names.

Quote
However now I have used your code to create a unit for handling the listing of serial ports on Linux.
And I had problems with the (for me) unfamiliar data type TStringArray, so I created an overloaded version of the function GetSerialDevices() where a TStringList is used to transfer the list to my main application.
If you are more comfortable using a stringlist then that is perfectly fine.

With stringlists it seems that /I/ never can't decide if it should be a function and/or procedure and if the subroutine should create the stringlist automatically or not. So technically I chickened out  :-[

Quote
(my unit is attached)
Thank you for your changes.

I have attached an updated version of the cereal example as well by adding (also adopted a little) Graham1's implementation and also adding another function based on Graham's code that retrieves the characteristics of the found serial device (which are also displayed).

I do not know if that is or can be helpful for your project but I thought it was a nice addition showing the usefulness of Graham's implementation.

Oh, almost forgot. I added your stringlist version as well so that you can see/test/verify that the 3 implementations return the same results (at least in theory they should).
Today is tomorrow's yesterday.

 

TinyPortal © 2005-2018