【问题标题】:Get payload value of an XBee packet on Arduino UNO r3在 Arduino UNO r3 上获取 XBee 数据包的有效负载值
【发布时间】:2013-03-28 18:06:37
【问题描述】:

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

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

项目的目的很简单,就是打开/关闭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 获得的“奇怪”数字:

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

【问题讨论】:

    标签: java arduino xbee


    【解决方案1】:

    好吧,经过一番搜索,通过 Arduino 获取 XBee 有效载荷值的最佳方法是使用 XBee API for Arduino,正如我阅读此网站:wirelessly transmit analog signals using the 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-22
      • 2015-06-27
      • 1970-01-01
      • 2016-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      相关资源
      最近更新 更多