Recent

Author Topic: serial port properties  (Read 1467 times)

Rik

  • Jr. Member
  • **
  • Posts: 84
serial port properties
« on: August 05, 2025, 01:13:13 pm »
Elsewhere in this forum (https://forum.lazarus.freepascal.org/index.php/topic,16616.msg90544.html#msg90544) I found a script that return the friendly name of a serial port via the Windows Registry.
The friendly name is retrieved using the TRegistry.ReadString('FriendlyName') function.
I would expect that it is possible to retrieve other serial port properties as wel via the ReadString function?
With properties I mean those that can be found via the Device Manager -> Ports (COM & LPT)-> Device -> Properties -> Details.
As a quick and dirty test I tried to get the Device description using TRegistry.ReadString('DeviceDescription'), but that failed.

Is it possible to retrieve other properties as wel via the ReadString function?
If so, where can I find a list of the strings to be used and the corresponding properties?
.

gues1

  • Guest
Re: serial port properties
« Reply #1 on: August 05, 2025, 03:15:23 pm »
All Hardware properties (where settings were maintained) are described in the key:

HKLM/System/CurrentControlSet/Enum

But more subkey are locked (you neede special auth to access them, admin is not enough).

You can access to these data and more via WMI interface, here one example: https://theroadtodelphi.com/category/wmi/

For serial port you can also open all of them and read directly the properties, but you will not know the limits (for example maximun baud rates supported).

Rik

  • Jr. Member
  • **
  • Posts: 84
Re: serial port properties
« Reply #2 on: August 05, 2025, 03:54:44 pm »
Quote
All Hardware properties (where settings were maintained) are described in the key: HKLM/System/CurrentControlSet/Enum
Indeed, but I already tried that ...
Quote
But more subkey are locked (you neede special auth to access them, admin is not enough)
.. and got stuck on an access denied error for the subkey Properties, even with admin privileges.

Quote
You can access to these data and more via WMI interface
That's new stuff to me, I'll have a look at it.

Rik

  • Jr. Member
  • **
  • Posts: 84
Re: serial port properties
« Reply #3 on: August 08, 2025, 12:49:33 pm »
WMI is not needed for what I was looking for.
The funcion TRegistry.GetValueNames: TUnicodeStringArray returns the names of all available properties of the serial port.

CM630

  • Hero Member
  • *****
  • Posts: 1527
  • Не съм сигурен, че те разбирам.
    • http://sourceforge.net/u/cm630/profile/
Re: serial port properties
« Reply #4 on: August 08, 2025, 01:42:57 pm »
I use this:

Code: Pascal  [Select][+][-]
  1. function GetComFriendlyName (PortName : string) : String;
  2. var
  3.   Registry : TRegistry;
  4.   PortType: String = '';
  5.   VID_PID: String = '';
  6.   ID: string = '';
  7.   Contents : array of String;
  8.   KeyName: String = '';
  9. begin
  10.   Result := '';
  11.   Registry := TRegistry.Create(KEY_READ);
  12.   try
  13.     Registry.RootKey := HKEY_LOCAL_MACHINE;
  14.     if Registry.OpenKey('\SYSTEM\ControlSet001\Control\COM Name Arbiter\Devices',False) then
  15.     begin
  16.       Contents := Registry.ReadString(PortName).Split(['#']);
  17.       if (Length(Contents) > 2) then
  18.       begin
  19.         PortType := copy(Contents[0],5,MaxInt) ;
  20.         VID_PID := Contents[1];
  21.         ID := Contents[2];
  22.         KeyName := '\SYSTEM\ControlSet001\Enum\' + PortType + '\' + VID_PID + '\' + ID;
  23.         if Registry.OpenKey(KeyName,False) then
  24.           Result := Registry.ReadString('FriendlyName');
  25.       end; //Length
  26.     end; //if Registry.OpenKey
  27.   finally
  28.     Registry.Free;
  29.   end; //try
  30. end;
  31.  
  32. procedure TForm1.FormCreate(Sender: TObject);
  33. begin
  34.   ShowMessage (GetComFriendlyName('COM51'));
  35. end;


I get the port names from GetSerialPortNames, which is a routine of the multiplatform TLazSerial.
How to get friendly names in Linux (if they exist) - I do not know.


« Last Edit: August 08, 2025, 02:07:08 pm by CM630 »
Лазар 4,2 32 bit (sometimes 64 bit); FPC3,2,2

gues1

  • Guest
Re: serial port properties
« Reply #5 on: August 08, 2025, 03:18:22 pm »
WMI is not needed for what I was looking for.
The funcion TRegistry.GetValueNames: TUnicodeStringArray returns the names of all available properties of the serial port.

Good to know.

jamie

  • Hero Member
  • *****
  • Posts: 7323
Re: serial port properties
« Reply #6 on: September 13, 2025, 04:27:49 pm »
The only true wisdom is knowing you know nothing

Jurassic Pork

  • Hero Member
  • *****
  • Posts: 1290
Re: serial port properties
« Reply #7 on: September 14, 2025, 01:07:11 am »
WMI is not needed for what I was looking for.
Hello,
Maybe : it is what you are looking for  ;D
With this code , i can retrieve some properties of my Serial ports on Windows 11 with Lazarus 3.6 .
Code: Pascal  [Select][+][-]
  1. implementation
  2. uses contnrs,utilwmi;
  3. {$R *.lfm}
  4. { TForm1 }
  5. procedure TForm1.Button5Click(Sender: TObject);
  6. var
  7.   WMIResult         : TFPObjectList;
  8.   i                 : Integer;
  9.   PropNamesIndex    : Integer;
  10.   PropNames         : Array[0..4] of String =
  11.                       ( 'Manufacturer','Caption','Name',
  12.                         'Description','DeviceID');
  13.   PropName       : String;
  14.   PropValue      : String;
  15. begin
  16.     WMIResult := GetWMIInfo('Win32_PnPEntity', PropNames,
  17.                  'Where PnpClass = ''Ports'' ');
  18.                  // 'Where Name LIKE ''%(COM%'' ');
  19.   for i := 0 to Pred(WMIResult.Count) do
  20.   begin
  21.     Memo1.Append('================================================');
  22.     for PropNamesIndex := Low(PropNames) to High(PropNames) do
  23.     begin
  24.       PropName :=   TStringList(WMIResult[i]).Names[PropNamesIndex];
  25.       PropValue :=   TStringList(WMIResult[i]).ValueFromIndex[PropNamesIndex];
  26.       Memo1.Append(PropName + ' : ' +
  27.               PropValue);
  28.     end;
  29.   end;
  30.   // Clean up
  31.   WMIResult.Free;
  32. end;  
Quote
Manufacturer : FTDI
Caption : USB Serial Port (COM5)
Name : USB Serial Port (COM5)
Description : USB Serial Port
DeviceID : FTDIBUS\VID_0403+PID_6001+6&1FEC562&7&1\0000
================================================
Manufacturer : FTDI
Caption : USB Serial Port (COM6)
Name : USB Serial Port (COM6)
Description : USB Serial Port
DeviceID : FTDIBUS\VID_0403+PID_6001+6&1FEC562&7&2\0000

In attachment in a zip file my utility  utilwmi.

Not sure that it is OK with "Hardware serial ports" (not USB - i can't check) .

Friendly, J.P
« Last Edit: September 14, 2025, 01:18:25 am by Jurassic Pork »
Jurassic computer : Sinclair ZX81 - Zilog Z80A à 3,25 MHz - RAM 1 Ko - ROM 8 Ko

 

TinyPortal © 2005-2018