Main /
XBeeJavaAnalysisBelow is an analysis of the calling sequence for XBeeJava. It turns out, that it is easier to use XBeeLogging.
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; }
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; } } |