访问量: 29019次,访客数: 24501人,浏览量: 1次 
首页  编辑  

端口是否有效

Tags: /超级猛料/Hardware.硬件相关/其它硬件/   Date Created: Wed Aug 14 2002 05:33:35 GMT+0000 (Coordinated Universal Time)

function ComPortAvailable(Port: PChar): Boolean;

var

 DeviceName: array[0..80] of Char;

 ComFile: THandle;

begin

 StrPCopy(DeviceName, Port);

 ComFile := CreateFile(DeviceName, GENERIC_READ or GENERIC_WRITE, 0, nil,

   OPEN_EXISTING,

   FILE_ATTRIBUTE_NORMAL, 0);

 Result := ComFile <> INVALID_HANDLE_VALUE;

 CloseHandle(ComFile);

end;

procedure TForm1.Button1Click(Sender: TObject);

begin

 if ComPortAvailable('COM1:') then

   ShowMessage('Port available')

 else

   ShowMessage('Port not available');

end;