Xbee hardware Serial UART copying reponses to Arduino

This is a strange one, I must not understand an intricacy of the Arduino hardware Serial commands. I found a way to stop the problem but I don’t think I should need to do this so I would like to fill in the missing knowledge.

Hardware: Arduino DUE talking to an XBEE Pro S3B using Serial2 hardware serial.

Task: Simply sending AT** commands in command mode and getting responses

What I expected:

\r = Return Character

Send to Xbee: {1 second}+++{1 second}
Received from Xbee: OK\r

Send to Xbee: ATCT 1000\r
Received from Xbee: OK\r

Send to Xbee: ATSH\r
Received from Xbee: 3BF0\r

Send to Xbee: ATSL\r
Received from Xbee: 4FB3\r

Send to Xbee: ATCT 1000\r
Received from Xbee: OK\r

But here’s what I got:

Send to Xbee: {1 second}+++{1 second}
Received from Xbee: OK\r

Send to Xbee: ATCT 1000\r
Received from Xbee: OK\rOK\r

Send to Xbee: ATSH\r
Received from Xbee: OK\r3BF0\r

Send to Xbee: ATSL\r
Received from Xbee: 3BF0\r4FB3\r

Send to Xbee: ATCT 1000\r
Received from Xbee: 4FB3\rOK\r

So what is happening is the Xbee response to the CURRENT command is getting tacked on to the xbee response from the LAST command.

Here is the truncated code doing this:

Serial2.print(strXBEECommand); // Send Xbee command with Carriage Return
Serial2.flush(); // Wait for characters to get send out

delay(500); // Let Xbee think and respond

// Read back the xbee response (Note: I was using readBytes() before and thought this would solve it)
ulongTargetMillis = millis() + ulongWaitMillis;
iCharCounter = 0;

while (millis() < ulongTargetMillis)
{
if (Serial2.available() > 0)
{
strXBEEResponse[iCharCounter++] = char(Serial2.read());
}
delay(5);
}

Things I tried to figure out what is happening that didn’t help:

  1. I cleared the strXBEEResponse char array before reading thinking that might be the issue.
  2. Forced the echo off on the Xbee.
  3. Put debug code everywhere to see what was exactly in the char array strings before and after. With this I determined that it was actually reading the duplicate from the Xbee as far as I could tell.
  4. I thought my Xbee was flaking so I tried another with the exact same result
  5. Spent hours reading posts and blogs for the answer.

So this is what I did to stop the last response from coming through on the current response:

I added this code BEFORE sending the Xbee command. It just removes any existing data from the buffer and also counts it to see if any was there.

iCharCounter = 0;
while (Serial2.available() > 0)
{
incomingByte = Serial2.read();
iCharCounter++;
delay(10);
}

Sure enough, after the first command and response it always has the last responses data in it even though I had already used Serial2.read() to grab it. By removing these extra chars the responses are exactly as expected.

My Question:

What don’t I understand here with the Xbee or the hardware Serial commands? My understanding (from experience and renewed reading) is that once you Serial.read() a byte it gets erased from the incoming buffer. And if the Xbee is doing this, why? I have not seen online that anyone is having this issue? Like I said, clearing the buffer before sending the command works but I want to know why this is happening so any wisdom is much appreciated!

Thank You!

You need to post a complete program.

Have a look at the examples in Serial Input Basics. Your system for receiving data is not at all robust.

This

ulongTargetMillis = millis() + ulongWaitMillis;
iCharCounter = 0;
     
while (millis() < ulongTargetMillis)

is not a good way to use millis(). Have a look at how it is done in the demo Several Things at a Time

…R