Welcome, Guest
Username: Password: Remember me
Discussions for CodeTyphon Object Pascal Programming Language
  • Page:
  • 1

TOPIC:

Synapse Synaser, list serial ports in linux 1 year 6 months ago #17659

  • Andrzej Franek
  • Andrzej Franek's Avatar Topic Author
  • Visitor
  • Visitor
The Synapse library in the SynaSer module includes a GetSerialPortNames function that lists available serial ports. On Linux, you get the contents of the directory listing files with the /dev/ser* mask. Below is my function (for linux) GetSerialPortNames returning a list of files from /dev/ser associated with connected devices. Maybe someone will find it useful.

{$ifdef linux}
function GetSerialPortNames: string;
var
  sr : TSearchRec;
  ser: TBlockSerial;
begin
  Result := '';
  if FindFirst('/dev/tty*', faAnyFile, sr) = 0 then
    repeat
      if (sr.Attr and $FFFFFFFF) = Sr.Attr then
      begin
         if (pos('S', sr.Name) > 0) or (pos('A', sr.Name) > 0) or (pos('USB', sr.Name) > 0) then
         begin
            try
            ser:=TBlockSerial.Create;
            ser.Connect('/dev/' + sr.Name);
            ser.config(460800,8,'N',0,false,true);
            ser.sendbyte(0);                      // try write to port
            if ser.LastError = 0 then             // write OK
               begin
               if Result <> '' then Result := Result + #13#10;
               Result := Result {+ '/dev/'} + sr.Name;
               end;
            finally
            ser.free;
            end;
         end;
      end;
    until FindNext(sr) <> 0;
  FindClose(sr);
end;
{$endif}    

 

Please Log in or Create an account to join the conversation.

Synapse Synaser, list serial ports in linux 1 year 6 months ago #17660

  • Matis A.
  • Matis A.'s Avatar
  • Away
  • Moderator
  • Moderator
  • Posts: 1090
  • Thank you received: 157
Thank
We will test and add your source code to Synapse pkg
PilotLogic Core Programmer

Please Log in or Create an account to join the conversation.

Synapse Synaser, list serial ports in linux 1 year 6 months ago #17675

  • Vlad Wilhelm
  • Vlad Wilhelm's Avatar
  • Offline
  • Junior Member
  • Junior Member
  • Posts: 34
  • Thank you received: 2
Gentlemen, good day!
It seems to me that a safer way would be to search the "/sys/class/tty/" folder for directories containing the "device/driver" folder, which indicates the presence of a physical device. This is sufficient WITHOUT transferring data to some file or even a real port that may be currently being used by another process.
For example:

procedure TmainFrm.FormCreate(Sender: TObject);
var
 sr: TSearchRec;
 r: integer;
begin
 ComboBox1.Items.Clear;
 r:= FindFirst('/sys/class/tty/*', faDirectory, sr);
 while r = 0 do begin
  if (sr.Name[1] <> '.') and DirectoryExists('/sys/class/tty/' + sr.Name + '/device/driver')
  then ComboBox1.Items.Add('/dev/' + sr.Name);
  r:= FindNext(sr);
    end;
 FindClose(sr);
end;

Sorry for the intrusion! :)

Please Log in or Create an account to join the conversation.

Last edit: by Vlad Wilhelm.
  • Page:
  • 1