buffer overflow when recieving large bitstream of data via xbee?

Hello everyone. I am working on a project which involves having to send a JPEG image from one Xbee transmitter to another Xbee receiver. For testing purposes however, I am only using text files now to simplify things and just to wrap my head around on how the bitstream is sent from one Xbee to another.
In the transmitting end, I am sending a packet of characters via the XCTU software from the transmitting Xbee. In the receiving end, I wrote a code where the Arduino reads any incoming byte via the receiving Xbee and then immediately writes the byte to a file on the SD card. So far, I have noticed that if make the packet in the XCTU hold more data(500 bytes and more), some of the characters get lost in the receiving end ,after the file is written and also when seeing it in the serial port. I figured that it might be due to a buffer overflow where the receiving end( reading an incoming byte and then writing it to the SD card) - isn’t fast enough to keep up with the incoming byte stream sent by the transmitting Xbee, hence I figured that some of the incoming bytes get lost. I considered lowering the baudrate to 1200, but I noticed that it only makes it worse. Any ideas? I have attached snippets of my code in the attachments. Any constructive criticism on the algorithm that I developed and how I used it would appreciated. Thanks.

EDIT: I noticed that the pictures didn’t show up so well so I’ll just copy and paste the code below:

//////external libraries included
#include <Adafruit_VC0706.h>
#include <SPI.h>
#include <SD.h>
#include <SoftwareSerial.h>

#define chipSelect 10

// Using SoftwareSerial (Arduino 1.0+) or NewSoftSerial (Arduino 0023 & prior):
#if ARDUINO >= 100
// On Uno: camera TX connected to pin 2, camera RX to pin 3:
SoftwareSerial cameraconnection = SoftwareSerial(4, 5);
SoftwareSerial XBee(2, 3); // RX, TX

// On Mega: camera TX connected to pin 69 (A15), camera RX to pin 3:
//SoftwareSerial cameraconnection = SoftwareSerial(69, 3);
NewSoftSerial cameraconnection = NewSoftSerial(4, 5);

Adafruit_VC0706 cam = Adafruit_VC0706(&cameraconnection);

int jpglen = 5000;
uint8_t b=0;
File imgFile;
char filename[14];

void setup() {




// delay(5000);

// ------------------------------------------------------------------------------------------------------------------------------------start

/// uint16_t jpglen = 1300000;

//uint8_t myData = 0;

// When using hardware SPI, the SS pin MUST be set to an
// output (even if not connected or used). If left as a
// floating input w/SPI on, this can cause lockuppage.
#if !defined(SOFTWARE_SPI)
#if defined(AVR_ATmega1280) || defined(AVR_ATmega2560)
if (chipSelect != 10) pinMode(10, OUTPUT); // SS on Uno.

// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println(“Card failed, or not present”);
// don’t do anything more:

//create a file
// char filename[14];
strcpy(filename, “XXAGZ000.JPG”);
for (int i = 0; i < 100; i++) {
filename[5] = ‘0’ + i/10;
filename[6] = ‘0’ + i%10;
filename[7] = ‘0’ + i%10;
// create if does not exist, do not open existing, write, sync after write
if (! SD.exists(filename)) {
imgFile = SD.open(filename, FILE_WRITE);


void loop() {
/// Serial.write(-1); ///y

// uint8_t b=0;
int file_close=0;

if(XBee.available()>0){////when byte stream arrives

file_close = 1; /////set to one to close file

// imgFile = SD.open(filename, FILE_WRITE);
//Serial.print(“succesfully created file”);

///write to file until bytes finish

b = XBee.read();




imgFile.close(); //closee file



  1. using code tags would be even nicer than simply including the source text.
  2. You are almost certainly consuming (reading) data faster than the transmitter can send the data and you close the file immediately you can't read any more. Your while loop "while(jpglen>0)" must handle the situation where there is a temporary pause in the data transmission.