/** * 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.DebugFormat(ToString() + "Sending XBee packet: \n{0}", packet.ToPrettyString()); // Write bytes with the required escaping mode. switch (operatingMode) { case OperatingMode.API: default: var buf = packet.GenerateByteArray(); connectionInterface.SerialPort.Write(buf, 0, buf.Length); break; case OperatingMode.API_ESCAPE: var buf2 = packet.GenerateByteArrayEscaped(); connectionInterface.SerialPort.Write(buf2, 0, buf2.Length); break; } }
//public event EventHandler<PacketReceivedEventArgs> PacketReceived; /** * Notifies subscribed XBee packet listeners that a new XBee packet has * been received. * * @param packet The received XBee packet. * * @see com.digi.xbee.api.packet.XBeeAPIPacket * @see com.digi.xbee.api.packet.XBeePacket */ private void NotifyPacketReceived(XBeePacket packet) { logger.DebugFormat(connectionInterface.ToString() + "Packet received: \n{0}", packet.ToPrettyString()); try { lock (packetReceiveListeners) { var removeListeners = new List<IPacketReceiveListener>(); var tasks = new ConcurrentBag<Task>(); foreach (IPacketReceiveListener listener in packetReceiveListeners.Keys) { tasks.Add(Task.Factory.StartNew((state) => { var listener0 = (IPacketReceiveListener)state; lock (listener0) { if (packetReceiveListeners[listener0] == ALL_FRAME_IDS) listener0.PacketReceived(packet); else if (((XBeeAPIPacket)packet).NeedsAPIFrameID && ((XBeeAPIPacket)packet).FrameID == packetReceiveListeners[listener0]) { listener0.PacketReceived(packet); removeListeners.Add(listener0); } } }, listener)); } Task.WaitAll(tasks.ToArray()); //executor.shutdown(); // Remove required listeners. foreach (IPacketReceiveListener listener in removeListeners) { int value; packetReceiveListeners.TryRemove(listener, out value); } } } catch (Exception e) { logger.Error(e.Message, e); } }