Enumerating a List of systems Com Ports in Delphi
-
03-07-2019 - |
Question
Objective: I would like to be able to list the available COM Ports on a system in Delphi.
Homework:
I have read this SO thread on enumerating the LPT ports of a system using the registry. I have also found that the COM ports are listed in the registry at HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
but found unanswered gesticulation in the same thread that this might not be reliable on different machines and different versions of windows.
I also found articles referencing the use of QueryDosDevice() but upon trying this sample code, I found that it did not appear to list any COM ports at all.
Question: What is the most reliable way (across unknown Windows Versions) to list the COM ports on a Windows Machine?
Solution
DEVICEMAP\SERIALCOMM is good for all NT versions. You'll probably need to look under DYN_DATA for Win9x.
Use this method if you need runtime reliability.
OTHER TIPS
Please go through URL which is written in C++
http://www.codeproject.com/KB/system/serial_portsenum_fifo.aspx
and same approach can be implemented in delphi too.. or somebody can convert for you in SO..
This will work for all windows versions since this works from the principle of device manager which is available for all window versions.
This is code for LINUX not for WINDOWS....
function GetSerialPortNames: string;
var
Index: Integer;
Data: string;
TmpPorts: String;
sr : TSearchRec;
begin
try
TmpPorts := '';
if FindFirst('/dev/ttyS*', $FFFFFFFF, sr) = 0 then
begin
repeat
if (sr.Attr and $FFFFFFFF) = Sr.Attr then
begin
data := sr.Name;
index := length(data);
while (index > 1) and (data[index] <> '/') do
index := index - 1;
TmpPorts := TmpPorts + ' ' + copy(data, 1, index + 1);
end;
until FindNext(sr) <> 0;
end;
FindClose(sr);
finally
Result:=TmpPorts;
end;
end;