简体   繁体   中英

Xbee communication using Digi xbee-java api

I have configured one xbee pro as coordinator (API mode) and other as router (API mode). 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. 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). 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?

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? 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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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