我一直在做一个涉及xbees的项目。我的设置是一个简单的Xbee (协调器Api模式),连接到一个Arduino作为主单元。这个主单元从多个仅由电池供电的Xbees (从)接收数据,并从那里的A/D转换器pin17读取数据。ADC值被传输到主xbee,以便在串行端子上显示。从设备Xbee配置为(路由器AT模式)。我只在两个xbees之间这样做:一个主机和一个从机。我有一个代码,它读取发送的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);
}
}
} 发布于 2015-04-09 05:23:16
在您跳过数据以获取模拟值的代码中,您将找到发送数据的设备的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数据样本帧(类型0x92)中的各个字段。
https://stackoverflow.com/questions/29500617
复制相似问题