Xbee Serial.read is not clearing buffer

447 Views Asked by At

I wrote a example program to test Serial read from xbee. I was expecting a message passed from transmitter to receiver every 5 sec's but in serial monitor of receiver I am observing a continuous stream of repeat messages. Can anyone what I am missing. FYI: Also attached link to serial monitor screenshot. [1]: https://i.stack.imgur.com/Lgxx5.png

/*  ~ Simple Arduino - xBee Transmitter sketch ~ Router
*/

int count = 0;

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

void loop() {
    //Send the message:
    count ++;
    Serial.println(String("Hello World : " + String(count)));
  delay(5000);
}
/*  ~ Simple Arduino - xBee Receiver sketch ~ Coordinator
*/

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

void loop() {
  if (Serial.available() > 0){
    Serial.write(Serial.read());
  }
}
2

There are 2 best solutions below

0
Vamsi On BEST ANSWER

Figured out a work around to address the issue. Here are the details:

https://i.stack.imgur.com/3qZMi.png

Circuit connections from Arduino to XBEE Shield: 
D0/RX to TX
D1/TX to RX 
5V to 5V 
GND to GND 
/*  ~ Simple Arduino - xBee Transmitter sketch ~ Router
 */

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

void loop() {
  //Send the message:
  Serial.print('<');
  Serial.print("Hello World");
  Serial.println('>');
  delay(1000);
}
/*  ~ Simple Arduino - xBee Receiver sketch ~ Coordinator
*/

bool started = false; //True: Message is strated
bool ended = false;   //True: Message is finished
byte index;           //Index of array

char character; //Variable to store the incoming byte
char msg[13];   //Message - array

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

void loop()
{

  while (Serial.available())
  {
    character = Serial.read();
    if (character == '<')
    {
      started = true;
      index = 0;
      msg[index] = '\0'; // Throw away any incomplete packet
    }

    //End the message when the '>' symbol is received
    else if (character == '>')
    {
      ended = true;
      break; // Done reading - exit from while loop!
    }

    //Read the message!
    else
    {
      if (index < 11)
      {                         // Make sure there is room
        msg[index] = character; // Add char to array
        index++;
        msg[index] = '\0'; // Add NULL to end
      }
    }
  }

  if (started && ended)
  {
    Serial.print("Message: ");
    Serial.println(msg);

    index = 0;
    msg[index] = '\0';
    started = false;
    ended = false;
  }
}
3
tomlogic On

For the receiver, don't you just want to pass Serial.read() to print() instead of Serial.write()? If you have two serial ports, one to the console and one to the XBee, they should have different names.

Could you provide some more details on your serial connections? What are COM3 and COM6 attached to? Are you sharing serial port pins with the XBee and your console? it seems like that could be part of your problem, if either the Arduino or XBee can drive the RX pin of your receiver's serial port, you'd end up echoing your characters back to yourself.