非常郁闷的问题,读取串口时第一条读取很正常,以后读取总是多一些上次读取的字符,而且多余的数据越来越多,比如我先发送at,接到ok,我在发送at,可能会接到okok等等,用PurgeComm( hCom, PURGE_RXABORT + PURGE_RXCLEAR )清空缓冲区也不解决问题;而且发送缓冲区我也清空了也不行,哪位大虾看看问题出在哪里了??同步方式读写,我是发送一条指令,在接收指令,其中ReadCom函数中的BufLen参数我是调用接收缓冲区函数InputCountfunction WriteCom(Buf:Pointer; BufLen:Dword) : boolean; //发送
begin
...
WriteFile(hCom,Buf^,BufLen,txLen,Nil);
PurgeComm(hCom, PURGE_TXABORT + PURGE_TXCLEAR);
...
end;function ReadCom(Buf:Pointer; BufLen :Dword) : boolean; //接收
begin
...
ReadFile(hCom,Buf^,BufLen,rxLen,Nil);
PurgeComm( hCom, PURGE_RXABORT + PURGE_RXCLEAR );
...
end;function InputCount: Integer; //接收缓冲区大小
var
dErr: Dword;
CommStat:TComStat;
begin
if not ClearCommError(hCom, dErr, @CommStat) then
raise EComPort.Create(CError_ClearComFailed, GetLastError);
Result := commstat.cbInQue;
end;
begin
...
WriteFile(hCom,Buf^,BufLen,txLen,Nil);
PurgeComm(hCom, PURGE_TXABORT + PURGE_TXCLEAR);
...
end;function ReadCom(Buf:Pointer; BufLen :Dword) : boolean; //接收
begin
...
ReadFile(hCom,Buf^,BufLen,rxLen,Nil);
PurgeComm( hCom, PURGE_RXABORT + PURGE_RXCLEAR );
...
end;function InputCount: Integer; //接收缓冲区大小
var
dErr: Dword;
CommStat:TComStat;
begin
if not ClearCommError(hCom, dErr, @CommStat) then
raise EComPort.Create(CError_ClearComFailed, GetLastError);
Result := commstat.cbInQue;
end;
试下:
PurgeComm(CommHandle,(PURGE_RXABORT and PURGE_RXCLEAR));
PURGE_RXABORT + PURGE_RXCLEAR
和
PURGE_RXABORT and PURGE_RXCLEAR
是不一样的!