Re: Reading names of comports [COM1, COM2 etc]



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

.



Relevant Pages

  • Re: Help!! USB not working!! Any ideas??
    ... exclamation mark has also appeared, ... I've run several Registry Mechanic scans, ... The computer has 8 USB ports, ... I connect my digital camera, a drive appears to show it is connected, ...
    (microsoft.public.windowsxp.general)
  • Re: HELP - DUPLICATE COM & LPT PORTS
    ... System Restore is the preferred way to remove those registry entries. ... the place the install added the ports, ... > Faxes) and click on PORTS I see 2 each of all the following ports: ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: Nothing Present in Device Manager!!!
    ... as I can't get my scanner to be ... and the problem seems to specifically affect my com ports. ... I'd also like to know more about this "export key from registry" business. ... During the cleaning of those types of systems, ...
    (microsoft.public.windowsxp.general)
  • Re: Ethernet problem...
    ... Finally i got both ethernet ports working.. ... I have added the registry entry, which cook mentioned, but nothing happened. ...
    (microsoft.public.windowsce.platbuilder)
  • Re: OWA Setup - HELP PLS
    ... > running Exchange Server 2000...all of these articles ... >>modifying the registry. ... >>is a firewall between OWA and the Exchange Server ... two ports need to be statically mapped. ...
    (microsoft.public.exchange2000.setup.installation)