(******************************************************************** ** ** GetAvailableComPorts - ** ** This is a procedure whose purpose is to determine ** which COM ports are available on the user's computer. It then ** fills up the PortsComboBox with those available COM port numbers. ** ********************************************************************)
procedure TJeffsForm.GetAvailableComPorts(); var i : Integer; S : String; ComName: array[0..9] of Char; TheComHandle: Integer;
begin
(* ** Okay, Clear everything in the PortsComboBox because ** we want to start off fresh. *)
PortsComboBox.Clear();
(* ** Windows 95/98 can only handle up to 50 COM ports. ** All we are doing here is checking to see if Windows ** "can" open up the COM port. If so, then we know it's ** available. And so we add it to our ComboBox list ** and of course close the COM port each time we check it. *)
for i := 1 to 50 do begin StrFmt( ComName, '\\.\COM%d', [i] ); TheComHandle := CreateFile ( ComName, // name GENERIC_READ or GENERIC_WRITE, // access attributes 0, // no sharing nil, // no security OPEN_EXISTING, // creation action FILE_ATTRIBUTE_NORMAL or FILE_FLAG_OVERLAPPED, // attributes 0 // no template ); if ( TheComHandle <> INVALID_HANDLE_VALUE ) then begin S := Format('COM%d', [i]); PortsComboBox.Items.Add(S); end; CloseHandle( TheComHandle ); end; {end of for loop}
end;
| |