Re: Reading names of comports [COM1, COM2 etc]
- From: alanglloyd@xxxxxxx
- Date: 6 Aug 2005 14:44:01 -0700
I think Serial0, Serial1, etc are Win NT entries, while Com1, Com2, etc
are Win 9x entries in the registry.
Additionally both RS232 _and_ modem virtual ports will be in the
registry.
I found that looking in the registry was a slowish method, I opened the
COM ports one by one and checked which ones were working and were
RS232.
My oldish & tatty code is as follows (don't use it now as all my
devices are USB).
procedure TPortsList.GetAvailableCommPorts(ComList : TStringList);
{puts the COM ports into a list. available ports have the stringlist
objects
set to a non-nil value. to be available the ports must be a hardware
port (in
the registry list of comm ports) and have a ProviderSubType of
PST_RS232}
var
CommSL : TStringList;
CommName : string;
hComm : THandle;
PtrCommConfig : PCommConfig;
i, j, CommConfigSize : integer;
Available : boolean;
begin
CommSL := TStringList.Create;
// AGL 14/12/00
with CommSL do begin
Sorted := true;
Duplicates := dupIgnore;
end;
// AGL end
(* GetCommNames(CommSL);*)
// inserting the list in CommSL cuts the time from 840mS to 11mS
with CommSL do begin
Clear;
Add('COM1');
Add('COM2');
Add('COM3');
Add('COM4');
end;
{CommSL now contains the list of commports from the registry}
for i := 0 to 1{CommSL.Count - 1} do begin
CommName := CommSL.Strings[i]; {Format('COM%d', [i]);}
{open the port as a file}
hComm := CreateFile(PChar(CommName), GENERIC_READ or GENERIC_WRITE,
0, nil, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL, 0);
if hComm <> INVALID_HANDLE_VALUE then begin
{its a useable COM port - check if its an RS232 type}
CommConfigSize := SizeOf(TCommConfig);
PtrCommConfig := AllocMem(CommConfigSize);
if not GetCommConfig(hComm, PtrCommConfig^, CommConfigSize) then
begin
{not enough memory - get what's needed}
ReAllocMem(PtrCommConfig, CommConfigSize);
GetCommConfig(hComm, PtrCommConfig^, CommConfigSize);
end;
Available := (PtrCommConfig^.dwProviderSubType = PST_RS232);
FreeMem(PtrCommConfig);
end {if hComm <> INVALID_HANDLE_VALUE}
else
Available := false;
{end; if hComm <> INVALID_HANDLE_VALUE else}
CloseHandle(hComm);
//AGL 14/12/00
{set commport as available in list}
with ComList do begin
j := IndexOf(CommName);
if j > -1 then
Objects[j] := pointer(Available);
end; {with ComList}
// ComList.AddObject(CommName, pointer((Available)));
// AGL end
end; {for i := 0 to CommSL.Count - 1}
CommSL.Free;
end;
Alan Lloyd
.
- References:
- Reading names of comports [COM1, COM2 etc]
- From: Vic Fraenckel
- Reading names of comports [COM1, COM2 etc]
- Prev by Date: Re: Reading names of comports [COM1, COM2 etc]
- Next by Date: Re: Reading names of comports [COM1, COM2 etc]
- Previous by thread: Re: Reading names of comports [COM1, COM2 etc]
- Next by thread: Re: Reading names of comports [COM1, COM2 etc]
- Index(es):
Relevant Pages
|