Serial port 将XBee数据读入处理

Serial port 将XBee数据读入处理,serial-port,processing,xbee,Serial Port,Processing,Xbee,我最近制作了一个Tweet a Watt()无线电源监视器,它使用XBee进行数据传输。我试图将Tweet中的一瓦特数据进行处理,用于创建一些视觉能量反馈原型。使用XBEEAPI库进行处理(),我已经取得了一些进展,但遇到了一个障碍,我希望您能提供一些帮助 我的处理草图如下所示: /* XBee通信原型 Daniel Shiffman和Rob Faludi编写的XBee API库:http://www.faludi.com/code/xbee-api-library-for-processing

我最近制作了一个Tweet a Watt()无线电源监视器,它使用XBee进行数据传输。我试图将Tweet中的一瓦特数据进行处理,用于创建一些视觉能量反馈原型。使用XBEEAPI库进行处理(),我已经取得了一些进展,但遇到了一个障碍,我希望您能提供一些帮助

我的处理草图如下所示:

/*
XBee通信原型
Daniel Shiffman和Rob Faludi编写的XBee API库:http://www.faludi.com/code/xbee-api-library-for-processing/
改编自Tom Igoe的XBee通信代码示例:http://www.tigoe.net/pcomp/code/category/Processing/148
*/
//导入xbee和串行库:
导入xbee。*;
输入处理。串行。*;
//设置Xbee参数:
串口;
XbeerLeader xbee;
int rssi=0;//接收信号强度
int地址=0;//发信人地址
int samples=0;//样本总数
int[]模拟;//来自模拟I/O引脚的值
无效设置(){
//设置xbee
端口=新的串行(这是Serial.list()[0],9600);
xbee=新的XBEREADER(此端口);
xbee.startXBee();
}
void draw(){}
//每次接收到XBee事件时调用:如果是Tweet,则每2秒调用一瓦特
公共无效xBeeEvent(XBeeReader xbee){
//抓取一帧数据
XBeeDataFrame data=xbee.getXbeerReading();
println(“”);
println(“循环”+hour()+:“+minute()+”:“+second());
//获取发送器地址
address=data.getAddress16();
println(“API ID:+地址);
//获取RSSI
rssi=data.getRSSI();
println(“RSSI:+RSSI”);
//获取样本总数
samples=data.getTotalSamples();
println(“总样本:+样本”);
//输出每个样本的模拟读数
//仅获取第一个样本-如何访问所有样本?
对于(int i=0;i样本;i++){
模拟=数据。getAnalog(i);
打印(“[”);
对于(int j=0;j模拟长度;j++){
打印(模拟[j]);
如果(j模拟长度-1){print(“,”);}
}
打印(“]”);
如果(i样本-1){print(“,”;}
else{println(“”;}
}
}
这一切都按预期进行。xBeeEvent每2秒调用一次,并输出API ID、RSSI和总采样数的正确值(19)。然而,当输出模拟读数的内容时,我似乎得到了重复19次的第一个样本。请参见此示例输出:

循环10:37:57
API ID:1
RSSI:-61
样本总数:19
[512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1], [512, -1, -1, -1, 688, -1]
环路10:38:59
API ID:1
RSSI:-61
样本总数:19
[503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1], [503, -1, -1, -1, 561, -1]
如您所见,第一个示例重复了19次。从Tweet运行原始Python脚本A Watt软件(watcher.py)会输出类似的XBee数据包读数,但有19个不同的示例。这是我在处理过程中试图达到的状态

在XBee API库中,getAnalog()和getAnalog(n)函数定义如下:

getAnalog() – returns an array of integers that represents the current state of each analog channel with -1 indicating that the channel is not configured for analog. Use this when there is only one sample per frame. getAnalog(int n) – returns the nth sample of analog data as an array of integers with -1 indicating that the channel is not configured for analog. getAnalog()–返回一个整数数组,该数组表示每个模拟通道的当前状态,其中-1表示该通道未配置为模拟。当每帧只有一个采样时,使用该数组。 getAnalog(int n)–以整数数组的形式返回模拟数据的第n个样本,-1表示通道未配置为模拟。 我在for循环中使用了getAnalog(int n)。在调用XBeeDataFrame data=xbee.getXbeerReading();,是否只获得了一帧数据

我也尝试过不使用XBee API库直接读取串行数据包(参考()、()和()),但由于我在这方面缺乏经验,这有点不太可能


如果任何熟悉XBee数据包、XBee API库或在处理过程中读取串行数据的人都能提供帮助,我将不胜感激。我希望数据在那里,我只是没有正确地访问它。我意识到这是一个非常具体的问题,我已经在Adafruit(推特的制造商a Watt kit-)上发布了它和处理()论坛,但经过几十次的浏览,我还没有收到任何回复,所以我想我应该把网络扩大一点。

在我的书中,我重点介绍了使用Andrew Rapp创建的更完整的功能。它们涵盖了系列1和系列2的无线电,提供了一整套API交互。使用这些库的代码示例可以在网站上找到