我用的是spcomm控件。我现在是用这种方法:if ComPortAvailable('COM1:') then
begin
//ShowMessage('com1口可以使用.!');
end
else
begin
try
comm1.CommName:='com1';
comm1.StopComm;
sleep(500);
comm1.StartComm;
Lblopencom.Caption:='当前使用的串口为:com1';
exit;
except
showmessage('不存在串口1或被占用。');
end;
end;所用的函数:
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;上述的程序最大的一个问题是不能区分该串口是被占用,还是不存在。请高手指点,谢谢。
begin
//ShowMessage('com1口可以使用.!');
end
else
begin
try
comm1.CommName:='com1';
comm1.StopComm;
sleep(500);
comm1.StartComm;
Lblopencom.Caption:='当前使用的串口为:com1';
exit;
except
showmessage('不存在串口1或被占用。');
end;
end;所用的函数:
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;上述的程序最大的一个问题是不能区分该串口是被占用,还是不存在。请高手指点,谢谢。
注册表方法不行,里面有的,不一定表示就真的有,没有的也不表示真的没有
用别的程序应该关不了的!