Recent

Author Topic: Enumerate COM ports in Windows with Lazarus  (Read 9574 times)

padiazg

  • Newbie
  • Posts: 2
Enumerate COM ports in Windows with Lazarus
« on: April 13, 2012, 04:26:38 pm »
Full history here: http://patotech.blogspot.com/2012/04/enumerate-com-ports-in-windows-with.html

As I have only USB ports on my development machine, is quite common at connecting a serial device that I have to go to the device manager to see in which COM port it was installed.

 I found this page: http://www.lazarus.freepascal.org/index.php?topic=14313.0 which publishes three interesting features, but none of them fullfills my needs.

So I came up with this:

function GetSerialPortNamesExt: string;
var
  reg  : TRegistry;
  l,v  : TStringList;
  n    : integer;
  pn,fn: string;
 
  function findFriendlyName(key: string; port: string): string;
  var
    r : TRegistry;
    k : TStringList;
    i : Integer;
    ck: string;
    rs: string;
  begin
    r := TRegistry.Create;
    k := TStringList.Create;
 
    r.RootKey := HKEY_LOCAL_MACHINE;
    r.OpenKeyReadOnly(key);
    r.GetKeyNames(k);
    r.CloseKey;
 
    try
      for i := 0 to k.Count - 1 do
      begin
        ck := key + k + '\'; // current key
        // looking for "PortName" stringvalue in "Device Parameters" subkey
        if r.OpenKeyReadOnly(ck + 'Device Parameters') then
        begin
          if r.ReadString('PortName') = port then
          begin
            //Memo1.Lines.Add('--> ' + ck);
            r.CloseKey;
            r.OpenKeyReadOnly(ck);
            rs := r.ReadString('FriendlyName');
            Break;
          end // if r.ReadString('PortName') = port ...
        end  // if r.OpenKeyReadOnly(ck + 'Device Parameters') ...
        // keep looking on subkeys for "PortName"
        else // if not r.OpenKeyReadOnly(ck + 'Device Parameters') ...
        begin
          if r.OpenKeyReadOnly(ck) and r.HasSubKeys then
          begin
            rs := findFriendlyName(ck, port);
            if rs <> '' then Break;
          end; // if not (r.OpenKeyReadOnly(ck) and r.HasSubKeys) ...
        end; // if not r.OpenKeyReadOnly(ck + 'Device Parameters') ...
      end; // for i := 0 to k.Count - 1 ...
      result := rs;
    finally
      r.Free;
      k.Free;
    end; // try ...
  end; // function findFriendlyName ...
 
begin
  v      := TStringList.Create;
  l      := TStringList.Create;
  reg    := TRegistry.Create;
  Result := '';
 
  try
    reg.RootKey := HKEY_LOCAL_MACHINE;
    if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') then
    begin
      reg.GetValueNames(l);
 
      for n := 0 to l.Count - 1 do
      begin
        pn := reg.ReadString(l[n]);
        fn := findFriendlyName('\System\CurrentControlSet\Enum\', pn);
        v.Add(pn + ' = '+ fn);
      end; // for n := 0 to l.Count - 1 ...
 
      Result := v.CommaText;
    end; // if reg.OpenKeyReadOnly('HARDWARE\DEVICEMAP\SERIALCOMM') ...
  finally
    reg.Free;
    v.Free;
  end; // try ...
end;

seaton

  • New Member
  • *
  • Posts: 48
Re: Enumerate COM ports in Windows with Lazarus
« Reply #1 on: August 06, 2012, 07:48:12 am »
many thanks just what I was after for my current project

exdatis

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Enumerate COM ports in Windows with Lazarus
« Reply #2 on: August 06, 2012, 08:10:31 am »
Great! Thanks!

picstart

  • Full Member
  • ***
  • Posts: 236
Re: Enumerate COM ports in Windows with Lazarus
« Reply #3 on: August 06, 2012, 09:35:48 am »
I had to change  ck := key + k + '\'; // current key
to 
Code: [Select]
ck := key + k[i] + '\'; // current key to get it to work

win7 pro 32bit laz 1ORC1 FPC 2.6.

 

TinyPortal © 2005-2018