• Welcome to Forum.Deepsoftware.Com. Please login or sign up.
 

OnAfterReceive handling

Started by Jovans, December 08, 2011, 10:32:39 am

Previous topic - Next topic

Jovans

Hi,

Does OnAfterReceive have its own thread? Can I increase the priority of this thread?

I am sending 10 commands one after another, only when a reply is received.

My program appears to be stuck in a function waiting for OnAfterReceive to process it has received x bytes.

The basics of the program are below:

OnAfterReceive:
- Receives each byte.
- if 6 bytes received check acknowledge command is correct then allow more commands to be sent by setting globalvariable = true.

globalvariable initialised to true

WriteFunction:

for (unsigned int i = 0; i < 10; i++) {
while (globalvariable == false)
  {
      ; // wait for OnAfterReceive to clear flag
  }
  nrComm1->SendData(TxData,11); // send data
  globalvariable = false; //set flag so write command cannot be sent
}
 

So in theory the writefunction should send the first command. OnAfterReceive should process when 6 bytes received, and are correct, set the flag to send more data. Is this the correct approach?

I have nrComm working for sending 1 command at a time, and OnAfterReceive works. I also have it receiving 1000 bytes of data fine, and getting that data re-sent with a command, but have this problem in sending x commands in a row by waiting for correct reply before sending more.

Hopefully you can help me out, its much appreciated

Thanks
Jovan

Jovans

Hello? Are you there Roman?

Any ideas how to implement the handshaking and changing nrcomm thread priority?

Thanks
Jovan

Roman Novgorodov

Hello

I'm sorry, we missed your question.

It seems you choose non optimal way.

If you want to send command after every 6 incoming byte the following code is more correct:

var globalBuffer:string = '';

procedure TForm1.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);
var i:integer;
    ch:AnsiChar;
begin
  for i := 0 to Received - 1 do begin
    globalBuffer := globalBuffer + PAnsiChar(Buffer)[I];
    if Length(globalBuffer) = 6 then begin
       PorcessDataAndReply();        // your routine ...
       globalBuffer := '';
    end;
  end;
end;

procedure     TForm1.PorcessDataAndReply();        // your routine ...
begin
   if isOK(globalBuffer)
      then nrComm1->SendData(TxData,11); // send data
end;


Also you can play with UseMainThread property for thread change context of nrComm1AfterReceive handler.

Roman Novgorodov
DeepSoftware LLC
DeepSoftware llc - The professional components for Delphi/CBuilder/.NET. The high quality custom software development.
Forums.nrCommLib.Com - DeepSoftware Tech Support Forum.