从xbee读取包API到处理

时间:2014-07-07 07:37:28

标签: xbee

我正在使用IMU传感器,我正试图通过xbee发送3个值。 传感器使用I2C协议进行配置。 电路原理图如下:IMU-arduino uno-xbee router-xbee coordinator- proccesing。 我也想使用proccesing软件读取值。 我正在使用arduino和Xbee中的Rx和Tx端口发送数据。 问题是我无法读取我发送的值。 到目前为止,这是我写的代码。

import processing.serial.*; 
import org.apache.log4j.PropertyConfigurator;
import java.util.concurrent.ConcurrentLinkedQueue;
import com.rapplogic.xbee.api.ApiId;
import com.rapplogic.xbee.api.PacketListener;
import com.rapplogic.xbee.api.XBee;
import com.rapplogic.xbee.api.XBeeResponse;
import com.rapplogic.xbee.api.zigbee.ZNetRxIoSampleResponse;



XBee xbee;
Queue<XBeeResponse> queue = new ConcurrentLinkedQueue<XBeeResponse>();
boolean message;
XBeeResponse response;

void setup() {
try { 
//optional.  set up logging
PropertyConfigurator.configure(dataPath("log4j.properties")); 
xbee = new XBee();
// replace with your COM port
xbee.open("COM20", 115200);

xbee.addPacketListener(new PacketListener() {
public void processResponse(XBeeResponse response) {
queue.offer(response);
}
}
);
} 
catch (Exception e) {
System.out.println("XBee failed to initialize");
e.printStackTrace();
System.exit(1);
}
}

void draw() {
try {
 readPackets();
} 
catch (Exception e) {
e.printStackTrace();
}
}

void readPackets() throws Exception {

while ((response = queue.poll()) != null) {
// we got something!
try {
  RxResponseIoSample ioSample = (RxResponseIoSample) response;

  println("We received a sample from " + ioSample.getSourceAddress());

  if (ioSample.containsAnalog()) {
    println("10-bit temp reading (pin 19) is " +
      ioSample.getSamples()[0].getAnalog1());
  }
 } 
 catch (ClassCastException e) {
  // not an IO Sample
}
}
}

提前致谢!

0 个答案:

没有答案