2013-04-06 58 views
2

我正在開發一個使用XBee S1作爲發送器/接收器的項目。這是我使用的硬件:在Arduino UNO上獲取XBee數據包的有效載荷值r3

  1. 協調員:的XBee USB 2.0 Explorer公司用的XBee模塊S1
  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

回答

2

那麼,在做一些搜索之後,通過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