API串口通信,接收时,第八位丢失
吾用API编写的串口通信程序,用的是单串口发送与接收。在没有修改任何代码的情况下,第八位接收不正常时有时无。
请高手赐教。感激不尽。
procedure TForm_main.Button6Click(Sender: TObject);
var wLen,len,dwError,dwReLen:cardinal;
strSend:string;
lbWR:longBool;
pStat:ComStat;
i:integer;
comHandle:cardinal;
begin
strSend:='ad我来了,and you.';
wLen:=length(strSend);
for i:=1 to wLen do
wBf[i]:=strSend[i];
showMessage(wBf);
//打开串口
comHandle:=createFile('com1',GENERIC_READ+GENERIC_WRITE,0,nil,OPEN_EXISTING,FILE_FLAG_OVERLAPPED,0);
setupComm(comHandle,1000,1000);
PurgeComm(comHandle,PURGE_TXCLEAR and PURGE_TXABORT and PURGE_RXCLEAR and PURGE_RXABORT);
wOL.Offset:=0;
wOL.OffsetHigh:=0;
//写串口
lbWR:=writeFile(comHandle,wBf,wLen,len,wOL{lpOl});
if not lbWR then
begin
if GetLastError=ERROR_IO_PENDING then
begin
GetOverlappedResult(comHandle,wOL^,len,true);
end else
begin
showmessage('写串口出错,代码:'+IntToStr(GetLastError));
exit;
end;
end;
clearCommError(comHandle,dwError,@pStat);
dwReLen:=pStat.cbInQue;
len:=0;
if dwReLen>0 then
begin
rOL.Offset:=0;
rOL.OffsetHigh:=0;
//读串口
lbWR:=ReadFile(ComHandle,rBf,wLen,len,rOL);
if lbWR=false then
begin
if GetLastError=ERROR_IO_PENDING then
begin
GetOverlappedResult(comHandle,rOL^,len,true);
end;
end;
end;
strSend:='';
for i:=1 to len do
strSend:=strSend+rBf[i];
showMessage(strSend);
end;