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

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list
Is there a way to list the available (working) serial ports on a Linux
platform like the RaspberryPi?

I would like to offer a dropdown list of working serial ports to the
user to select among, but I am dissuaded from it when I do the
following:

ls -la /dev/tty*
It returns a very long list of serial devices and most of these are
probably not even existing in real life.

On an RPi I get
/dev/tty
/dev/ttyN (where N=0..63
/dev/ttyAMA0 <== This is RPi Serial0 port on the pin header.
/dev/ttyprintk
and:
/dev/ttyUSB0../dev/ttyUSB3 depending on what is plugged into USB

All in all about 70 devices, but not sure which are actually available
for use.

Is this an impossible task or can one check for "live" serial ports
only? If so how?

On Windows I have ported a convoluted function from Delphi, which uses
Registry reads to give me the list.


--
Bo Berglund
Developer in Sweden

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list

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?


On 7/10/20 10:35 AM, Bo Berglund via lazarus wrote:
Is there a way to list the available (working) serial ports on a Linux
platform like the RaspberryPi?

I would like to offer a dropdown list of working serial ports to the
user to select among, but I am dissuaded from it when I do the
following:

ls -la /dev/tty*
It returns a very long list of serial devices and most of these are
probably not even existing in real life.

On an RPi I get
/dev/tty
/dev/ttyN (where N=0..63
/dev/ttyAMA0 <== This is RPi Serial0 port on the pin header.
/dev/ttyprintk
and:
/dev/ttyUSB0../dev/ttyUSB3 depending on what is plugged into USB

All in all about 70 devices, but not sure which are actually available
for use.

Is this an impossible task or can one check for "live" serial ports
only? If so how?

On Windows I have ported a convoluted function from Delphi, which uses
Registry reads to give me the list.



--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
That is how do it:

Function GetSerialPortNames(): String;
Var
  sl: TStringlist;
Var
  Info: TSearchRec;
  hdl: THandle;
  b: Boolean;
Begin
  sl := TStringlist.create;
  If FindFirst('/dev/tty*', faSysFile, Info) = 0 Then Begin
    Repeat
      b := true;
      Try
        hdl := FileOpen('/dev/' + info.Name, fmOpenReadWrite);
        If hdl = -1 Then Begin
          b := false;
        End;
      Except
        b := false;
      End;
      If hdl >= 0 Then Begin
        FileClose(hdl);
      End;
      If b Then Begin
        sl.Add('/dev/' + info.Name);
      End;
    Until FindNext(info) <> 0;
  End;
  FindClose(Info);
  result := sl.CommaText;
  sl.free;
End;

On 10/6/20 11:35 PM, Bo Berglund via lazarus wrote:

> Is there a way to list the available (working) serial ports on a Linux
> platform like the RaspberryPi?
>
> I would like to offer a dropdown list of working serial ports to the
> user to select among, but I am dissuaded from it when I do the
> following:
>
> ls -la /dev/tty*
> It returns a very long list of serial devices and most of these are
> probably not even existing in real life.
>
> On an RPi I get
> /dev/tty
> /dev/ttyN (where N=0..63
> /dev/ttyAMA0 <== This is RPi Serial0 port on the pin header.
> /dev/ttyprintk
> and:
> /dev/ttyUSB0../dev/ttyUSB3 depending on what is plugged into USB
>
> All in all about 70 devices, but not sure which are actually available
> for use.
>
> Is this an impossible task or can one check for "live" serial ports
> only? If so how?
>
> On Windows I have ported a convoluted function from Delphi, which uses
> Registry reads to give me the list.
>
>

--

Auf meiner Homepage www.Corpsman.de ist immer was los, ständig wird sie aktualisiert und erweitert. Da ist für jeden was dabei.

--

Schütze deine Privatsphäre, nutze E-mail Verschlüsselung.
Wie das geht steht z.B. hier :
https://support.mozilla.org/en-US/kb/digitally-signing-and-encrypting-messages#w_installing-gpg-and-enigmail

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus
Reply | Threaded
Open this post in threaded view
|

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

Free Pascal - Lazarus mailing list
In reply to this post by Free Pascal - Lazarus mailing list
On Wed, 7 Oct 2020 11:43:50 +1300, Wolf via lazarus
<[hidden email]> 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

--
_______________________________________________
lazarus mailing list
[hidden email]
https://lists.lazarus-ide.org/listinfo/lazarus