Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2022-11

How to list available serial orts on Ubuuntu?

13 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bo Berglund

I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu.
It seems to build just fine except for a combobox fill function which uses the Windows way of populating the selectoer box with the serial ports available on the system.
This is the code I use on Windows:

function THanSimulatorMain.ListSerialPorts(LS: TStrings): integer;
var
  I:integer;
  {$ifdef MSWINDOWS}
  Reg:tregistry;
  {$endif}
begin
  {$ifdef MSWINDOWS}
  //List available serial ports on Windows:
  Reg := TRegistry.Create;
  try
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then
    begin
      LS.Clear;
      Reg.GetValueNames(LS);
      for I := 0 to LS.Count - 1 do
          LS[i] := Reg.ReadString(LS[i]);
    end;
    Result := LS.Count;
  finally
    Reg.Free;
  end;
  {$endif}
  {$ifdef UNIX}
  //List available serial ports on Linux:
  //What do I put here?
  {$endif}
end;

I have looked at /dev/tty* but it lists a large number of items which makes it impossible for me. I do not belive all of?them are real serial ports...

What can I do to get a selector for real serial ports on Ubuntu.

Bo Berglund Developer in Sweden

Reply by email

Christo Crause
to Bo Berglund

On Mon, Nov 28, 2022 at 12:31 PM Bo Berglund via lazarus <

Show quoted text 10 lines
wrote: I have looked at /dev/tty* but it lists a large number of items which makes it impossible for me. I do not belive all of?them are real serial ports... What can I do to get a selector for real serial ports on Ubuntu. https://lists.lazarus-ide.org/listinfo/lazarus

Here is an example for POSIX type OSs:
https://github.com/ccrause/LazScope/blob/master/gui/serialobject.pas#L299 -------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20221128/fa51fac6/attachment.htm>

Reply by email

Carlos E. R.
to Bo Berglund: I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu.…
Bo Berglund wrote 5 lines
I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu. It seems to build just fine except for a combobox fill function which uses the Windows way of populating the selectoer box with the serial ports available on the system.

...

I have looked at /dev/tty* but it lists a large number of items which makes it impossible for me. I do not belive all of?them are real serial ports...

I think they are /dev/ttyS*, but also /dev/ttyUSB* or /dev/ttyACM*, which is not all /dev/tty* :-D

What can I do to get a selector for real serial ports on Ubuntu.

Maybe (googled for "name of serial port in linux"):

https://www.cyberciti.biz/faq/find-out-linux-serial-ports-with-setserial/

What it says does not work in my machine (which has a true serial port)

dmesg | grep tty

because the log buffer has rotated out the boot section. But:

Telcontar:~ # zgrep -i 'ttyS' /var/log/messages-20221126.xz <0.6> 2022-11-17T18:18:26.555284+01:00 Telcontar kernel - - - [ 1.072472] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
<0.6> 2022-11-19T11:58:45.529903+01:00 Telcontar kernel - - - [ 1.082125] 00:05: ttyS0 at I/O 0x3f8 (irq = 4, base_baud = 115200) is a 16550A
Telcontar:~ #

But that is too complicated for your code, it is rather for a user to do
manually.

This other method works:

Telcontar:~ # setserial -g /dev/ttyS[0123]
/dev/ttyS0, UART: 16550A, Port: 0x03f8, IRQ: 4
/dev/ttyS1, UART: unknown, Port: 0x02f8, IRQ: 3
/dev/ttyS2, UART: unknown, Port: 0x03e8, IRQ: 4
/dev/ttyS3, UART: unknown, Port: 0x02e8, IRQ: 3
Telcontar:~ #

But it doesn't find *any* serial port. Not USB based ones unless you tell it to check those. Maybe you better ask the user to name the port, and you test to find out if it is true.

Cheers / Saludos, Carlos E. R. (from 15.3 x86_64 at Telcontar) -------------- next part -------------- A non-text attachment was scrubbed... Name: OpenPGP_signature Type: application/pgp-signature Size: 203 bytes Desc: OpenPGP digital signature URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20221128/e2727450/attachment.sig>

Reply by email

Giuliano Colla
to Bo Berglund: I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu.…

Il 28/11/22 11:31, Bo Berglund via lazarus ha scritto:

Show quoted text 43 lines
I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu. It seems to build just fine except for a combobox fill function which uses the Windows way of populating the selectoer box with the serial ports available on the system. This is the code I use on Windows: function THanSimulatorMain.ListSerialPorts(LS: TStrings): integer; var I:integer; {$ifdef MSWINDOWS} Reg:tregistry; {$endif} begin {$ifdef MSWINDOWS} //List available serial ports on Windows: Reg := TRegistry.Create; try Reg.RootKey := HKEY_LOCAL_MACHINE; if Reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then begin LS.Clear; Reg.GetValueNames(LS); for I := 0 to LS.Count - 1 do LS[i] := Reg.ReadString(LS[i]); end; Result := LS.Count; finally Reg.Free; end; {$endif} {$ifdef UNIX} //List available serial ports on Linux: //What do I put here? {$endif} end; I have looked at /dev/tty* but it lists a large number of items which makes it impossible for me. I do not belive all of?them are real serial ports... What can I do to get a selector for real serial ports on Ubuntu.

My solution has been to scan all possible port names, such as /dev/ttyS*, dev/ttyUSB*, /dev/ttyXR*, etc. and then attempt to open each one by using SerOpen (in Serial.pp from fpcsrc- add "Serial"? the Uses clause) If successful the port exists, and I add it to my list, if not, it doesn't exist. Sample code which displays the first valid serial port found and puts all the others in a drop-down list for the user to select:

{$IFDEF MSWINDOWS}
 ? for PortNr := 1 to 9 do begin
 ??? PortName := 'COM' + IntToStr(PortNr);
 ??? PortHandle := SerOpen('\\.\'+PortName);
 ??? if PortHandle > 0 then begin
 ????? cbSelPort.Items.Add(PortName);
 ????? SerClose(PortHandle);
 ????? if cbSelPort.Text = '' then begin
 ??????? cbSelPort.Text:=PortName;
 ??????? PortSel.Caption:= PortName;
 ??????? end;
 ????? end;
 ??? end;
{$ELSE}
 ? for PortNr := 0 to 9 do begin
 ??? PortName := '/dev/ttyS' + IntToStr(PortNr);
 ??? writeln('Trying ',PortName);
 ??? //PortHandle:= FpOpen(PortName, O_RDWR {or O_NOCTTY} or O_NDELAY);
 ??? PortHandle := SerOpen(PortName);
 ??? if PortHandle > 0 then begin
 ????? if cbSelPort.Text = '' then begin
 ??????? cbSelPort.Text:=PortName;
 ??????? PortSel.Caption:= PortName;
 ??????? end;
 ????? cbSelPort.Items.Add(PortName);
 ????? SerClose(PortHandle);
 ????? end;
 ??? end;
 ? for PortNr := 0 to 9 do begin
 ??? PortName := '/dev/ttyUSB' + IntToStr(PortNr);
 ??? //writeln('Trying ',PortName);
 ??? PortHandle := SerOpen(PortName);
 ??? if PortHandle > 0 then begin
 ????? if cbSelPort.Text = '' then begin
 ??????? cbSelPort.Text:=PortName;
 ??????? PortSel.Caption:= PortName;
 ??????? end;
 ????? cbSelPort.Items.Add(PortName);
 ????? SerClose(PortHandle);
 ????? end;
 ??? end;
 ? for PortNr := 0 to 9 do begin
 ??? PortName := '/dev/ttyXR' + IntToStr(PortNr);
 ??? //writeln('Trying ',PortName);
 ??? PortHandle := SerOpen(PortName);
 ??? if PortHandle > 0 then begin
 ????? if cbSelPort.Text = '' then begin
 ??????? cbSelPort.Text:=PortName;
 ??????? PortSel.Caption:= PortName;
 ??????? end;
 ????? cbSelPort.Items.Add(PortName);
 ????? SerClose(PortHandle);
 ????? end;
 ??? end;
 ? for PortNr := 0 to 9 do begin
 ??? PortName := '/dev/ttyACM' + IntToStr(PortNr);
 ??? //writeln('Trying ',PortName);
 ??? PortHandle := SerOpen(PortName);
 ??? if PortHandle > 0 then begin
 ????? if cbSelPort.Text = '' then begin
 ??????? cbSelPort.Text:=PortName;
 ??????? PortSel.Caption:= PortName;
 ??????? end;
 ????? cbSelPort.Items.Add(PortName);
 ????? SerClose(PortHandle);
 ????? end;
 ??? end;
 ? {$ifdef cpuarm} //Raspberry
 ? for PortNr := 0 to 9 do begin
 ??? PortName := '/dev/ttyAMA' + IntToStr(PortNr);
 ??? PortHandle := SerOpen(PortName);
 ??? if PortHandle > 0 then begin
 ????? if cbSelPort.Text = '' then begin
 ??????? cbSelPort.Text:=PortName;
 ??????? PortSel.Caption:= PortName;
 ??????? end;
 ????? cbSelPort.Items.Add(PortName);
 ????? SerClose(PortHandle);
 ????? end;
 ??? end;
 ? {$endif}
{$ENDIF}

Hope that it helps,

Giuliano

Do not do to others as you would have them do to you.They might have different tastes. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20221128/df3d0bb0/attachment.htm>

Reply by email

Bo Berglund
to Giuliano Colla
Giuliano Colla wrote
Hope that it helps,

Thanks, it does.

I had thought of this but it looked like an overly complicated way given that the list of tty* ports using ls -l /dev/tty* on my system comprises no less than 98 hits....

So I am now limiting the program to use USB connected serial adapters USB0..USB3 and on start I populate the list of ports by checking that /dev/USBx eists where x is 0..3.

After all this is the most common use nowadays with USB serial devices in connection with developing IoT devices.

Bo Berglund Developer in Sweden

Reply by email

Carlos E. R.
to Bo Berglund
Bo Berglund wrote 10 lines
On Mon, 28 Nov 2022 13:31:12 +0100, Giuliano Colla via lazarus wrote: > Hope that it helps, Thanks, it does. I had thought of this but it looked like an overly complicated way given that the list of tty* ports using ls -l /dev/tty* on my system comprises no less than 98 hits....

Please see my post. It is not tty*

Cheers / Saludos, Carlos E. R. (from 15.3 x86_64 at Telcontar) -------------- next part -------------- A non-text attachment was scrubbed... Name: OpenPGP_signature Type: application/pgp-signature Size: 203 bytes Desc: OpenPGP digital signature URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20221128/be961188/attachment.sig>

Reply by email

Giuliano Colla
to Bo Berglund: On Mon, 28 Nov 2022 13:31:12 +0100, Giuliano Colla via lazarus Thanks, it does. I had…

Il 28/11/22 14:08, Bo Berglund via lazarus ha scritto:

Show quoted text 18 lines
On Mon, 28 Nov 2022 13:31:12 +0100, Giuliano Colla via lazarus wrote: > Hope that it helps, Thanks, it does. I had thought of this but it looked like an overly complicated way given that the list of tty* ports using ls -l /dev/tty* on my system comprises no less than 98 hits.... So I am now limiting the program to use USB connected serial adapters USB0..USB3 and on start I populate the list of ports by checking that /dev/USBx eists where x is 0..3. After all this is the most common use nowadays with USB serial devices in connection with developing IoT devices.

/dev/tty* is pretty much useless. It's there for historical reasons. /dev/ttyS* is the one for actual serial adapters, and, unless you override with a command line parameter on boot, it is limited to 0..3 in modern kernels.

Giuliano

Do not do to others as you would have them do to you.They might have different tastes.

Reply by email

Bo Berglund
to Giuliano Colla: Il 28/11/22 14:08, Bo Berglund via lazarus ha scritto: /dev/tty* is pretty much useless.…
Giuliano Colla wrote 5 lines
/dev/tty* is pretty much useless. It's there for historical reasons. /dev/ttyS* is the one for actual serial adapters, and, unless you override with a command line parameter on boot, it is limited to 0..3 in modern kernels.

Here is what I get on Ubuntu 20.04.5 LTS:

$ ll /dev/ttyS*
crw-rw---- 1 root dialout 4, 64 2022-11-26 08:47 /dev/ttyS0 crw-rw---- 1 root dialout 4, 65 2022-11-26 08:47 /dev/ttyS1 crw-rw---- 1 root dialout 4, 74 2022-11-26 08:47 /dev/ttyS10 crw-rw---- 1 root dialout 4, 75 2022-11-26 08:47 /dev/ttyS11 crw-rw---- 1 root dialout 4, 76 2022-11-26 08:47 /dev/ttyS12 crw-rw---- 1 root dialout 4, 77 2022-11-26 08:47 /dev/ttyS13 crw-rw---- 1 root dialout 4, 78 2022-11-26 08:47 /dev/ttyS14 crw-rw---- 1 root dialout 4, 79 2022-11-26 08:47 /dev/ttyS15 crw-rw---- 1 root dialout 4, 80 2022-11-26 08:47 /dev/ttyS16 crw-rw---- 1 root dialout 4, 81 2022-11-26 08:47 /dev/ttyS17 crw-rw---- 1 root dialout 4, 82 2022-11-26 08:47 /dev/ttyS18 crw-rw---- 1 root dialout 4, 83 2022-11-26 08:47 /dev/ttyS19 crw-rw---- 1 root dialout 4, 66 2022-11-26 08:47 /dev/ttyS2 crw-rw---- 1 root dialout 4, 84 2022-11-26 08:47 /dev/ttyS20 crw-rw---- 1 root dialout 4, 85 2022-11-26 08:47 /dev/ttyS21 crw-rw---- 1 root dialout 4, 86 2022-11-26 08:47 /dev/ttyS22 crw-rw---- 1 root dialout 4, 87 2022-11-26 08:47 /dev/ttyS23 crw-rw---- 1 root dialout 4, 88 2022-11-26 08:47 /dev/ttyS24 crw-rw---- 1 root dialout 4, 89 2022-11-26 08:47 /dev/ttyS25 crw-rw---- 1 root dialout 4, 90 2022-11-26 08:47 /dev/ttyS26 crw-rw---- 1 root dialout 4, 91 2022-11-26 08:47 /dev/ttyS27 crw-rw---- 1 root dialout 4, 92 2022-11-26 08:47 /dev/ttyS28 crw-rw---- 1 root dialout 4, 93 2022-11-26 08:47 /dev/ttyS29 crw-rw---- 1 root dialout 4, 67 2022-11-26 08:47 /dev/ttyS3 crw-rw---- 1 root dialout 4, 94 2022-11-26 08:47 /dev/ttyS30 crw-rw---- 1 root dialout 4, 95 2022-11-26 08:47 /dev/ttyS31 crw-rw---- 1 root dialout 4, 68 2022-11-26 08:47 /dev/ttyS4 crw-rw---- 1 root dialout 4, 69 2022-11-26 08:47 /dev/ttyS5 crw-rw---- 1 root dialout 4, 70 2022-11-26 08:47 /dev/ttyS6 crw-rw---- 1 root dialout 4, 71 2022-11-26 08:47 /dev/ttyS7 crw-rw---- 1 root dialout 4, 72 2022-11-26 08:47 /dev/ttyS8 crw-rw---- 1 root dialout 4, 73 2022-11-26 08:47 /dev/ttyS9

32 in all...

Bo Berglund Developer in Sweden

Reply by email

Giuliano Colla
to Bo Berglund

Inviato da iPhone

Show quoted text 57 lines
Il giorno 28 nov 2022, alle ore 17:00, Bo Berglund via lazarus ha scritto: ?On Mon, 28 Nov 2022 14:48:20 +0100, Giuliano Colla via lazarus wrote: > /dev/tty* is pretty much useless. It's there for historical reasons. > /dev/ttyS* is the one for actual serial adapters, and, unless you > override with a command line parameter on boot, it is limited to 0..3 in > modern kernels. > Here is what I get on Ubuntu 20.04.5 LTS: $ ll /dev/ttyS* crw-rw---- 1 root dialout 4, 64 2022-11-26 08:47 /dev/ttyS0 crw-rw---- 1 root dialout 4, 65 2022-11-26 08:47 /dev/ttyS1 crw-rw---- 1 root dialout 4, 74 2022-11-26 08:47 /dev/ttyS10 crw-rw---- 1 root dialout 4, 75 2022-11-26 08:47 /dev/ttyS11 crw-rw---- 1 root dialout 4, 76 2022-11-26 08:47 /dev/ttyS12 crw-rw---- 1 root dialout 4, 77 2022-11-26 08:47 /dev/ttyS13 crw-rw---- 1 root dialout 4, 78 2022-11-26 08:47 /dev/ttyS14 crw-rw---- 1 root dialout 4, 79 2022-11-26 08:47 /dev/ttyS15 crw-rw---- 1 root dialout 4, 80 2022-11-26 08:47 /dev/ttyS16 crw-rw---- 1 root dialout 4, 81 2022-11-26 08:47 /dev/ttyS17 crw-rw---- 1 root dialout 4, 82 2022-11-26 08:47 /dev/ttyS18 crw-rw---- 1 root dialout 4, 83 2022-11-26 08:47 /dev/ttyS19 crw-rw---- 1 root dialout 4, 66 2022-11-26 08:47 /dev/ttyS2 crw-rw---- 1 root dialout 4, 84 2022-11-26 08:47 /dev/ttyS20 crw-rw---- 1 root dialout 4, 85 2022-11-26 08:47 /dev/ttyS21 crw-rw---- 1 root dialout 4, 86 2022-11-26 08:47 /dev/ttyS22 crw-rw---- 1 root dialout 4, 87 2022-11-26 08:47 /dev/ttyS23 crw-rw---- 1 root dialout 4, 88 2022-11-26 08:47 /dev/ttyS24 crw-rw---- 1 root dialout 4, 89 2022-11-26 08:47 /dev/ttyS25 crw-rw---- 1 root dialout 4, 90 2022-11-26 08:47 /dev/ttyS26 crw-rw---- 1 root dialout 4, 91 2022-11-26 08:47 /dev/ttyS27 crw-rw---- 1 root dialout 4, 92 2022-11-26 08:47 /dev/ttyS28 crw-rw---- 1 root dialout 4, 93 2022-11-26 08:47 /dev/ttyS29 crw-rw---- 1 root dialout 4, 67 2022-11-26 08:47 /dev/ttyS3 crw-rw---- 1 root dialout 4, 94 2022-11-26 08:47 /dev/ttyS30 crw-rw---- 1 root dialout 4, 95 2022-11-26 08:47 /dev/ttyS31 crw-rw---- 1 root dialout 4, 68 2022-11-26 08:47 /dev/ttyS4 crw-rw---- 1 root dialout 4, 69 2022-11-26 08:47 /dev/ttyS5 crw-rw---- 1 root dialout 4, 70 2022-11-26 08:47 /dev/ttyS6 crw-rw---- 1 root dialout 4, 71 2022-11-26 08:47 /dev/ttyS7 crw-rw---- 1 root dialout 4, 72 2022-11-26 08:47 /dev/ttyS8 crw-rw---- 1 root dialout 4, 73 2022-11-26 08:47 /dev/ttyS9 32 in all... -- Bo Berglund Developer in Sweden -- _______________________________________________ lazarus mailing list https://lists.lazarus-ide.org/listinfo/lazarus

Yes they?re there but they?re not enabled. If you search for ttyS* in the boot log you?ll see how many are probed for. Typically from ttyS0 to ttyS3. Maybe less, it depends on which ones are enabled in BIOS. I know because I had in a project an extra PCI board with four more ports, and I had to modify the boot parameters to have them properly initialized. Otherways they were completely non existing. Just a dummy entry in the /dev folder.

Reply by email

Wayne Sherman
to Giuliano Colla: Il 28/11/22 11:31, Bo Berglund via lazarus ha scritto: My solution has been to scan all…
Show quoted text 5 lines
My solution has been to scan all possible port names, such as /dev/ttyS*, dev/ttyUSB*, /dev/ttyXR*, etc. and then attempt to open each one by using SerOpen (in Serial.pp from fpcsrc- add "Serial" the Uses clause) If successful the port exists, and I add it to my list, if not, it doesn't exist.

If the port is in use (already open in another process), you will get a SerOpen error and incorrectly exclude that port.

Some other ways to find Linux serial ports are discussed here: https://stackoverflow.com/questions/2530096/how-to-find-all-serial-devices-ttys-ttyusb-on-linux-without-opening-them

Reply by email

Unknown author
to Bo Berglund: I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu.…
Bo Berglund wrote 6 lines
I haved a debugging tool program created on Windows and I am orting it to Linux Ubuntu. It seems to build just fine except for a combobox fill function which uses the Windows way of populating the selectoer box with the serial ports available on the system. This is the code I use on Windows:

[...]

I have looked at /dev/tty* but it lists a large number of items which makes it impossible for me. I do not belive all of?them are real serial ports... What can I do to get a selector for real serial ports on Ubuntu.

would you believe you asked almost this very same question back in 2015, 2018, 2020, and here again in 2022 :)

in Sep 2018, giuliano colla posted this procedure to you:

procedure TForm1.btnScanPortClick(Sender: TObject);
var
  PortNr: Integer;
  PortName: String;
  PortHandle: TSerialHandle;
begin
   cbSelPort.Items.Clear;
   cbSelPort.Text:= '';
{$IFDEF MSWINDOWS}
   for PortNr := 1 to 9 do begin
     PortName := 'COM' + IntToStr(PortNr);
     PortHandle := SerOpen('\\.\'+PortName);
     if PortHandle > 0 then begin
       cbSelPort.Items.Add(PortName);
       SerClose(PortHandle);
       if cbSelPort.Text = '' then begin
         cbSelPort.Text:=PortName;
         PortSel.Caption:= PortName;
         end;
       end;
     end;
{$ELSE}
   for PortNr := 0 to 9 do begin
     PortName := '/dev/ttyS' + IntToStr(PortNr);
     PortHandle := SerOpen(PortName);
     if PortHandle > 0 then begin
       if cbSelPort.Text = '' then begin
         cbSelPort.Text:=PortName;
         PortSel.Caption:= PortName;
         end;
       cbSelPort.Items.Add(PortName);
       SerClose(PortHandle);
       end;
     end;
   for PortNr := 0 to 9 do begin
     PortName := '/dev/ttyUSB' + IntToStr(PortNr);
     PortHandle := SerOpen(PortName);
     if PortHandle > 0 then begin
       if cbSelPort.Text = '' then begin
         cbSelPort.Text:=PortName;
         PortSel.Caption:= PortName;
         end;
       cbSelPort.Items.Add(PortName);
       SerClose(PortHandle);
       end;
     end;
{$ENDIF}
end;

and in that same thread in Sep 2018, mark morgan lloyd posted this function to you:

(* If there are no serial ports on the system then return NIL, otherwise a      *)
(* TStringList.        *)
(*        *)
(* This returns an object, it is the caller's (eventual) responsibility to free *)
(* this.        *)
//
FUNCTION EnumeratePorts: TStringList;
(* On a Linux system with udev or similar the /dev directory will only contain  *)
(* devices which have been detected as being present. On an older system it     *)
(* will probably contain all possible devices so we need to restrict the list   *)
(* to what's reasonable; the user should still be able to enter a device name   *)
(* manually if necessary, e.g. where a machine with 2x standard ports plus a    *)
(* 2-port card insists that it's got ttyS0, ttyS1, ttyS45 and ttyS46.        *)

CONST countTtyS= 12; (* Main board plus an 8-port card *) countTtyUSB= 8; (* Four dual-port adapters *) countTtyI= 4; (* A single 4-port ISDN card. *)

VAR     searchRec: TSearchRec;
         counter: INTEGER;
BEGIN
   RESULT:= TStringList.Create;
   RESULT.Sorted:= TRUE;
   counter:= countTtyS;
   IF FindFirst('/dev/ttyS*', faSysFile, searchRec) = 0 THEN
     REPEAT
       RESULT.Append('/dev/' + searchRec.Name);
       DEC(counter)
     UNTIL (FindNext(searchRec)  0) OR (counter <= 0);
   FindClose(searchRec);
   counter:= countTtyUSB;
   IF FindFirst('/dev/ttyUSB*', faSysFile, searchRec) = 0 THEN
     REPEAT
       RESULT.Append('/dev/' + searchRec.Name);
       DEC(counter)
     UNTIL (FindNext(searchRec)  0) OR (counter <= 0);
   FindClose(searchRec);
   counter:= countTtyI;
   IF FindFirst('/dev/ttyI*', faSysFile, searchRec) = 0 THEN
     REPEAT
       RESULT.Append('/dev/' + searchRec.Name);
       DEC(counter)
     UNTIL (FindNext(searchRec)  0) OR (counter <= 0);
   FindClose(searchRec);
   IF Result.Count = 0 THEN
     FreeAndNil(RESULT)
END { EnumeratePorts } ;

in another thread back in Oct 2020, posted this function to you:

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;

i love having mailing list history in my email reader! :) :) :)

NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list where it belongs!*

Reply by email

Giuliano Colla
to Unknown author

Il 29/11/2022 11:52, wkitty42--- via lazarus ha scritto:

would you believe you asked almost this very same question back in 2015, 2018, 2020, and here again in 2022 :)

:-):-):-)

Do not do to others as you would have them do to you.They might have different tastes.

Reply by email