procedure EnumPorts(PortList:TStringList);
var
MaxPorts:integer;
hPort:THandle;
PortNumber:integer;
PortName:integer;
begin
if PortList=nil then Exit;
case WIN32PlatForm of
VER_PLATFORM_WIN32_NT: MaxPorts:=256;
VER_PLATFORM_WIN32_WINDOWS: MaxPorts:=9;
end;
for PortNumber :=1 to MaxPorts do
begin
if PortNumber>9 then
PortName :='//./COM'+IntToStr(PortNumber)
else
PortName :='COM'+IntToStr(PortNumber);
hPort :=CreateFile(Pchar(PortName),GENERIC_READ or GENERIC_WRITE),0,nil,OPEN_EXISTING,0,0);
if not (hPort=INVALID_HANDLE_VALUE) then
PortList.Add(PortName);
CloseHandle(hPort);
end;
end;