【问题标题】:Two xbees ( Router At) connected to Xbee(coordinator)两个 xbee(Router At)连接到 Xbee(协调器)
【发布时间】:2015-04-09 05:23:54
【问题描述】:

我一直在从事一个涉及 xbees 的项目。我的设置只是一个连接到 Arduino 作为主单元的 Xbee(协调器 Api 模式)。该主单元接收来自多个 Xbee(从属)的数据,这些 Xbee(从属)仅由电池供电并从 ADC 引脚 17 读取数据。 ADC 值被传输到主 xbee 以在串行终端上显示。从站 Xbee 配置为(路由器 AT 模式)。我只在两个 xbee 之间做这个:1 个主控和 1 个从控。我有一个代码可以读取发送 Xbee 的 mac 地址,然后显示发送的发送 ADC 值。在我添加另一个从机之前,这一切都很好,我真的需要帮助,因为我无法将每个 mac 地址与正确的 ADC 值相关联。我的代码都不能同时读取两者;在某些时候,它会停止从一个读取。如果对如何识别来自同一网络上的多个 xbee 的数据有任何建议,我将不胜感激。 这是我的代码:

#include <XBee.h>
#include <SoftwareSerial.h>

SoftwareSerial myserial(5,6);

float distance;

uint8_t myaddress[10];

XBee xbee = XBee();

uint8_t shCmd[] = {'S','H'};
uint8_t slCmd[] = {'S','L'};
AtCommandRequest atRequestSH = AtCommandRequest(shCmd);
AtCommandRequest atRequestSL = AtCommandRequest(slCmd);
AtCommandResponse atResponse = AtCommandResponse();

void getMyAddress(){
  xbee.send(atRequestSL);

  if(xbee.readPacket(5000)){
      xbee.getResponse().getAtCommandResponse(atResponse);
      if (atResponse.isOk()){
        for(int i = 0; i < atResponse.getValueLength(); i++){
          myaddress[i] = atResponse.getValue()[i];
        }
      }
  }
  delay(100);
}

void setup(){
Serial.begin(1200);
myserial.begin(1200);
xbee.begin(myserial);
}

void loop() {
  getMyAddress();
  for(int i=0; i < 10; i++) {
      Serial.print(myaddress[i], HEX);
      Serial.print(" ");
  }

Serial.print("\n");

if (myserial.available() >= 21) { //
if (myserial.read() == 0x7E) { 
  for (int i = 1; i<19; i++) { // Skip ahead to the analog data

    byte discardByte = myserial.read();
}
  int analogMSB = myserial.read(); // Read the first analog byte data
  int analogLSB = myserial.read(); // Read the second byte
  int analogReading = analogLSB + (analogMSB * 256);
  distance = ((analogReading *1.0) / 1023.0)* 3.3;
  Serial.println(distance);

}
 } 
}  

【问题讨论】:

    标签: arduino arduino-uno xbee software-serial


    【解决方案1】:

    在您跳过数据以获取模拟值的代码中,您会找到发送数据的设备的 MAC 地址。

    if (myserial.read() == 0x7E) { 
      for (int i = 1; i<19; i++) { // Skip ahead to the analog data
    
        byte discardByte = myserial.read();
    }
    

    在处理数据之前,您应该仔细查看该数据 - 通过查看帧类型、检查帧长度、检查帧末尾的校验和等,确保它是一个 I/O 样本。您使用的库似乎具有处理 AT 命令和响应的函数,也许它还具有 I/O 示例帧类型的函数。

    Digi support page 解释了 I/O 采样并记录了 IO Data Sample Received 帧(类型 0x92)中的各个字段。

    【讨论】:

    • 在一周内尝试遵循您的建议后,我设法让它发挥了作用。非常感谢:)
    • 那么您可能想发布自己问题的答案,显示有效的代码,以便将来搜索的人可以看到您是如何解决问题的。
    • @AboorahAbeer 我知道这已经有一段时间了,但如果你能发布你问题的答案,这对我非常有帮助,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-09-15
    • 2018-07-22
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多