/// <summary> /// Function for PacketListener to loop in /// </summary> private void Run() { try { stop = false; while (!stop) { byte[] rawPacket = TryReadPacket(xbeeDevice.OpMode); if (rawPacket != null) { // If the protocol is 802.15.4, check if the packet is valid. If not, skip it if (xbeeDevice.Protocol == XBeeProtocol.Byte.RAW_802_15_4 && !CheckPacket802_15_4(rawPacket)) { continue; } // Build the packet XBeeAPIPacket readPacket; try { readPacket = (XBeeAPIPacket)Factory.BuildFrame(rawPacket, xbeeDevice.OpMode); } catch (InvalidPacketException e) { Log.Error(String.Format("Error processing packet {0} : {1}", Utils.BytesToHexString(rawPacket, true), e.Message)); continue; } Log.Debug(String.Format(XBeeDevice.LOG_PATTERN, xbeeDevice.ComPort.PortName, "RECEIVED", OperatingMode.Get(xbeeDevice.OpMode), Utils.BytesToHexString(rawPacket, true))); // Add the packet to the queue AddPacketQueue(readPacket); // If the packet has information about a remote device, extract it and update the remote device RemoteXBeeDevice remote = TryAddRemoteDevice(readPacket); // Execute callbacks to process API packets OnPacketReceivedAPI(readPacket); ExecuteUserCallbacks(readPacket, remote); } } } catch (Exception e) { if (!stop) { Log.Error(e); Console.Write(e.StackTrace); } } finally { if (!stop) { stop = true; if (comPort.IsOpen) { comPort.Close(); } } } }