简体   繁体   English

使用Digi xbee-java api进行Xbee通信

[英]Xbee communication using Digi xbee-java api

I have configured one xbee pro as coordinator (API mode) and other as router (API mode). 我已将一个xbee pro配置为协调器(API模式),将另一个配置为路由器(API模式)。 I trying to send data from coordinator to router using xbee java api, but in the router code i keep getting null, am I doing something wrong. 我试图使用xbee java api将数据从协调器发送到路由器,但是在路由器代码中我一直为空,这是在做错什么。 Below is the code for Sending data (coordinator): 以下是发送数据的代码(协调器):

public class MainApp {
private static final String PORT = "/dev/ttyUSB0";
private static final int BAUDRATE = 9600;

public static void main(String[] args)
{
    String data = "Helloww";

    XBeeDevice mycord = new XBeeDevice(PORT, BAUDRATE);     

    try {
        mycord.open();
        System.out.println("Port is opened\n");
        System.out.println("remote device connection\n");
        //mac of my router
        RemoteXBeeDevice router = new RemoteXBeeDevice(mycord,
                new XBee64BitAddress("0013A20040DD9BDD"));
        System.out.println("Sending data\n");
        mycord.sendData(router, data.getBytes());

    } catch (XBeeException e) {
        e.printStackTrace();
        mycord.close();
        System.exit(1);
    }
}

} }

code on router side 路由器端的代码

public class RecvApp {
private static final String PORT = "/dev/ttyUSB1";
private static final int BAUDRATE = 9600;

public static void main(String[] args)
{
    XBeeDevice myrouter = new XBeeDevice(PORT, BAUDRATE);

        try {
            myrouter.open();
            System.out.println("router port opened\n");
            //mac of coordinator
            RemoteXBeeDevice remotecord = new RemoteXBeeDevice(myrouter, new XBee64BitAddress("0013A20040D96FE5"));
            XBeeMessage msg = myrouter.readDataFrom(remotecord);
            System.out.print(msg);

        } catch (XBeeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            myrouter.close();
            System.exit(1);
        }
      }
   }

On the router you need to have a loop that checks for messages and prints them out. 在路由器上,您需要有一个循环来检查消息并打印出来。 The API should have a method you can call to check for messages before calling readDataFrom() (or maybe you just ignore the null response). API应该有一个方法,您可以在调用readDataFrom()之前调用该方法来检查消息(或者您可能只是忽略null响应)。 Sleep for a few milliseconds between each check. 每次检查之间睡眠几毫秒。 Right now, there isn't much opportunity for your message to come through before the program quits. 目前,在程序退出之前,您没有太多机会传达您的信息。

When debugging something like this, start by isolating your problem. 当调试类似的东西时,首先要隔离问题。 Which side is failing, the coordinator or the router? 哪一方发生故障,协调器还是路由器? Are you sure the XBee modules have joined to each other and are on the same network? 您确定XBee模块已相互连接并在同一网络上吗?

One test would be to run a simple terminal emulator on the serial port connected to the router, do you see any frames coming through? 一种测试是在连接到路由器的串行端口上运行一个简单的终端仿真器,您是否看到任何帧通过? If you look at a hex dump of the bytes, do you see your "Helloww" message? 如果查看字节的十六进制转储,是否看到“ Helloww”消息? If not, you need to get the coordinator working first before you debug your router. 如果没有,您需要先使协调器工作,然后再调试路由器。

Found the issue, I was not converting the message received in the correct format. 发现了问题,我没有转换以正确格式接收的消息。 Added the below lines 添加了以下几行

String content = HexUtils.prettyHexString(HexUtils.byteArrayToHexString(xbeeMessage.getData()));
    System.out.println("Hex data" + "" + content + "\n");
    String value = new String(xbeeMessage.getData());
    System.out.print("Actual msg" + " " + value + "\n");

Works now :) 现在可以工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM