<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div class="moz-cite-prefix">Il 28/11/22 11:31, Bo Berglund via
lazarus ha scritto:<br>
</div>
<blockquote type="cite"
cite="mid:c539ohhn9q5bnh3dk5lsonl9uni01gp11r@4ax.com">
<pre class="moz-quote-pre" wrap="">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.
</pre>
</blockquote>
<p>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:</p>
<pre>{$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}
</pre>
<p>Hope that it helps,</p>
<p>Giuliano<br>
</p>
<pre class="moz-signature" cols="72">--
Do not do to others as you would have them do to you.They might have different tastes.</pre>
</body>
</html>