Author Topic: Received number of bytes problem  (Read 2090 times)

Jovans

  • Newbie
  • *
  • Posts: 8
    • View Profile
Received number of bytes problem
« on: October 15, 2011, 01:50:48 am »
Hi,

Am having an issue with the Received value inside nrComm1AfterReceive changing.

I have hardware with FTDI serial to USB driver sending the same packet of 16 bytes @ 115200 baud.
Have checked hardware with oscilloscope, port sniffer and the data is consistantly 16 bytes in length (and the data is not changing).

My problem is that Received value is changing from 16 to random values like 1,3,7 etc (but not over 16). Also there is no pattern to the errors occuring.

I have compiled and used the demo program String Receiver BCB and can see the data received not being 16 bytes all the time. I also made a separate program and it shows an error count when Received is != 16.

I am sure that the nrcomm component works. This problem has to be some internal windows issue (am using Windows 7).

My question is how does the event AfterReceive get triggered?

Is there a timeout other than TimeoutRead (which is set to 0) to work out when the received bytes are in the buffer?

The USB settings are default: Received/Transmit bytes = 4096, Latency: 16msec

Any suggestions if anyone has experienced this issue, and any suggestions would be much appreciated.

Thanks
Jovan

Roman Novgorodov

  • Engineer
  • Administrator
  • Hero Member
  • *****
  • Posts: 994
  • we will win
    • View Profile
    • Forums.nrCommLib.Com
Re: Received number of bytes problem
« Reply #1 on: October 16, 2011, 03:59:25 pm »
Hello

Please read this:
http://forums.nrcommlib.com/index.php?topic=8.0

If you want to detect 16 byte packet, it is very easy. Please see following code:

Code: [Select]
var
   bufferGlobal:array [0..15] of byte;
   indexBuffer :integer = 0;

procedure TForm1.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);
var i:integer;
    ch:AnsiChar;
begin
  for i := 0 to Received - 1 do begin
    bufferGlobal[indexBuffer] := Byte(PAnsiChar(Buffer)[i]);
    Inc(indexBuffer);
    if indexBuffer = 16 then begin
      indexBuffer := 0;
      DosomethingWith16bytePacket(bufferGlobal); // your routine for process packet
    end;
  end;
end;

Also possible more info you can find here:
http://forums.nrcommlib.com/index.php?board=19.0

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.

Jovans

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Received number of bytes problem
« Reply #2 on: October 17, 2011, 01:18:02 am »
Thanks for you reply,

I wrote this code in OnAfterReceive:

Code: [Select]
for(int i=0; i < Received; i++) {
char ch = PAnsiChar(Buffer)[i];
InBuffer[i] = ch;
RxMemo->Lines->Add(InBuffer[i]);
LCount->Caption = Received;
}
RxMemo->Lines->Add("\n");
if (Received != 16) {
g_Counter++;
LCounter->Caption = g_Counter;
}

My packets will be of varying length, but I did 16 bytes same packets for a quick test.

I was monitoring the number of bytes received in the buffer, and it would vary from 16,16,16, then randomly 1 bytes, 2 bytes etc. Over the last 2 days I have used the DataProcessor and yes it has fixed the issue. I have some other questions using the DataProcessor so will post in the other section.

I didn't realise that the internal windows buffer won't read all 16 bytes in one (considering that windows has a large buffer). I read your http://forums.nrcommlib.com/index.php?topic=8.0 earlier and it all makes sense now.

Thank you very much for your explanation.

Jovan

Hello

Please read this:
http://forums.nrcommlib.com/index.php?topic=8.0

If you want to detect 16 byte packet, it is very easy. Please see following code:

Code: [Select]
var
   bufferGlobal:array [0..15] of byte;
   indexBuffer :integer = 0;

procedure TForm1.nrComm1AfterReceive(Com: TObject; Buffer: Pointer;
  Received: Cardinal);
var i:integer;
    ch:AnsiChar;
begin
  for i := 0 to Received - 1 do begin
    bufferGlobal[indexBuffer] := Byte(PAnsiChar(Buffer)[i]);
    Inc(indexBuffer);
    if indexBuffer = 16 then begin
      indexBuffer := 0;
      DosomethingWith16bytePacket(bufferGlobal); // your routine for process packet
    end;
  end;
end;

Also possible more info you can find here:
http://forums.nrcommlib.com/index.php?board=19.0

Roman Novgorodov
DeepSoftware LLC