Receiving data via Xbee - Serial port buffer overflow

Hello. I send data packets from one Xbee S1 to another. When a packet bigger than 64 bytes is sent there is lost of data. I use readPacket() method from Arduino library (run in every 20ms). There is also a method called available() which returns number of bytes in buffer. I print this value and what I see is that until any data is received it returns always 0 (this is normal), but if data is sent buffer immediately is equal to size of sent packets.

So if I send a packet of size 20 → serial buffer goes immediately from 0 to 20
If I send a packet of size 80 → serial buffer goes immediately from 0 to 64 and causes loss of data

From my knowledge reading should start when there is any data, not when whole data packet is in serial buffer, then buffer will be not overflowed. I tried setting Xbee which sends data to 9600 baud rate(thought that then data will be sent slower so receiver will have much more time for processing, but it wont help).

My configuration:
Xbee S1(sender - coordinator):
Firmware: 10ec
API: 2
No encryption
Baudrate: 57600

Xbee S1(receiver - end device)
Firmware: 10ec
API: 2
No encryption
Baudrate: 57600

Initialization (BAUDARATE = 57600 and POLL_INTERVAL = 20(ms))

  // Initialization
   template<typename OsModel_P>
   int ArduinoXBeeRadio<OsModel_P>::enable_radio()
   {
      if(!initialized_)
      {
            Serial1.begin(BAUDRATE);
            xbee_.setSerial(Serial1);
            initialized_ = true;
      }

      if(enabled_ && disabling_) {
         disabling_ = false;
      }

      if(!enabled_) {
         pinMode(9, OUTPUT);
         digitalWrite(9, LOW);
         delay(300);

         timer_->template set_timer<ArduinoXBeeRadio<OsModel_P> , &ArduinoXBeeRadio<OsModel_P>::read_recv_packet > ( POLL_INTERVAL, this , ( void* )timer_ );

         enabled_ = true;
         update_led();
      }
      return SUCCESS;
   }

For reading I use this function. It is called every 20 miliseconds.

   template<typename OsModel_P>
   void ArduinoXBeeRadio<OsModel_P>::read_recv_packet(void*)
   {
      if(!enabled_) { return; }
      if(disabling_)
      {
         disable();
         return;
      }

      ::uint32_t t_read = millis();
      xbee_.readPacket();
      if(xbee_.getResponse().isAvailable())
      {
         Rx16Response rx16 = Rx16Response();
         node_id_t from_id;
         size_t length;
         block_data_t* data;

         if (xbee_.getResponse().getApiId() == RX_16_RESPONSE)
         {
            xbee_.getResponse().getRx16Response(rx16);
            from_id = rx16.getRemoteAddress16();
            data = rx16.getData();
            length = rx16.getDataLength();
            received(data, length, from_id);
         }
         else if (xbee_.getResponse().getApiId() == TX_STATUS_RESPONSE)
         {
            TxStatusResponse txStatus = TxStatusResponse();
            //xbee_.getResponse().getZBTxStatusResponse(txStatus);
            xbee_.getResponse().getTxStatusResponse(txStatus);
         }
      }

Any ideas what can be a cause of a problem?

Thanks

First, both XBees should be at the same baud rate.

Second, no code generally equals no help.

I tried to use simplified version of code.

Every two seconds I send a message:

Send start: 7e 00 50 01 01 ff ff 00
Payload: 05 00 01 03 84 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 7d 31 12 7d 33 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 03

#include <XBee.h>

XBee xbee = XBee();

uint8_t start_data[] = { 0x7E, 0x00, 0x50, 0x01, 0x01, 0xff, 0xff,
0x00, 0x05, 0x00, 0x01, 0x03, 0x84, 0x00 };

void setup() 
{
  Serial.begin(57600);
}

void loop()
{
  
  for( int i = 0; i < 14; i++)
  {
    Serial.write(start_data[i]);
  }
  
  for( int i = 1; i < 70; i++)
  {
    Serial.write(i);
  }
  
  Serial.write(0x03);
  
  delay(2000);
}

For receiving I tried with code:

#include <XBee.h>

XBee xbee = XBee();

void setup() 
{
  Serial.begin(57600);
  Serial1.begin(57600);
}

void loop()
{
  Serial.print("Serial: ");
  Serial.println(Serial1.available(), DEC); 
  if (Serial1.available() > 0)
  {
    char c = Serial1.read();
    Serial.print("Read:   ");
    Serial.println(c, HEX);
  }
}

Output is:

...
Serial: 0
Serial: 0
Serial: 0
Serial: 0
Serial: 0
Serial: 1
Read:   7E
Serial: 23
Read:   0
Serial: 45
Read:   50
Serial: 63
Read:   FFFFFF81
Serial: 63
Read:   12
Serial: 62
Read:   35
Serial: 61
Read:   1A
Serial: 60
Read:   2
Serial: 59
Read:   5
Serial: 58
Read:   0
Serial: 57
Read:   1
Serial: 56
Read:   3
Serial: 55
Read:   FFFFFF84
Serial: 54
Read:   0
Serial: 53
Read:   1
Serial: 52
Read:   2
Serial: 51
Read:   3
Serial: 50
Read:   4
Serial: 49
Read:   5
Serial: 48
Read:   6
Serial: 47
Read:   7
Serial: 46
Read:   8
Serial: 45
Read:   9
Serial: 44
Read:   A
Serial: 43
Read:   B
Serial: 42
Read:   C
Serial: 41
Read:   D
Serial: 40
Read:   E
Serial: 39
Read:   F
Serial: 38
Read:   10
Serial: 37
Read:   7D
Serial: 36
Read:   31
Serial: 35
Read:   12
Serial: 34
Read:   7D
Serial: 33
Read:   33
Serial: 32
Read:   14
Serial: 31
Read:   15
Serial: 30
Read:   16
Serial: 29
Read:   17
Serial: 28
Read:   18
Serial: 27
Read:   19
Serial: 26
Read:   1A
Serial: 25
Read:   1B
Serial: 24
Read:   1C
Serial: 23
Read:   1D
Serial: 22
Read:   1E
Serial: 21
Read:   1F
Serial: 20
Read:   20
Serial: 19
Read:   21
Serial: 18
Read:   22
Serial: 17
Read:   23
Serial: 16
Read:   24
Serial: 15
Read:   25
Serial: 14
Read:   26
Serial: 13
Read:   27
Serial: 12
Read:   28
Serial: 11
Read:   29
Serial: 10
Read:   2A
Serial: 9
Read:   2B
Serial: 8
Read:   2C
Serial: 7
Read:   2D
Serial: 6
Read:   2E
Serial: 5
Read:   2F
Serial: 4
Read:   30
Serial: 3
Read:   31
Serial: 2
Read:   32
Serial: 1
Read:   3C
Serial: 0
Serial: 0
Serial: 0
Serial: 0
Serial: 0
...

So it can’t read with the same speed as it is sent… even if both devices are configured for the same baud rate…

Even tried to add 500 ms delays between writing to serial, still the same problem occurs.

for( int i = 0; i < 14; i++)
  {
    Serial.write(start_data[i]);
    delay(500);
  }
  
  for( int i = 1; i < 70; i++)
  {
    Serial.write(i);
    delay(500);
  }

Serial: 0
Serial: 1
Read: 7E
Serial: 23

Once the data starts to arrive, you begin outputting, at the same speed, orders of magnitude more data than what is coming in. Of course the incoming buffer is going to overflow.

The Arduino is capable of reading serial data faster than it can arrive, but not while doing all that you are doing with each byte.

The packet contains a start byte and a length. Once you have the start of a packet, you should have a while loop that spins waiting for the length byte or a time to elapse. Once the length arrives, you should have an inner while loop that spins waiting for that number of bytes to arrive or a timeout to happen.

Of course, since you are using the XBee library, which can handle the collection of serial data correctly, this is all rather academic. Just let the XBee library do what it is supposed to do.