使用 Digi 的 XBee Java 库和 API 中的 XBee 2(转义模式)

Using Digi's XBee Java Library with the XBee in API 2 (Escaped mode)

产品系列:XBP24-ZB 功能集:ZigBee 协调器 API 固件版本:21A7

你好,我目前正在使用 Digi 的 XBee Java 库和 API (AP=1),它工作正常。 但是,我网络的另一个节点与 Arduino 相关联,我想使用 'Arduino Library for communicating with XBee in API mode' (https://github.com/andrewrapp/xbee-arduino)。 Arduino 库需要 API 转义操作模式 (API 2)。这不应该是个问题,因为 XBee Java 库支持 API 2。然而,当我尝试打开与 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)

使用这个 Java 库编程时,API 和 API 2 有什么区别吗?

我不确定是否使用 Arduino 库,但我看到您正在打开您的设备,但从未告诉它连接到另一个 xbee。

你需要告诉xbee你想连接哪个设备。

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();
        }
}

网络中每个模块的波特率和API模式不需要匹配,它们只是用于与本地主机的串行通信。请注意,XBee 模块的 ATAP 设置需要与您尝试在主机软件中使用的任何 API 模式相匹配。

继续使用 ATAP=1 和 Java 库,并使用 ATAP=2 和 Arduino。 API 模式 2 只是一种 "escaped" 模式,其中有效载荷中的某些字节被转义,作为将它们与 0x7E 帧起始字符区分开来的一种方式。

您甚至可以 运行 Java 以 115200 或 57600bps 结束,并保持 Arduino 以 9600bps 结束。您可以使用 "AT mode" 固件 运行 模块,并使用 "API mode" 从模块与它们通信。无线电模块缓冲数据并以 250kbps 的速度发送 over-the-air,无论您的主机设置如何。