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

Received number of bytes problem

Started by Jovans, October 14, 2011, 10:50:48 pm

Previous topic - Next topic

Jovans

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

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:


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

Thanks for you reply,

I wrote this code in OnAfterReceive:


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

Quote from: Roman Novgorodov on October 16, 2011, 01: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:


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