Home > Back-end >  Delphi mscomm control receive timeout interrupt, then send instruction does not perform, please help
Delphi mscomm control receive timeout interrupt, then send instruction does not perform, please help

Time:09-30

The unit Unit1;

Interface

USES the
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, RzButton StdCtrls, RzEdit OleCtrls, MSCommLib_TLB, ExtCtrls, dateutils,
Buttons.
Type
Treceivetext=record
Amount: integer;
Rectext: string;
end;
Type Tinstruction=record
Com: string;
Retextlength: integer;
Runtimes: integer;
end;
Type
TForm1=class (TForm)
MSComm1: TMSComm;
RzMemo1: TRzMemo;
RzButton1: TRzButton;
Procedure FormCreate (Sender: TObject);
Procedure RzButton1Click (Sender: TObject);
Procedure MSComm1Comm (Sender: TObject);
Private
{Private declarations}

Currcom commtemp, lastcom: string;
Receivetextlength: integer;

Public
{Public declarations}
Cominfo: Tinstruction;
Reclist: tstringlist;
Procedure sendcom (com: string; Rectxtlength: integer);

end;

Var
Form1: TForm1;
Sendok receiveok, linkinterrupt: Boolean;
Receivetext: treceivetext;
Readrecordcount dalayMS, Trytoconnect stateinfo: INTEGER;
Currjh: string;

Implementation

{$R *. DFM}

Procedure TForm1. FormCreate (Sender: TObject);
The begin
Trytoconnect:=3;
end;

Procedure TForm1. Sendcom (com: string; Rectxtlength: integer);
The begin
Mscomm1. OutBufferCount:=0;
Mscomm1. InBufferCount:=0;
MSComm1. SThreshold:=1;
MSComm1. RThreshold:=rectxtlength;
MSComm1. Output:=com;
end;

Procedure TForm1. RzButton1Click (Sender: TObject);
Var firsttickcount: real;
Temp: string;
I: integer;
The begin

Reclist:=tstringlist. Create;
MSCOMM1.Com mPort:=9;
MSComm1. InBufferSize:=2048;
MSComm1. OutBufferSize:=256;
MSComm1. Settings:='9600, n, 8, 1;
MSComm1. InputLen:=0;
MSComm1. InBufferCount:=0;
MSComm1. OutBufferCount:=0;
MSComm1. RThreshold:=3;
MSComm1. An InputMode:=comInputModeText;
Mscomm1. SThreshold:=0;
Mscomm1. PortOpen:=true;
Mscomm1. InitControlData;

Currjh:='01';
Readrecordcount:=10;



Currjh currcom:='SA' + + '! ';
Cominfo.com:=currcom;
Cominfo. Retextlength:=11;
Sendcom (cominfo.com, cominfo retextlength);
Cominfo. Runtimes:=0;
Rzmemo1. Lines. The Add (' execution '+ cominfo.com);


Stateinfo:=0;
Reclist. The Clear;
I:=0;

Repeat
Application. ProcessMessages;
Inc (I);
If (stateinfo=1) or (stateinfo=1) then
The begin
If reclist. Count & gt; 0 then
Rzmemo1. Lines. AddStrings (reclist);
end;
Rzmemo1. Lines. The add (' I:='+ inttostr (I));
Until (stateinfo=1) or (stateinfo=1);

If mscomm1. PortOpen then
The begin
Mscomm1. OutBufferCount:=0;
Rzmemo1. Lines. The Add (' finished! ');
Mscomm1. PortOpen:=false;
end;
Reclist. The Clear;

end;

Procedure TForm1. MSComm1Comm (Sender: TObject);
Var STR, temp, tempstr, XFSJSTR: string;
Num, je: string;
I, reclength: integer;
Recno, amount, realgetrec tempamount: integer;
Readeof: Boolean;
Firsttickcount: real;
The begin
If linkinterrupt then EXIT.
RZMEMO1. LineS. The Add (' MSComm1.Com mEvent: + inttostr mEvent) (MSComm1.Com).
Case mEvent of MSComm1.Com
ComEvReceive:
The begin
Application. ProcessMessages;
Receiveok:=TRUE;
Temp:=mscomm1. Input;
If temp="' then the exit;
RZMEMO1. LineS. The Add (CURRCOM + + TEMP ':');
If currcom='SK + currjh +'! 'then
The begin
If temp='E000000001! 'then
The begin
Sleep (1000);
If cominfo. Runtimes & lt; Trytoconnect then
The begin
Sendcom (cominfo.com, cominfo retextlength);
End
The else
The begin
Stateinfo:=1;
end;
End
The else
If temp='E000000002! 'then
The begin
Stateinfo:=1;
End
The else
If LENGTH (trim (temp))=11 then
The begin
Tempstr:=temp;
STR:=copy (tempstr, 1, 5);
Tempstr=copy (tempstr, 6, 5);
Recno:=strtoint (STR);
Amount:=strtoint (tempstr);
If the amount & gt; Readrecordcount then
Realgetrec:=Readrecordcount
The else
Realgetrec:=amount;
Tempstr=format (' %. 2 d '[realgetrec]);
Commtemp:=STR + tempstr;
Commtemp currcom:='DA' + + '! ';
Cominfo.com:=currcom;
Cominfo. Retextlength:=realgetrec * 40;
Sendcom (cominfo.com, cominfo retextlength);
Cominfo. Runtimes:=0;

End
The else
The begin
Showmessage (temp);
If cominfo. Runtimes & lt; Trytoconnect then
The begin
Sendcom (cominfo.com, cominfo retextlength);
End
The else
The begin
Stateinfo:=1;
end;
end;
End
The else
If currcom='DA' + commtemp + '! 'then
The begin
If length (temp)=MSComm1. RThreshold then
The begin
Currcom:='OK! ';
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related