Author Topic: OnAfterReceive handling  (Read 1633 times)

Jovans

  • Newbie
  • *
  • Posts: 8
    • View Profile
OnAfterReceive handling
« on: December 08, 2011, 12:32:39 pm »
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:
Code: [Select]
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

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: OnAfterReceive handling
« Reply #1 on: December 14, 2011, 04:10:51 am »
Hello? Are you there Roman?

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

Thanks
Jovan

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: OnAfterReceive handling
« Reply #2 on: December 20, 2011, 03:37:31 pm »
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:

Code: [Select]
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.