Search code examples
javaxbee

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. 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. However, when I change the while condition in readPacket() method from while((response = queue.poll()) != null) to while(true), then it works. 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. Therefore I prefer to work with PacketListener.

If anyone has an idea, why my PacketListener does not work, would be great.


Solution

  • 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. The same I did at the end of while loop, where I added another response.

    This is my openConnection method:

    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:

    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();
                }