public XBeePacket ParsePacket(SerialPort serialPort, OperatingMode mode)
        {
            Contract.Requires <ArgumentNullException>(serialPort != null, "Input stream cannot be null.");
            Contract.Requires <ArgumentException>(serialPort.IsOpen);
            Contract.Requires <ArgumentException>(mode == OperatingMode.API || mode == OperatingMode.API_ESCAPE, "Operating mode must be API or API Escaped.");

            try
            {
                // Read packet size.
                int hSize  = ReadByte(serialPort, mode);
                int lSize  = ReadByte(serialPort, mode);
                int Length = hSize << 8 | lSize;

                // Read the payload.
                byte[] payload = ReadBytes(serialPort, mode, Length);

                // Calculate the expected checksum.
                XBeeChecksum checksum = new XBeeChecksum();
                checksum.Add(payload);
                byte expectedChecksum = (byte)(checksum.Generate() & 0xFF);

                // Read checksum from the input stream.
                byte readChecksum = (byte)(ReadByte(serialPort, mode) & 0xFF);

                // Verify the checksum of the read bytes.
                if (readChecksum != expectedChecksum)
                {
                    throw new InvalidPacketException("Invalid checksum (expected 0x"
                                                     + HexUtils.ByteToHexString(expectedChecksum) + ").");
                }

                return(ParsePayload(payload));
            }
            catch (IOException e)
            {
                throw new InvalidPacketException("Error parsing packet: " + e.Message, e);
            }
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of class <see cref="XBeePacket"/>.
 /// </summary>
 protected XBeePacket()
 {
     checksum = new XBeeChecksum();
 }
        public XBeePacket ParsePacket(SerialPort serialPort, OperatingMode mode)
        {
            Contract.Requires<ArgumentNullException>(serialPort != null, "Input stream cannot be null.");
            Contract.Requires<ArgumentException>(serialPort.IsOpen);
            Contract.Requires<ArgumentException>(mode == OperatingMode.API || mode == OperatingMode.API_ESCAPE, "Operating mode must be API or API Escaped.");

            try
            {
                // Read packet size.
                int hSize = ReadByte(serialPort, mode);
                int lSize = ReadByte(serialPort, mode);
                int Length = hSize << 8 | lSize;

                // Read the payload.
                byte[] payload = ReadBytes(serialPort, mode, Length);

                // Calculate the expected checksum.
                XBeeChecksum checksum = new XBeeChecksum();
                checksum.Add(payload);
                byte expectedChecksum = (byte)(checksum.Generate() & 0xFF);

                // Read checksum from the input stream.
                byte readChecksum = (byte)(ReadByte(serialPort, mode) & 0xFF);

                // Verify the checksum of the read bytes.
                if (readChecksum != expectedChecksum)
                    throw new InvalidPacketException("Invalid checksum (expected 0x"
                                + HexUtils.ByteToHexString(expectedChecksum) + ").");

                return ParsePayload(payload);

            }
            catch (IOException e)
            {
                throw new InvalidPacketException("Error parsing packet: " + e.Message, e);
            }
        }
 /// <summary>
 /// Initializes a new instance of class <see cref="XBeePacket"/>.
 /// </summary>
 protected XBeePacket()
 {
     checksum = new XBeeChecksum();
 }