Recent Changes - Search:

edit SideBar

XBeeJavaAnalysis

Below is an analysis of the calling sequence for XBeeJava.

It turns out, that it is easier to use XBeeLogging.

  • The XBeeDevice constructor invokes the constructor of the superclass, AbstractXBeeDevice, which does the bulk of the constructor work by setting some fields.
            /**
             * Class constructor. Instantiates a new {@code XBeeDevice} object in the
             * given port name and baud rate.
             *
             * @param port Serial port name where XBee device is attached to.
             * @param baudRate Serial port baud rate to communicate with the device.
             *                 Other connection parameters will be set as default (8
             *                 data bits, 1 stop bit, no parity, no flow control).
             *
             * @throws IllegalArgumentException if {@code baudRate < 0}.
             * @throws NullPointerException if {@code port == null}.
             *
             * @see #AbstractXBeeDevice(IConnectionInterface)
             * @see #AbstractXBeeDevice(String, SerialPortParameters)
             * @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress)
             * @see #AbstractXBeeDevice(XBeeDevice, XBee64BitAddress, XBee16BitAddress, String)
             * @see #AbstractXBeeDevice(String, int, int, int, int, int)
             */
            public AbstractXBeeDevice(String port, int baudRate) {
                    this(XBee.createConnectiontionInterface(port, baudRate));
            }

XBee (Javadoc, source creates the connection interface:

       /**
         * Retrieves a serial port connection interface for the provided port with
         * the given baud rate.
         *
         * @param port Serial port name.
         * @param baudRate Serial port baud rate.
         *
         * @return The serial port connection interface.
         *
         * @throws NullPointerException if {@code port == null}.
         *
         * @see #createConnectiontionInterface(String, SerialPortParameters)
         * @see com.digi.xbee.api.connection.IConnectionInterface
         */
        public static IConnectionInterface createConnectiontionInterface(String port, int baudRate) {
                IConnectionInterface connectionInterface = new SerialPortRxTx(port, baudRate);
                return connectionInterface;
        }
SerialPortRxTx (Javadoc) uses the RxTx library.

  • XBeeDevice.open() is below, it creates a DataReader (JavaDoc, Source), which is used later:
           /**
             * Opens the connection interface associated with this XBee device.
             *
             * <p>When opening the device an information reading process is
             * automatically performed. This includes:</p>
             *
             * <ul>
             * <li>64-bit address.</li>
             * <li>Node Identifier.</li>
             * <li>Hardware version.</li>
             * <li>Firmware version.</li>
             * <li>XBee device protocol.</li>
             * <li>16-bit address (not for DigiMesh modules).</li>
             * </ul>
             *
             * @throws InterfaceAlreadyOpenException if this device connection is
             *                                       already open.
             * @throws XBeeException if there is any problem opening this device
             *                       connection.
             *
             * @see #close()
             * @see #isOpen()
             */

            public void open() throws XBeeException {
                    logger.info(toString() + "Opening the connection interface...");

                    // First, verify that the connection is not already open.
                    if (connectionInterface.isOpen())
                            throw new InterfaceAlreadyOpenException();

                    // Connect the interface.
                    connectionInterface.open();

                    logger.info(toString() + "Connection interface open.");

                    // Initialize the data reader.
                    dataReader = new DataReader(connectionInterface, operatingMode, this);
                    dataReader.start();

                    // Wait 10 milliseconds until the dataReader thread is started.
                    // This is because when the connection is opened immediately after
                    // closing it, there is sometimes a concurrency problem and the
                    // dataReader thread never dies.
                    try {
                            Thread.sleep(10);
                    } catch (InterruptedException e) {}

                    // Determine the operating mode of the XBee device if it is unknown.
                    if (operatingMode == OperatingMode.UNKNOWN)
                            operatingMode = determineOperatingMode();

                    // Check if the operating mode is a valid and supported one.
                    if (operatingMode == OperatingMode.UNKNOWN) {
                            close();
                            throw new InvalidOperatingModeException("Could not determine operating mode.");
                    } else if (operatingMode == OperatingMode.AT) {
                            close();
                            throw new InvalidOperatingModeException(operatingMode);
                    }

                    // Read the device info (obtain its parameters and protocol).
                    try {
                            readDeviceInfo();
                    } catch (ATCommandException e) {
                            throw new XBeeException("Error reading device information.", e);
                    }
            }

XBeeDevice.sendBroadCastData() invokes the superclass:

       /**
         * Sends the provided data to all the XBee nodes of the network (broadcast).
         *
         * <p>This method blocks till a success or error transmit status arrives or
         * the configured receive timeout expires.</p>
         *
         * <p>The receive timeout is configured using the {@code setReceiveTimeout}
         * method and can be consulted with {@code getReceiveTimeout} method.</p>
         *
         * @param data Byte array containing the data to be sent.
         *
         * @throws InterfaceNotOpenException if this device connection is not open.
         * @throws NullPointerException if {@code data == null}.
         * @throws TimeoutException if there is a timeout sending the data.
         * @throws XBeeException if there is any other XBee related exception.
         *
         * @see #getReceiveTimeout()
         * @see #setReceiveTimeout(int)
         */

        public void sendBroadcastData(byte[] data) throws TimeoutException, XBeeException {
                sendData(XBee64BitAddress.BROADCAST_ADDRESS, data);
        }

XBeeDevice.sendData() creates an XBeePacket (Javadoc, Source)

       /**
         * Sends the provided data to the XBee device of the network corresponding
         * to the given 64-bit address.
         *
         * <p>This method blocks till a success or error response arrives or the
         * configured receive timeout expires.</p>
         *
         * <p>The receive timeout is configured using the {@code setReceiveTimeout}
         * method and can be consulted with {@code getReceiveTimeout} method.</p>
         *
         * <p>For non-blocking operations use the method
         * {@link #sendDataAsync(XBee64BitAddress, byte[])}.</p>
         *
         * @param address The 64-bit address of the XBee that will receive the data.
         * @param data Byte array containing the data to be sent.
         *
         * @throws InterfaceNotOpenException if this device connection is not open.
         * @throws NullPointerException if {@code address == null} or
         *                              if {@code data == null}.
         * @throws TimeoutException if there is a timeout sending the data.
         * @throws XBeeException if there is any other XBee related exception.
         *
         * @see #getReceiveTimeout()
         * @see #setReceiveTimeout(int)
         * @see #sendData(RemoteXBeeDevice, byte[])
         * @see #sendData(XBee64BitAddress, XBee16BitAddress, byte[])
         * @see #sendDataAsync(RemoteXBeeDevice, byte[])
         * @see #sendDataAsync(XBee64BitAddress, byte[])
         * @see #sendDataAsync(XBee64BitAddress, XBee16BitAddress, byte[])
         * @see com.digi.xbee.api.models.XBee64BitAddress
         */

        protected void sendData(XBee64BitAddress address, byte[] data) throws TimeoutException, XBeeException {
                // Verify the parameters are not null, if they are null, throw an exception.
                if (address == null)
                        throw new NullPointerException("Address cannot be null");
                if (data == null)
                        throw new NullPointerException("Data cannot be null");

                // Check if device is remote.
                if (isRemote())
                        throw new OperationNotSupportedException("Cannot send data to a remote device from a remote device.");

                logger.debug(toString() + "Sending data to {} >> {}.", address, HexUtils.prettyHexString(data));

                XBeePacket xbeePacket;
                switch (getXBeeProtocol()) {
                case RAW_802_15_4:
                        xbeePacket = new TX64Packet(getNextFrameID(), address, XBeeTransmitOptions.NONE, data);
                        break;
                default:
                        xbeePacket = new TransmitPacket(getNextFrameID(), address, XBee16BitAddress.UNKNOWN_ADDRESS, 0, XBeeTransmitOptions.NONE, data);
                }
                sendAndCheckXBeePacket(xbeePacket, false);
        }

Eventually AbstractXBeeDevice.sendXBeePacket() is called, which creates some listeners and then calls AbstractXBeeDevice.writePacket():

        /**
         * Sends the given XBee packet asynchronously and registers the given
         * packet listener (if not {@code null}) to wait for an answer.
         *
         * <p>The method will not wait for an answer for the packet, but the given
         * listener will be notified when the answer arrives.</p>
         *
         * @param packet XBee packet to be sent.
         * @param packetReceiveListener Listener for the operation, {@code null}
         *                              not to be notified when the answer arrives.
         *
         * @throws InterfaceNotOpenException if this device connection is not open.
         * @throws InvalidOperatingModeException if the operating mode is different
         *                                       than {@link OperatingMode#API} and
         *                                       {@link OperatingMode#API_ESCAPE}.
         * @throws IOException if an I/O error occurs while sending the XBee packet.
         * @throws NullPointerException if {@code packet == null}.
         *
         * @see #sendXBeePacket(XBeePacket)
         * @see #sendXBeePacket(XBeePacket, IPacketReceiveListener)
         * @see #sendXBeePacketAsync(XBeePacket)
         * @see com.digi.xbee.api.listeners.IPacketReceiveListener
         * @see com.digi.xbee.api.packet.XBeePacket
         */

        protected void sendXBeePacket(XBeePacket packet, IPacketReceiveListener packetReceiveListener)
                        throws InvalidOperatingModeException, IOException {
                // Check if the packet to send is null.
                if (packet == null)
                        throw new NullPointerException("XBee packet cannot be null.");
                // Check connection.
                if (!connectionInterface.isOpen())
                        throw new InterfaceNotOpenException();

                OperatingMode operatingMode = getOperatingMode();
                switch (operatingMode) {
                case AT:
                case UNKNOWN:
                default:
                        throw new InvalidOperatingModeException(operatingMode);
                case API:
                case API_ESCAPE:
                        // Add the required frame ID and subscribe listener if given.
                        if (packet instanceof XBeeAPIPacket) {

                                        insertFrameID(packet);

                                        XBeeAPIPacket apiPacket = (XBeeAPIPacket)packet;

                                        if (packetReceiveListener != null
                                                        && apiPacket.needsAPIFrameID())
                                                dataReader.addPacketReceiveListener(packetReceiveListener, apiPacket.getFrameID());
                                        else if (packetReceiveListener != null)
                                                dataReader.addPacketReceiveListener(packetReceiveListener);
                        }

                        // Write packet data.
                        writePacket(packet);
                        break;
                }
        }
...
       /**
         * Writes the given XBee packet in the connection interface of this device.
         *
         * @param packet XBee packet to be written.
         *
         * @throws IOException if an I/O error occurs while writing the XBee packet
         *                     in the connection interface.
         *
         * @see com.digi.xbee.api.packet.XBeePacket
         */

        private void writePacket(XBeePacket packet) throws IOException {
                logger.debug(toString() + "Sending XBee packet: \n{}", packet.toPrettyString());
                // Write bytes with the required escaping mode.
                switch (operatingMode) {
                case API:
                default:
                        connectionInterface.writeData(packet.generateByteArray());
                        break;
                case API_ESCAPE:
                        connectionInterface.writeData(packet.generateByteArrayEscaped());
                        break;
                }
        }

Edit - History - Print - Recent Changes - Search
Page last modified on December 27, 2015, at 10:05 pm