[Lazarus] How to list available serial orts on Ubuuntu?

Giuliano Colla giuliano.colla at fastwebnet.it
Mon Nov 28 13:31:12 CET 2022


Il 28/11/22 11:31, Bo Berglund via lazarus ha scritto:
> 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>


More information about the lazarus mailing list