Warning for those using Arduinos + Xbee

The xbee-arduino library has a pretty major flaw.

Specifically, it does a byte-wise check for ‘7E’ and instantly assumes that it represents a new packet, or ‘7D’ and assumes it’s an escape. Essentially the code looks like this…

if (_pos > 0 && b == 0x7E) {
_response.setErrorCode(UNEXPECTED_START_BYTE);
return;
}

if (_pos > 0 && b == 0x7D) {
_response.setErrorCode(EXSCAPE_BYTE);
return;
}

obviously the problem with this is that ‘7E’ and ‘7D’ are both valid values in an analog sample packet. With 40+ samples per packet, this bug turns into a tonne of data loss as it drops any packet containing 253, 254, 509, 510, 765, 766, 1021, or 1022 (i.e. many of them).

Don’t use the library. Instead, monitor the serial data from your XBee module for the 0x7E byte, then capture the byte-count values and put that many bytes from the serial port in an array you can parse later. You might want to compute the checksum as you go and compare it with the checksum at the end of the series of bytes. Also, it’s worth checking the Status byte to ensure you don’t have a problem. This way you only look for the 0x7E at the start of communications.

To me, the Arduino has a bigger problem–it shares its serial port with the USB interface. I have run into problems when trying to load code while I have a serial device connected to the serial port (but not active). Sadly, the Uno module has only one serial port.

If it’s doing those checks on 7E and 7D, then it’s assuming that the XBee is set to use API mode 2. So you should be able to use the library if you make that setting.

@ johnf
Indeed, I am mistaken. The library is correct. Though my Xbee is in API = 2, after reviewing the raw data the radio is failing to properly escape all characters (though it does some…)
I guess this shouldn’t surprise me, as I’ve read configuration values back from the radio that I’m pretty damned sure I never set on more the one occasion.
It seems to be happy pumping out ADC samples over unicast though, I’ve swapped my Xbees and their configurations and everything seems to be working nicely.

@microguy
The UNO + a V3 Xbee breakout board works pretty well. If you pull the micro-controller from the Arduino you can use X-CTU to configure the radio. With the controller in, and the breakout board switch set to ‘USB’ you can program the micro-controller. Once the switch is set to ‘xbee’ the radio and the micro controller can communicate, and can be monitored via USB.