[英]Xbee API - Packet Listener seems not to work
I have a class that is trying to read packet from other Xbees connected with Arduino board. 我有一个正在尝试从与Arduino开发板连接的其他Xbee中读取数据包的类。 Here is the class:
这是课程:
public class XbeeReceiver2 {
private XBee xbee;
private int[] payloadToSend;
private int[] payloadReceived;
private Queue<XBeeResponse> queue = new ConcurrentLinkedQueue<XBeeResponse>();
public XBeeResponse response;
private final static Logger LOGGER = Logger.getLogger(XbeeReceiver2.class .getName());
public XbeeReceiver2(){
xbee = new XBee();
}
public void openXbeeConnection(String portNumber){
try {
xbee.open(portNumber, 9600);
xbee.addPacketListener(new PacketHandler());
} catch (XBeeException e) {
System.out.println("" + portNumber + " - connection problems");
e.printStackTrace();
}
};
public void readPacket(){
System.out.println(queue.isEmpty());
while((response = queue.poll()) != null){
try {
response = xbee.getResponse();
LOGGER.info("received response " + response.toString());
if (response.getApiId() == ApiId.ZNET_RX_RESPONSE){
ZNetRxResponse rx = (ZNetRxResponse) response;
payloadReceived = rx.getData();
}
} catch (XBeeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void closeXbeeConnection(){
xbee.close();
};
private class PacketHandler implements PacketListener{
@Override
public void processResponse(XBeeResponse response) {
//if (response instanceof XBeeResponse){
queue.offer(response);
//}
}
}
}
In the main program, I have the following piece of code: 在主程序中,我有以下代码段:
XbeeReceiver2 xbeeReceiver = new XbeeReceiver2();
xbeeReceiver.openXbeeConnection("COM23");
xbeeReceiver.readPacket();
When I run it, however, the readPacket()
method will get stuck at the beginning of the loop, since there is any instance of XbeeResponse stored in the queue. 但是,当我运行它时,由于队列中存储了XbeeResponse的任何实例,因此
readPacket()
方法将在循环开始时卡住。 However, when I change the while
condition in readPacket() method from while((response = queue.poll()) != null)
to while(true)
, then it works. 但是,当我将readPacket()方法中的
while
条件从while((response = queue.poll()) != null)
更改为while(true)
,它就可以工作。 This means I am getting the response, but I do not think that using while(true)
is the good solution as I read on some pages. 这意味着我正在得到响应,但是我认为在某些页面上阅读时,使用
while(true)
是一个不错的解决方案。 Therefore I prefer to work with PacketListener
. 因此,我更喜欢使用
PacketListener
。
If anyone has an idea, why my PacketListener
does not work, would be great. 如果有人有主意,为什么我的
PacketListener
不起作用,那就太好了。
Ok I have found a solution, it was not that hard. 好吧,我找到了解决方案,这并不难。 Before starting the loop, I got a response from Xbee and added it into queue, so it did not break at the beginning.
在开始循环之前,我从Xbee获得了响应并将其添加到队列中,因此它在开始时并没有中断。 The same I did at the end of while loop, where I added another response.
我在while循环结束时所做的相同,在其中添加了另一个响应。
This is my openConnection
method: 这是我的
openConnection
方法:
public void openXbeeConnection(String portNumber){
try {
xbee.open(portNumber, 57600);
xbee.addPacketListener(new PacketHandler());
response = xbee.getResponse();
queue.add(response);
} catch (XBeeException e) {
System.out.println("" + portNumber + " - connection problems");
e.printStackTrace();
}
};
And in the while loop of the main method it looks like this: 在main方法的while循环中,它看起来像这样:
while ((response = queue.poll()) != null){
//LOGGER.info(response.toString());
int[] payload = xbeeReceiver.readPacket();
dbHandler.updateDB(payload);
int[] payloadToSend2 = dbHandler.retrieveDataFromFB();
xbeeReceiver.sendPacket(payloadToSend2);
try {
XBee xbee = xbeeReceiver.getXbee();
response = xbee.getResponse();
queue.add(response);
} catch (XBeeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.