xbee dropping characters

I have two 60mW wire Xbee-PROs running on default settings. I have setup a number of simple tests to check to make sure they are communicating reliably, but no matter what I do, they are dropping characters. I have setup my "send" sketch to send A, B, A, B, A, B, and so on, but what the "receive" sketch is receiving is A, B, B, B, A, A, A, B, B, A randomly, with a few second pause of time in between sporadically. I am using arduino boards on both the receive and send sides.

My "send" program is here. On this side, I have the xbee attached to a Sparkfun xbee shield.

void setup()
{
Serial.begin(9600);
delay(1000);
}

void loop()
{
Serial.print("A");
delay(250);

Serial.print("B");
delay(250);
}

My "receive" program is here. On this side, I have a Sparkfun Explorer attached to an Arduino Mega, which has dedicated serial ports. I have the xbee attached to Serial #2 and an LCD attached to Serial #3 so that I can see what I'm receiving.

void setup()
{
Serial3.begin(19200); // setup LCD on Serial 3
Serial3.print(12,BYTE); // clear the LCD
Serial3.println("LCD Ready"); // display LCD Ready on the LCD

Serial2.begin(9600); // setup xbee on Serial 2
}

void loop()
{
Serial2.read(); // read the data from the xbee
if (Serial2.available() > 0)
{
byte myData = Serial2.read();
Serial3.print(myData); // display the data to the LCD
}
}

So, it's a very simple situation, but I can't get the data to come across consistently. I've tried changing the following, but the dropping of characters continues:

  1. Tried a different shield / explorer.
  2. Tried different Arduino Mega & Duemilanove boards.
  3. Tried two firmware versions (written to both xbees so that they were consistent): 10CD and 10E6
  4. Tried changing the delay times.
  5. Tried lighting LEDs instead of displaying to the LCD.

The results are always the same. I'm losing characters.

The only thing I haven't tried are these:

  1. I have not changed any xbee communication settings. (they are at default). My assumption is that I don't need to change any communication settings to get the above test to work. Is that right?
  2. I have not tried different xbees. Right now I only have the two. I have a third one on order in case one of the two I have is defective.
  3. Perhaps there is something wrong in my Arduino sketch that I don't understand.

Your help with be much appreciated. My project is dead in the water.

void loop()
{
[glow]Serial2.read(); // read the data from the xbee[/glow]
if (Serial2.available() > 0)
{
byte myData = Serial2.read();
Serial3.print(myData); // display the data to the LCD
}
}

That highlighted line may be stealing (and tossing in the trash) one of your bytes on every pass through loop. I doubt that this is what you want to have happen. Why is it there?

Thanks for the quick reply. I had picked that snippet of code up from someplace else and thought it was necessary, but you're right, now that I look at it, it doesn't seem like it should be there. I will test it right now and see if that helps the situation.

PaulS,

You're awesome! Thank you. That instantly solved my problem. I can't believe I've been staring at that code all this time and didn't see that problem. I eliminated that extra line that you identified and everything is working perfectly now. Thank you. I really appreciate it.

-rb451