XBeeSensorData ProcessSensorData(XBeeSensorData sensor, UInt16 channelMask, ref int pos, byte[] data)
        {
            const UInt16 DIGITAL_MASK = 0x1F;
            UInt16 digitalMask = (UInt16)(DIGITAL_MASK & channelMask);

            if (((channelMask & (UInt16)AnalogChannels.A5) != 0))
                sensor.A5 = ReadWordFromArray(data, ref pos);

            if (((channelMask & (UInt16)AnalogChannels.A4) != 0))
                sensor.A4 = ReadWordFromArray(data, ref pos);

            if (((channelMask & (UInt16)AnalogChannels.A3) != 0))
                sensor.A3 = ReadWordFromArray(data, ref pos);

            if (((channelMask & (UInt16)AnalogChannels.A2) != 0))
                sensor.A2 = ReadWordFromArray(data, ref pos);

            if (((channelMask & (UInt16)AnalogChannels.A1) != 0))
                sensor.A1 = ReadWordFromArray(data, ref pos);

            if (((channelMask & (UInt16)AnalogChannels.A0) != 0))
                sensor.A0 = ReadWordFromArray(data, ref pos);


            return sensor;
        }
        private XBeeSensorData ProcessXBeeFrame(byte[] data)
        {
            byte sum = (byte)data.Sum(q => q);
            if (sum != 0xFF) return null;    // invalid checksum

            int pos = 0;

            byte command = data[pos++];
            if (command != 0x82) return null;   // Only take 64-bit receive packets

            UInt64 address = 0;
            for (int i = 0; i < 8; i++)
            {
                address = (address << 8) + data[pos++];
            }
            var sAddr = address.ToString("X");
            byte rssi = data[pos++];
            byte options = data[pos++];


            int sampleCount = data[pos++];

            //UInt16 channelMask = (UInt16)((UInt16)(data[pos++] << 8) + (UInt16)(data[pos++]));
            UInt16 channelMask = ReadWordFromArray(data, ref pos);

            XBeeSensorData sensorData = null;
            for (int i = 0; i < sampleCount; i++)
            {
                sensorData = new XBeeSensorData()
                {
                    Address = address
                };
                ProcessSensorData(sensorData, channelMask, ref pos, data);
            }

            return sensorData;
        }