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
Google announced the kill...
by
GAN
[
Today
at 12:48:59 am]
Build FCP with WinCE cros...
by
dbannon
[
Today
at 12:44:36 am]
CustomDraw package compil...
by
wp
[
Today
at 12:40:56 am]
[SOLVED] Where are my Uni...
by
tfurnivall
[
Today
at 12:05:13 am]
Anyone interested in help...
by
Roland57
[November 06, 2025, 11:49:36 pm]
synedit
by
scasparz
[November 06, 2025, 11:22:06 pm]
32 team NFL schedule for ...
by
TBMan
[November 06, 2025, 10:29:10 pm]
OPCB – Object Pascal Comp...
by
fabianoallex
[November 06, 2025, 09:40:16 pm]
Strange result of text so...
by
PascalDragon
[November 06, 2025, 08:58:36 pm]
Raising an exception with...
by
PascalDragon
[November 06, 2025, 08:29:26 pm]
Recreate RES file
by
Paolo
[November 06, 2025, 08:10:13 pm]
How detect a keypress in ...
by
440bx
[November 06, 2025, 07:21:11 pm]
Code improvement, suggest...
by
cris75
[November 06, 2025, 07:17:19 pm]
TArrayHelper
by
Thaddy
[November 06, 2025, 07:10:33 pm]
TLazSerial : serial port ...
by
mas steindorff
[November 06, 2025, 07:03:25 pm]
Lazarus portable?
by
marcov
[November 06, 2025, 06:06:20 pm]
Text file and Timer
by
Ilya83
[November 06, 2025, 05:15:26 pm]
Searching character by ch...
by
Zvoni
[November 06, 2025, 05:07:44 pm]
CopyDirTree and empty dir...
by
Bart
[November 06, 2025, 03:44:55 pm]
Extract path from active ...
by
LemonParty
[November 06, 2025, 09:41:26 am]
Back arrow in App -> firs...
by
nicelybrewed
[November 06, 2025, 08:17:10 am]
What are some good modern...
by
mamta25
[November 06, 2025, 07:41:39 am]
Debugger freezes the PC
by
simsee
[November 05, 2025, 11:54:14 pm]
Procedure that undestand ...
by
n7800
[November 05, 2025, 10:15:54 pm]
Senior Lazarus / Free Pas...
by
lazdev
[November 05, 2025, 10:14:57 pm]
« previous
next »
Print
Pages: [
1
]
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;
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