hcom=0时就代表hcom表示的端口已经关闭了,所以你的代码可改为: if(hcom=0) then exit; 如果想重新打开一个端口,可以使用Windows API的CreateFile函数来实现。如: hcom=CreateFile(pchar(com1),GENERIC_READ and GENERIC_WRITE,0,nil,OPEN_EXISTING,0,0); //打开以读写的方式com1串口
可是但串口有值时我想把它关闭为什么不行呢? if(hcom<>0) then CloseHandle(hcom);<-------有时会出错
hcom 是全局变量吗?if (hcom <> 0) then try CloseHandel(hcom); except end;
if(hcom=0) then
exit;
如果想重新打开一个端口,可以使用Windows API的CreateFile函数来实现。如:
hcom=CreateFile(pchar(com1),GENERIC_READ and GENERIC_WRITE,0,nil,OPEN_EXISTING,0,0); //打开以读写的方式com1串口
if(hcom<>0) then
CloseHandle(hcom);<-------有时会出错
try
CloseHandel(hcom);
except
end;