2

我正在开发一个使用 XBee S1 作为发射器/接收器的项目。这些是我正在使用的硬件:

  1. 协调器:带有 XBee S1 模块的 XBee USB EXplorer v2.0
  2. 终端设备:带有 XBee 扩展和 XBee S1 模块的Arduino Uno r3.0

该项目的目的很简单,就是打开/关闭LED。这是代码(控制器):

public static void main(String args[]) throws XBeeException,
                                       InterruptedException {
    XBee xbee = new XBee();
    boolean running = true;
    try {
        // OPEN SERIAL PORT
        xbee.open("COM10", 9600); // String arg is
                                  // Unique to YOUR
                                  // MACHINE!

        Scanner input = new Scanner(System.in);
        XBeeAddress16 address = new XBeeAddress16(0x22, 0x22);
        int[] payload;
        payload = new int[1];

        System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn ON");
        payload[0] = (int) input.nextByte();

        TxRequest64 request = new TxRequest64(address, payload);

        System.out.println("\nXBee request is: " + request.getXBeePacket());

        while (running) {
            try {
                TxStatusResponse response = (TxStatusResponse) xbee
                        .sendSynchronous(request, 100);
                request.setFrameId(xbee.getNextFrameId());

                System.out.println("\nXBee request is: " + request.getXBeePacket());

                System.out.println("Response received" + response);

                if (response.getApiId() != ApiId.TX_STATUS_RESPONSE)
                {
                    System.out.println("Response error");
                }
                else
                {
                    System.out.println("Response success");
                }
            }
            catch (XBeeTimeoutException e) {
                System.out.println("Me ! Unable to send");
            }
            System.out.println("Enter a new command for LED: 0 to turn the LED OFF, 1 to turn ON");
            payload[0] = (int) input.nextByte();
            request.setPayload(payload);
            if(payload[0] == 2)
                running = false;
        }
    }
    finally {
        xbee.close();
    }
}

这是 Arduino(终端设备)的代码:

#include <LiquidCrystal.h>
int LED = 12;                //Turn this LED on or off, depending on packet rx'd
int debugLED = 13;           //Flash light to indicate rx
int packet;                 //Packet will be two bytes in length
int analogValue;
int debugValue = 0;
int discard;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  pinMode(LED, OUTPUT);
  pinMode(debugLED, OUTPUT);
  Serial.begin(9600);
  packet = 0x01;
  lcd.begin(16,2);
}

void loop() {
 lcd.setCursor(0, 1); 
 if(Serial.available() >= 16){     //<--Changing the value from 21 to 16 fixed the problem! 

    debugValue = 0;

    debugValue = Serial.read();

    if(debugValue == 0x7e){      //Look for starting byte
      lcd.clear();
      lcd.print(debugValue);
      delay(500);
      digitalWrite(debugLED, HIGH);    //Flash Arduino LED (not the one on digital pin 11)
      delay(1000);                          //to indicate rx'ing data
      digitalWrite(debugLED, LOW);
      //lcd.print(debugValue);


      for(int i = 2; i != 10; i++){      //Discard unused data (see XBee API protocol for more info)
        discard = Serial.read();
        if (i != 8)
          packet = discard;
          // print the number of seconds since reset:
         if(i == 5)
         lcd.setCursor(0,1);
         lcd.print(packet);
         delay(500);
      }
      lcd.print(discard);
   }
 }
 if(packet == 0x7e)
     digitalWrite(LED, HIGH);
 else
     digitalWrite(LED, LOW);
}

问题是 LCD 显示屏中的输出总是显示相同的输出。怎么可能?

这些是我从 LCD 得到的“奇怪”数字:

126 0 6 129 125 49 125 49 125 125 29 29

这些值保持不变,艰难我已将输入值从 0 更改为 1!

这是我的 Java 终端(控制台)的示例输出:

1

XBee request is: 0x7e,0x00,0x06,0x01,0x02,0x22,0x22,0x00,0x01,0xb7
Response receivedapiId=TX_STATUS_RESPONSE (0x89),length=3,checksum=0x75,error=false,frameId=0x01,status=SUCCESS
Response success
Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on
0

XBee request is: 0x7e,0x00,0x06,0x01,0x03,0x22,0x22,0x00,0x00,0xb7
Response receivedapiId=TX_STATUS_RESPONSE (0x89),length=3,checksum=0x74,error=false,frameId=0x02,status=SUCCESS
Response success
Enter a new command for LED: 0 to turn the LED OFF, 1 to turn on

我确定这是有效载荷值(标有 ^^^^):

0x7e,0x00,0x06,0x01,0x02,0x22,0x22,0x00,0x01,0xb7
                                        ^^^^

但是如何用 Arduino 获得这个价值呢?

无论如何,我参考了教程XBee API Project One

4

1 回答 1

2

好吧,在做了一些搜索之后,通过 Arduino 获取 XBee 有效载荷值的最佳方法是使用XBee API for Arduino,因为我阅读了这个网站:使用 Arduino XBee API 无线传输模拟信号。这是获取“有效负载”值的代码:

#include<Xbee.h>
int DEBUG = 13;
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
Rx16Response rx16 = Rx16Response();

void setup() {
  pinMode(DEBUG, OUTPUT);
  Serial.begin(9600);
  DEBUG = 0;
}

void loop() {
 xbee.readPacket();

 if(xbee.getResponse().isAvailable()){
   lcd.print("Data received");
     if(xbee.getResponse().getApiId() == RX_16_RESPONSE){
       xbee.getResponse().getRx16Response(rx16);

       uint8_t payload = rx16.getData(0);//this is the payload value, easy to get it :D
     }
 }     
}

那是!!:D

于 2013-04-15T18:17:29.790 回答