Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
Operating Systems
»
Windows
»
Enumerate COM ports in Windows with Lazarus
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Application triggers mess...
by
jamie
[
Today
at 12:37:30 am]
Star Wars Galaxian
by
flowCRANE
[
Today
at 12:27:35 am]
Finding List of Fonts & P...
by
jamie
[
Today
at 12:24:25 am]
Fpcupdeluxe
by
ap3000
[August 07, 2026, 11:40:58 pm]
Gtk3 becomes default widg...
by
Bart
[August 07, 2026, 10:45:51 pm]
Create small language
by
DavidL
[August 07, 2026, 07:57:45 pm]
Lazarus for Windows on aa...
by
Martin_fr
[August 07, 2026, 07:41:36 pm]
Unleashed Pascal (async/a...
by
creaothceann
[August 07, 2026, 07:06:23 pm]
New Big Integer library i...
by
ad1mt
[August 07, 2026, 06:42:46 pm]
Identify network devices
by
LemonParty
[August 07, 2026, 03:03:16 pm]
how to connect to mariadb...
by
Mike.Cornflake
[August 07, 2026, 01:30:59 pm]
Automatic generation of *...
by
marcov
[August 07, 2026, 01:12:50 pm]
Multiple CodeAlign
by
marcov
[August 07, 2026, 11:52:54 am]
[SOLVED]SpeedButton Font
by
Petrus Vorster
[August 07, 2026, 10:29:10 am]
[SOLVED] Transparent Butt...
by
Petrus Vorster
[August 07, 2026, 10:05:53 am]
Lazarus undead repository...
by
Mike.Cornflake
[August 07, 2026, 09:44:08 am]
excel and word automation...
by
CM630
[August 07, 2026, 09:06:14 am]
Fast Canvas Library V1.05...
by
Gigatron
[August 07, 2026, 01:53:29 am]
MathParser and CrossGraph...
by
ypisareff
[August 07, 2026, 01:20:50 am]
[SOLVED] synEdit autoComp...
by
Weiss
[August 06, 2026, 09:19:53 pm]
TyControls: a custom-draw...
by
armandoboza
[August 06, 2026, 08:55:43 pm]
Random
by
Paolo
[August 06, 2026, 08:18:11 pm]
Using UTF8
by
ADMGNS
[August 06, 2026, 06:51:50 pm]
TFPHTTPServer extract par...
by
LemonParty
[August 06, 2026, 06:44:05 pm]
Using the "Messages" wind...
by
Martin_fr
[August 06, 2026, 02:20:26 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Enumerate COM ports in Windows with Lazarus (Read 9897 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;
Logged
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
Logged
exdatis
Hero Member
Posts: 668
Re: Enumerate COM ports in Windows with Lazarus
«
Reply #2 on:
August 06, 2012, 08:10:31 am »
Great! Thanks!
Logged
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.
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
Operating Systems
»
Windows
»
Enumerate COM ports in Windows with Lazarus
TinyPortal
© 2005-2018