Using Data From XBee API Series 2 Communication

I’m trying very hard to understand XBee Series 2 API communication using the xbee-arduino library. I believe I’ve figured out how to transmit now without a problem, but now I’m having difficulty with the receiver’s code. The example I’m using is as follows:

// Copyright 2015, Matthijs Kooijman <matthijs@stdin.nl>
//
// Permission is hereby granted, free of charge, to anyone
// obtaining a copy of this document and accompanying files, to do
// whatever they want with them without any restriction, including, but
// not limited to, copying, modification and redistribution.
//
// NO WARRANTY OF ANY KIND IS PROVIDED.
//
//
// This example prints any received ZigBee radio packets to serial.

#include <XBee.h>
#include <Printers.h>
#include <AltSoftSerial.h>

XBeeWithCallbacks xbee;

AltSoftSerial SoftSerial;
#define DebugSerial Serial
#define XBeeSerial SoftSerial

void setup() {
  // Setup debug serial output
  DebugSerial.begin(115200);
  DebugSerial.println(F("Starting..."));

  // Setup XBee serial communication
  XBeeSerial.begin(9600);
  xbee.begin(XBeeSerial);
  delay(1);

  // Setup callbacks
  xbee.onZBRxResponse(processRxPacket);
}

void processRxPacket(ZBRxResponse& rx, uintptr_t) {
  DebugSerial.print(F("Received packet from "));
  printHex(DebugSerial, rx.getRemoteAddress64());
  DebugSerial.println();
  DebugSerial.print(F("Payload: "));
  DebugSerial.write(rx.getData(), rx.getDataLength());
  if(rx.getData() == 'D') {
    DebugSerial.write("THIS MUST BE A 'D'!!!");
  }
  DebugSerial.println();
}

void loop() {
  // Check the serial port to see if there is a new packet available
  xbee.loop();
}

This is a straight cut-and-paste from the example with one exception. I’m trying to use the data I receive instead of just printing it straight to the serial buffer, so I added the if statement:

if(rx.getData() == 'D') {
  DebugSerial.write("THIS MUST BE A 'D'!!!");
}

As soon as I added this, however, I got the error “ISO C++ forbids comparison between pointer and integer [-fpermissive]”. From the xbee-arduino library documentation, the getData() member function states this:

uint8_t * RxDataResponse::getData 	( 		) 	

Returns the payload array.

This may be accessed from index 0 to getDataLength() - 1

I’m not an expert on pointers, but I thought the ‘*’ was the dereference operator, so doesn’t that change the pointer back to the actual value?

In short – How can I get the payload data from the Xbee frame into a usable variable again?

Any information you can provide would be greatly appreciated! Thanks!

What TYPE does rx.getData() return? It looks like it returns a pointer to an array of bytes. Why are you trying to compare and array of bytes to the letter D?

You might want to compare the array of bytes to the string "D", but not using ==. Use strcmp().

From what I can tell, the xbee-arduino library packages the payload into that array of bytes regardless of what it is. I didn't actually realize what it was until you said it that way. I wonder if I could assign the array it returns to a new array in the receiver's code so I can handle more than one byte if the transmission sends it. Would I use strcopy() for that?

I wonder if I could assign the array it returns to a new array in the receiver's code so I can handle more than one byte if the transmission sends it

Why do you need to copy the array from one place to another? You can deal with multiple bytes in the payload where the class put them.

Would I use strcopy() for that?

No, because the payload array is not a string. memcpy() could be used, if you really MUST make a copy.

You're absolutely right! I can just use "rx.getData()" just like an array in this case. With just that little update, my code now works:

  if(rx.getData()[0] == 'D') {
    DebugSerial.write("THIS MUST BE A 'D'!!!");
  }

I think I was worried that since rx.getData() was a member function that by calling it repeatedly would mess up the indexing so, for example, rx.getData()[3] might not always be the fourth item in that array. Will that be a problem over time?

I think I was worried that since rx.getData() was a member function that by calling it repeatedly would mess up the indexing so, for example, rx.getData()[3] might not always be the fourth item in that array. Will that be a problem over time?

No. The item at index 3 will always be the 4th element of the array that the getData() function returns a pointer to.