我的程序步骤是,每隔一定时间发送一个命令,发送完一个命令后等进入等待状态,
下位机发一确认命令后,不是确认命令则开计时器。如是通过命令,则我发一个取数命令,
接完数据后,我再把计时器打开,是这样循环的,不知有没错。
FindOrder[1] :=$22;FindOrder[2]:=$23;FindOrder[3]:=$24;FindOrder[4]:=$25;
NowOrderIndex:=1;
procedure Tfrm_test.Comm2ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
CommFlag:boolean;
i:integer;
str:string;
begin
commflag:=true;
str:='';
move(buffer^,pchar(@ReceiveData)^,BufferLength);
for i:=1 to bufferlength do str:=str+inttohex(receivedata,1);
image1.Canvas.TextOut(10,y,str);
if y>270 then
begin
y:=10;
image1.Canvas.brush.color:=$00FEB483;
image1.Canvas.FillRect(image1.clientRect);
end
else y:=y+15;
application.ProcessMessages;
if receivedata[1] = $A3 then
begin
sbuf[1]:=$7f;
if comm2.WriteCommData(@sbuf[1],1) = false then commflag := false;
sleep(2);
if not commflag then MessageDlg('发送出错信息出信,请检查通信线路',mterror,[mbYes],0);
end
else
begin
sbuf[1]:=rightOrder;
if comm2.WriteCommData(@sbuf[1],1) = false then commflag := false;
sleep(2);
if not commflag then MessageDlg('发送出错信息出信,请检查通信线路',mterror,[mbYes],0);
timer3.Enabled:=true;
end;
end;
procedure Tfrm_test.Comm2ReceiveError(Sender: TObject;
EventMask: Cardinal);
begin
try
case EventMask of
CE_BREAK: showmessage('CE_BREAK');
CE_DNS: showmessage('CE_DNS');
CE_FRAME: dealError;
CE_IOE: showmessage('CE_IOE');
CE_MODE: showmessage('CE_MODE');
CE_OOP: showmessage('CE_OOP');
CE_OVERRUN: showmessage('CE_OVERRUN');
CE_PTO: showmessage('CE_PTO');
CE_RXOVER: showmessage('CE_RXOVER');
CE_RXPARITY:BEGIN
DealError;
END;
CE_TXFULL: showmessage('CE_TXTULL');
end;
except showmessage(' deal error miss the error');
end;
end;
procedure Tfrm_test.Timer3Timer(Sender: TObject);
var
commflag:Boolean;
begin
commflag:=true;
timer3.Enabled := false;
OrderNum :=1;
sbuf[1]:= findOrder[NowOrderIndex];
image1.canvas.TextOut(0,0,inttostr(noworderIndex));
application.ProcessMessages;
if NowOrderIndex = 4 then NowOrderIndex:=1
else NowOrderIndex := NowOrderIndex+1;
if comm2.WriteCommData(@sbuf[1],1) = false then commflag := false;
sleep(2);
if not commflag then MessageDlg('发送出错信息出信,请检查通信线路',mterror,[mbYes],0);
end;