Home > Back-end >  Delphi multiple event procedure call the same function
Delphi multiple event procedure call the same function

Time:09-29

Would like to ask a question, regarding the Delphi programming, if in the process of multiple events, all call the same function of the user to write your own words, if the first event is invoked once function1 function, then there are the second event is triggered, then turned to the second event handling process, if the second event process call once function1 function, calculated by the once function1 function is the result of the wrong, if there are multiple events, but they may have a function called the first event are not out of this function, the second event, and will call this function, what's the solution? Same functionality or said, you need to write more than once function1 function, different names, different events call names?

Similar to the following code:

CodePudding user response:

The code is:

//x x x x x x x x x x x x x x x x x x x x x x x x x x
//used to compute CRC16 CalCRC16 check code
//polynomial equation for X16 + X15 + X2 + 1
//x x x x x x x x x x x x x x x x x x x x x x x x x x
The function CRC16 (AData: an array of Byte; AStart, AEnd: Integer) : Word;
Const
GENP=$A001;//polynomial equation X15 X16 + + X2 + 1 (1100, 0000, 0000, 0101)
Var
CRC: Word;
I: Integer;
TMP: Byte;

Procedure CalOneByte (AByte: Byte);//calculate the check code 1 byte
Var
J: Integer;
The begin
CRC:=CRC xor AByte;//the data and CRC register low eight different or
For j:=0 to 7 do//to check every
The begin
TMP:=CRC and 1;//remove the lowest
The CRC: CRC SHR=1;//register to the right to remove a
CRC:=CRC and $7 FFF;//will be the highest position 0
If TMP=1 then//testing out, if is 1, then with polynomial exclusive or
CRC:=CRC xor GENP;
CRC:=CRC and $FFFF;
end;
end;
The begin
CRC:=$FFFF;//set the remainder to FFFF
For I:=AStart to AEnd do//to check every byte
CalOneByte (AData [I]);
Result:=CRC;


end;


//this process for the timer timing 3 s trigger events, events of CRC16 function called
Procedure TForm1. Timer1Timer (Sender: TObject);
Var
Incrc: WORD;

The begin

Incrc:=CRC16 (txBuffer, LOW (txBuffer), 4-1);

end;

//button 1 event trigger function: call CRC16 function inside the
Procedure TForm1. Button1Click (Sender: TObject);
Var
SanF: WORD;
The begin
SanF:=CRC16 (rxBuffer, LOW (txBuffer), 4-1);
end;


Such code

CodePudding user response:

System for ontimer and buttonclick message sorting, crc16 operations here are not concurrent, always can tell successively

Unless in the thread of execution, thread safety must be considered

CodePudding user response:

But if the crc16 function called ontimer news are not quit, triggered the buttonclick event, in the event of an buttonclick again call crc16 function, calculation of the value is wrong, what is the problem?

CodePudding user response:

In the context of your code, the Timer event is triggered during execution, is not response Btuuon. Click, so you don't have a weight of this code into the problem, it must be somewhere else having some problems, such as txBuffer where it is processed, how to deal with, etc.

CodePudding user response:

Oh, I wrote the program, the code above is just a point code,
Program, SPCOMM serial port control, timer control, timer trigger every 3 s events, enter Timer1Timer process using SPCOMM send () function, through the serial port will be issued data; Under a machine at the same time also can send data to the serial port, serial port to receive the data, the SPCOMM will enter Comm1ReceiveData () in the process, this time is called CRC function, but the result of calculation is wrong,
The code is as follows:

The function Managemessage (mbufferlength: WORD) : WORD;


Var
I, j: integer;
Government: shortint;

Test1 and test2: integer;

Crccompare1 crccompare2: WORD;

Aa: longint;

The begin


Crccompare2:=rxBuffer [mbufferlength - 3];//rxBuffer [] in the last few low byte CRC byte data bits, CRC high byte, $0. A, $0 d
Crccompare2:=(crccompare2 SHL 8) or rxBuffer [mbufferlength - 4];

Crccompare1:=CRC16 (rxBuffer, LOW (rxBuffer), mbufferlength - 4-1);

If crccompare1 & lt;> Crccompare2 then exit (0);

end;
Procedure TForm1. Timer1Timer (Sender: TObject);
Var
Incrc: WORD;

The begin
TxBUFFER [0] :=DeviceAddr;

TxBUFFER [1] :=byte ($01); {order number}

00 txBUFFER [2] :=byte ($).

TxBUFFER [3] :=byte ($5);


Incrc:=CRC16 (txBuffer, LOW (txBuffer), 4-1);

TxBuffer [4] :=byte (incrc);
TxBuffer [5] :=byte (incrc SHR 8);

Senddata publishes the event (6-1); Send function called {}
end;

//receiving data event handling
Procedure TForm1.Com m1ReceiveData (Sender: TObject; Buffer: Pointer;
BufferLength: Word);

Var

ForDisplay: string [11];
The begin


Move (buffer ^, pansichar ^ @ rxBUFFER, bufferlength);
If Managemessage (bufferlength) & lt;> 0 then
Showmessage (' data reception success! ');

end;

CodePudding user response:

Ever know, help solve, don't need to write a lot of the same function, different name function for the call?

CodePudding user response:

The same event handling thread points successively, treatment before the incident to the next event,

CodePudding user response:

As long as you are not a multithreaded program, event function will not be a haven't performed the next call to come over.

CodePudding user response:

Process, function performs within the thread, have the order,

Unless you are using multithreading, would need to consider security between threads,

CodePudding user response:

The function CRC16 (AData: an array of Byte; AStart, AEnd: Integer) : Word;

Procedure CalOneByte (AByte: Byte);

Procedure TForm1. Timer1Timer (Sender: TObject);
Var
Incrc: WORD;

The begin

Incrc:=CRC16 (txBuffer, LOW (txBuffer), 4-1);

end;

Oh, I wrote the program, the code above is just a point code,
Program, SPCOMM serial port control, timer control, timer trigger every 3 s events, enter Timer1Timer process using SPCOMM send () function, through the serial port will be issued data; Under a machine at the same time also can send data to the serial port, serial port to receive the data, the SPCOMM will enter Comm1ReceiveData () in the process, this time is called CRC function, but the result of calculation is wrong,
The code is as follows:

The function Managemessage (mbufferlength: WORD) : WORD;


According to what you say, you should is the pointer passed parameters, sealed you use function, it is to deal with SPCOMM transfer come over BUFF, so, when there are repeated calls, the results should not be the right
My level is limited, to SPCOMM also is not very proficient in, feel that the most direct way is to put the content of the pointer in the ReceiveDATA saved as a pointer, to call a function
In addition, send a 3 s to the COM data, this cycle if you have any questions? To combine your own COM port equipment transmission speed, and the processing flow, personal feel, if this is the COM on PC control, overflow or collapse is easy
  • Related