Communicate bewteen 2 "Arduinos Mega" by using 2 XBee API mode

Hello dears,

I am trying to send a frame {7E, 0014, 10, 01, 00000000, 0000FFFE, 00, 00, 54, 78, 44, 61, 74, 61, AD} from Arduino to another Arduino.

This frame is Tx (Transmit request) for Xbee protocol and in ASCII it gives “TxData”.
I can send this frame by uisng XCTU in API mode (AP=2) between coordinator and End Device.

I want now to send the same frame by using Arduino.

In the programming, I wrote the following code for the transmitter (my case coordinator):

#include <XBee.h>
XBee xbee = XBee(); //The XBee object provides functions for sending and receiving packets.
ZBTxStatusResponse tx = ZBTxStatusResponse();
byte B []={0x7E, 0x0014, 0x10, 0x01, 0x000, 0x0000, 0xFFFF, 0xFFFE, 0x00, 0x00, 0x54,0x78, 0x44, 0x61, 0x74, 0x61, 0xAD};
void setup()
{
** Serial.begin(9600);**
** Serial3.begin(9600);**
** xbee.setSerial(Serial3);**
** delay (1000);**
}
void loop() {
** XBeeAddress64 address = XBeeAddress64(0x0013A200,0x40A7ECCA);**
** ZBTxRequest zbTx = ZBTxRequest(address, B, sizeof(B));**
** xbee.send(zbTx);**
** delay(3000);**
** }**

And for the receiver (End Device):

#include <XBee.h>
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
ZBRxResponse rx = ZBRxResponse();
ModemStatusResponse msr = ModemStatusResponse();
void setup()
{
** Serial.begin(9600);**
** Serial3.begin(9600);**
** xbee.setSerial(Serial3);**
** delay (1000);**
}
void loop()
{
** xbee.readPacket(100);**

** if (xbee.getResponse().isAvailable()) {**
** // got something**
** Serial.println (“got something”);**
** if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {**
** // got a rx packet**
** Serial.println (“got a rx packet”);**
** xbee.getResponse().getZBRxResponse(rx);**
** }**
** }**
** for (int i = 0; i < rx.getDataLength(); i++) {**
** Serial.print(“B [”);**
** Serial.print(i, DEC);**
** Serial.print("] is ");**
** Serial.println(rx.getData(i));**
** delay (3000);**
}
}

There is no communication between them, can some one help me please
Regards,

How are the XBees configured? Are you sending to the correct device?

Hello, Yes it configured correctly because of :

XBeeAddress64 address = XBeeAddress64(0x0013A200,0x40A7ECCA); where the 0x0013A200 is a SH of End Device and 0x40A7ECCA is SL.

How are the XBees connected to the Arduinos?

4 pins. 3.3 V, Ground, Rx and Tx connected to Serial3.

I have tested the Xbee in At mode before and it was working with me.

I am sure there is no problem in connection but the problem is in the code. to be more clear, how can I write on the Serial (in the transmitter) and read from the Serial (in the Receiver) with using API becasue as you see here we just have a packet to read like:

xbee.readPacket(100);

There is no elements to read, like for instance the element of array.

how can I write on the Serial (in the transmitter) and read from the Serial (in the Receiver) with using API becasue as you see here we just have a packet to read like:

The library manages the writing of the packet to the appropriate Serial instance's pins and for reading the response(s) and storing the data in private fields that can be accessed using the get methods.

If YOU want to manage all that, using API mode seems pointless.

Okay, so according to you when I give an order of

xbee.readPacket();

That means all of my array has stored inside the memory of Arduino.

How can I obtain B[1] or B[12] for example to print them??

Thank you for your response, I really appreciate that my friend.

How can I obtain B[1] or B[12] for example to print them??

When you call readPacket() that tries to read an entire packet from the appropriate serial buffer. It may or may not be successful.

getResponse() will extract data from the packet, if one was read and is complete. It sets a flag to indicate whether it has good data. The isAvailable() method returns the state of the flag.

If there is data, getZBRxResponse() gets the data into a ZBRxResponse object. That object's getDataLength() and getData() methods extract the data from the response.

Since you are not seeing any data being printed, there is obviously some failure to communicate far above where you try to read the data. You can add more Serial.print() statements to the sketch and to the XBee library to see where the failure occurs.

If you are only using 2 XBees, and neither XBee is adding data to the packet on its own, using the XBee library and API mode is overly complicated.

Thank you , I did not solve the problem yet.