1

Product family: XBP24-ZB Function set: ZigBee Coordinator API Firmware version: 21A7

Hello, I am currently using Digi's XBee Java Library with API (AP=1) and it works properly. However, another node of my network is associated with an Arduino and I want to use 'Arduino Library for communicating with XBee in API mode' (https://github.com/andrewrapp/xbee-arduino). That Arduino Library requires API Escaped operating mode (API 2). It was not supposed to be a problem, since XBee Java Library supports API 2. Nevertheless, I got an error when trying to open the serial connection with the XBee.

package com.digi.xbee.example;

import com.digi.xbee.api.XBeeDevice;
import com.digi.xbee.api.exceptions.XBeeException;

public class MainApp {
    /* Constants */
    // TODO Replace with the port where your sender module is connected to.
    private static final String PORT = "COM4";
    // TODO Replace with the baud rate of your sender module.
    private static final int BAUD_RATE = 9600;

    private static final String DATA_TO_SEND = "Hello XBee World!";

    public static void main(String[] args) {
        XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
        byte[] dataToSend = DATA_TO_SEND.getBytes();

        try {
            myDevice.open();

            System.out.format("Sending broadcast data: '%s'", new String(dataToSend));

            myDevice.sendBroadcastData(dataToSend);

            System.out.println(" >> Success");

        } catch (XBeeException e) {
            System.out.println(" >> Error");
            e.printStackTrace();
            System.exit(1);
        } finally {
            myDevice.close();
        }
    }
}

Error com.digi.xbee.api.exceptions.TimeoutException: There was a timeout while executing the requested operation. at com.digi.xbee.api.AbstractXBeeDevice.sendXBeePacket(AbstractXBeeDevice.java:989) at com.digi.xbee.api.AbstractXBeeDevice.sendATCommand(AbstractXBeeDevice.java:806) at com.digi.xbee.api.AbstractXBeeDevice.sendParameter(AbstractXBeeDevice.java:1983) at com.digi.xbee.api.AbstractXBeeDevice.getParameter(AbstractXBeeDevice.java:1925) at com.digi.xbee.api.AbstractXBeeDevice.readDeviceInfo(AbstractXBeeDevice.java:365) at com.digi.xbee.api.XBeeDevice.open(XBeeDevice.java:219) at com.digi.xbee.example.MainApp.main(MainApp.java:20)

Is there any difference between API and API 2 when programming with this Java Library?

2 Answers2

0

I'm not sure about using the Arduino library, but I see that you're opening your device, and never telling it to connect to the other xbee.

You need to tell the xbee which device you want to connect to.

public void connect(String REMOTE_NODE_IDENTIFIER)
{
 //REMOTE_NODE_IDENTIFIER is the name of the xbee device you want to connect to.
    try 
    {
        myDevice.open();
        // Obtain the remote XBee device from the XBee network.
        XBeeNetwork xbeeNetwork = myDevice.getNetwork();
        RemoteXBeeDevice remoteDevice = xbeeNetwork.discoverDevice(REMOTE_NODE_IDENTIFIER);
        if (remoteDevice == null)
        {
            //Unable to connect to device
            System.out.println("Couldn't find the remote XBee device with '" + REMOTE_NODE_IDENTIFIER + "' Node Identifier.");
        }

        else
        {
            //Successfully connected
        }

     } catch (XBeeException e) 
        {
            e.printStackTrace();
        }
}
Bone
  • 91
  • 8
  • Thanks for your reply, but it would be my next step. Now I just want to open and close the serial communication with the local XBee. And as I said it works properly with API 1. When I change it to API 2, I get the error. – Thiago Bruno Jan 21 '16 at 11:51
  • I was curious, so I tried your code, and it worked for me. I did however have to change the baud rate to match that of my robot. Perhaps you need to change yours as well? Timeout exceptions on open usually occur when the baud rate is incorrect – Bone Jan 21 '16 at 15:16
  • Baud rate is 9600 in the code and in the XBee. I reset the XBee and changed from API 1 to API 2. Still not working. – Thiago Bruno Jan 21 '16 at 19:14
0

The baud rate and API mode on each module in a network do not need to match, they're just used for serial communication with the local host. Note that the XBee module's ATAP setting needs to match whichever API mode you're trying to use in your host software.

Keep using the working ATAP=1 with the Java library, and use ATAP=2 with the Arduino. API Mode 2 is just an "escaped" mode where certain bytes in the payload are escaped as a way to tell them apart from the 0x7E frame start character.

You can even run the Java end at 115200 or 57600bps, and keep the Arduino end at 9600bps. You can run modules with "AT mode" firmware and communicate with them from a module using "API mode". The radio module buffers data and it's sent over-the-air at 250kbps, regardless of your host settings.

tomlogic
  • 11,489
  • 3
  • 33
  • 59