I have a need to receive a specific confirmation packet after commanding my transmitting Xbee to stop sending normal packets.

My buffer still has normal packets in it when this happens, so next time I call readPacket(), I don't want to see them. So right before I call my stop command, I try to purge the incoming buffer like this:

void burnSerial(void) {
  while (xbee.readPacket(20)) {
  } //read all available responses
    // only do this when not in a constant stream of responses or it may hang.

  while (Serial.available()) { Serial.read(); }
  //burn whatever may be in serial buffer

But this doesn't work. The next time I call xbee.readPacket(), I see tons of normal packets before I finally see my confirmation packet. Why don't these functions do anything for me at all?

1 Answer 1


It means there's a lot of latency between your issuing a command and getting a confirmation of the command's effect. When you send the command, it's best that you wait and read packets until you find the confirmation and then you return from the function or do whatever else you want to do. There is no need for a separate burnSerial() function, nor should you blindly discard data. So basically, the sequence of operations should be:

  • Send the command
  • Read packets until you find the 'confirmation' identification marker
  • Return or exit

This assumes that your transmitter only sends confirmation after it has halted the stream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.