Home > Back-end >  About the serial port communication of memory management
About the serial port communication of memory management

Time:09-28

The younger brother spccom in use, has been process has no exit, a few hours and the function of serial port communication does not perform, then there is no this phenomenon, such as the event log is as follows:

Error application myplc. Exe version 0.0.0.0, error module kernel32. DLL, version 5.2.3790.3959, wrong address 0 x0000bee7,

Doubt whether there is internal leakage problem, consult, a serial port of the buffer zone, whether to free, again into the next to send and receive, or should be how to better handle, my code is as follows:
The function Tform1. Senddata publishes the event () : Integer;
Var
I: integer;
Commflg: Boolean;

The begin
Viewstring:=';
Commflg:=true;
For I:=1 to 8 do

The begin
If not Comm1. WriteCommData (@ sbuf [I], 1) then
The begin
Commflg:=false;
break;
The end;
sleep(2);
Viewstring:=viewstring + inttohex (sbuf [I], 2);

The end;

Result:=1;
If not commflg then
Result:=0;
If the N8. Checked then statusbar1. Panels [0]. Text:='send:' + viewstring;
//memo1. Lines. The Add (+ viewstring send:);
Delay (2000);
The end;


Procedure TForm1.Com m1ReceiveData (Sender: TObject; Buffer: Pointer;
BufferLength: Word);

Var
I: integer;
The begin
Viewstring:=';
Move (buffer ^, pchar (@ rbuf) ^, bufferlength);
For I:=1 to bufferlength do
Viewstring:=viewstring + inttohex (rbuf [I], 2);
Gotdevid:=rbuf [1].//received coding equipment, take the first data content by default, namely the former two bytes
Gotregid:=sbuf [4];//read the register address, directly take 4
Statusbar1. Panels [2]. Text:='received data: + viewstring +' check '+ booltostr (mycrc. Iscrcright (viewstring)) +' device address: + inttostr (gotdevid) + ', register: '+ inttostr (gotregid) +', insert data: '+ inttostr (processdata (viewstring)) +' a ';
//memo1. Lines. The Add (+ viewstring 'received data:' + ', check '+ booltostr (mycrc. Iscrcright (viewstring)) +' device address: + inttostr (gotdevid) + ', register: '+ inttostr (gotregid));
The end;


Procedure TForm1. Timer1Timer (Sender: TObject);//sent to put sbuf array data
The begin
Sbuf [1] :=byte ($01);
Sbuf [2] :=byte ($3);
Sbuf [3] :=byte ($00);
Sbuf [4] :=byte ($3);
Sbuf [5] :=byte ($00);
Sbuf [6] :=byte ($01);
Sbuf [7] :=byte ($74);
Sbuf [8] :=byte ($0 a);
Senddata publishes the event;

Sbuf [1] :=byte ($01);
Sbuf [2] :=byte ($3);
Sbuf [3] :=byte ($00);
Sbuf [4] :=byte ($4);
Sbuf [5] :=byte ($00);
Sbuf [6] :=byte ($01);
Sbuf [7] :=byte ($C5);
Sbuf [8] :=byte ($CB);
Senddata publishes the event;

The end;

CodePudding user response:

Buffer to remove ah, every time to send the clearance of the sending and receiving buffer, not Free is clear

CodePudding user response:

Buffer to remove ah, every time to send the clearance of sending and receiving buffer

CodePudding user response:

If the buffer is a dynamic application, best wishes must be released

CodePudding user response:

To ask how to clear the buffer
Dynamic array and how to release the memory?

CodePudding user response:

Useless SPCOMM, has been using CPORT, component packaging function more complete,
Components such as: CPORT buffer is static, there are two attributes can be set respectively to send and receive buffer size, and CelarBuffer method can be invoked, convenient,
Feel spcomm just simple API for the packaging, many convenient function remains to be themselves
  • Related