Ejemplo n.º 1
0
        /// <summary>
        /// Generates the XBee packet byte array.
        /// </summary>
        /// <remarks>Use only while working in API mode 1. If API mode is 2, use<see cref="GenerateByteArrayEscaped"/>.</remarks>
        /// <returns>The XBee packet byte array.</returns>
        /// <seealso cref="GenerateByteArrayEscaped"/>
        public byte[] GenerateByteArray()
        {
            checksum.Reset();
            byte[] packetData = GetPacketData();

            using (var os = new MemoryStream())
            {
                os.WriteByte(SpecialByte.HEADER_BYTE.GetValue());
                if (packetData != null)
                {
                    byte[] Length = ByteUtils.ShortToByteArray((short)packetData.Length);
                    var    msb    = Length[0];
                    var    lsb    = Length[1];
                    os.WriteByte(msb);
                    os.WriteByte(lsb);
                    for (int i = 0; i < packetData.Length; i++)
                    {
                        checksum.Add(packetData[i]);
                        os.WriteByte(packetData[i]);
                    }
                }
                else
                {
                    os.WriteByte(0);
                    os.WriteByte(0);
                }
                os.WriteByte((byte)(checksum.Generate() & 0xFF));
                return(os.ToArray());
            }
        }
        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);
            }
        }
        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);
            }
        }