我是用同步方式,线程中接收代码入下:
function TCommBase.ReceiveChar(nByteToReadWORD; var nByteReadWORD):BOOLEAN;
var
cs : ComStat;
statWORD;
RET: BOOL;
begin
if hComm = 0 then
begin
ReceiveChar := false;
Exit;
end;
if PortBusy then
begin
ReceiveChar := FALSE;
Exit;
end;
PortBusy := TRUE;
if ReadFile(hComm,PkBuffTemp[0],nByteToRead,nByteRead,nil) then
Begin
ReportMessage(WM_ERRORENCOUNTED, ERR_READPORT,ord(SendBuff[5]));
ReceiveChar := FALSE;
PortBusy := FALSE;
Exit;
End;
if nByteToRead <> nByteRead then
Begin
ret := PurgeComm(hComm,PURGE_TXCLEAR or PURGE_RXCLEAR);
RET := ClearCommError(hComm,stat,@CS);
PortBusy := FALSE;
ReceiveChar := FALSE;
End else
Begin
PortBusy := FALSE;
ReceiveChar := TRUE;
End;
end;