Arduino-Xbee- Java通信

时间:2018-09-20 04:15:28

标签: java arduino xbee

我对此并不陌生,我正在尝试制作一个简单的程序,该程序将向arduino发送命令,然后通过xbee将响应发送回Java。我可以将命令发送到arduino,但无法读取它的响应。我正在使用具有API-2配置的XBee S2C。如何在Java中读取来自arduino的响应?

这是我的Java代码:

public class Transmitdataxbee {
    private static final String PORT = "COM8";
    private static final int BAUD_RATE = 9600;
    private static RemoteXBeeDevice myremote;
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        XBeeDevice myDevice = new XBeeDevice(PORT, BAUD_RATE);
        Scanner input_rpi = new Scanner(System.in);
        String data;

        try {
            myDevice.open();
            XBeeNetwork myXBeeNetwork = myDevice.getNetwork();
            myXBeeNetwork.setDiscoveryTimeout(10000);
            myXBeeNetwork.startDiscoveryProcess();

            while (myXBeeNetwork.isDiscoveryRunning()) {
                System.out.println("Discovering devices...");

            }
            myremote = myXBeeNetwork.getDevice(new XBee64BitAddress("0013A20041768E48"));
            String nodeIdentifier = myremote.getNodeID();
            System.out.print("Node ID: ");
            System.out.println(nodeIdentifier);

            System.out.println("Enter Command");
            data = input_rpi.next();

            myDevice.sendData(myremote, data.getBytes());

            System.out.println("Current timeout: " + myDevice.getReceiveTimeout() + "milliseconds");

            //read from arduino
            XBeeMessage edMessage = myDevice.readDataFrom(myremote);
            String data_ed = edMessage.getDataString();
            System.out.println(data_ed);



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

这是我对arudino的代码

#include <XBee.h>

XBee xbee = XBee();
ZBRxResponse rx = ZBRxResponse();
XBeeAddress64 test = XBeeAddress64(0x0013A200, 0x41768E6E);

ModemStatusResponse msr = ModemStatusResponse();

uint8_t data;
char cmd1[9];
String cmd;
char d_ata;
int j = 0;
int icount = 0;
int count = 32;

void setup() {
     Serial.begin(9600);
     Serial2.begin(9600);
     xbee.setSerial(Serial2);
     xbee.setAPImode(2);
     Serial.println("Connecting....");
}

void loop() {
    xbee.readPacket(50);
    if (xbee.getResponse().isAvailable()){
        Serial.println("Connected");
        Serial.println("Getting Message...");

        if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
            xbee.getResponse().getZBRxResponse(rx);
            Serial.println("Packet received!");

            if(rx.getOption() == ZB_PACKET_ACKNOWLEDGED){
                Serial.println("Packet acknowledged");
            }
            cmd = "";
            Serial.println("Received Data: ");
            for (int i = 0; i < rx.getDataLength(); i++) {
                //print8Bits(rx.getData()[i]);
                cmd1[i] = (char) rx.getData()[i];
                cmd += cmd1[i];
                Serial.println(cmd);
                Serial.println();
            }
            if (cmd == "a") {
                data = "l";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }
            else if (cmd == "w"){
                data = "u";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }
            else if (cmd == "s"){
               data = "d";
               ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
               xbee.send(zbtx); 
            }
            else if (cmd == "d"){
                data = "r";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }
            else{
                data="e";
                ZBTxRequest zbtx = ZBTxRequest(test,data, sizeof(data));
                xbee.send(zbtx);
            }  
        }
    } else if (xbee.getResponse().isError()) {
       // some kind of error happened, I put the stars in so
       // it could easily be found
       Serial.print("************************************* error code:");
       Serial.println(xbee.getResponse().getErrorCode(),DEC);
    }
}

1 个答案:

答案 0 :(得分:0)

经过几次重新搜索,我发现了Digi的一些文档可能会对您有所帮助。

正如我在第一句话中所认为的那样,问题似乎出在您试图从设备读取数据的部分。

根据本文档:https://www.digi.com/resources/documentation/digidocs/90001438/reference/r_xb_java_lib_data_reception_callback.htm

您必须创建一个数据侦听器,然后将其注册到XBeeDevice。

它们甚至还给您一些有关XBeeMessage信息的提示。

我不知道您的Java等级,但是上面的链接中提供了所有代码。

如果您想了解有关侦听器的更多信息,请检查以下内容:What is the purpose of a listener in Java?