Exemplo n.º 1
0
        public XBeePacket(byte[] bytes)
        {
            ushort length = (ushort)bytes.Length;

            XBeeChecksum checksum = new XBeeChecksum();

            checksum.AddBytes(bytes);

            ByteWriter bw = new ByteWriter(length + 1 /* start byte */ + 2 /* bytes for length */ + 1 /* checksum byte */, ByteOrder.BigEndian);

            bw.Write(PACKET_STARTBYTE);
            bw.Write(length);
            bw.Write(bytes);
            bw.Write(checksum.Compute());

            //_packet = EscapePacket(bw.GetBytes());
            _packet = bw.GetBytes();
        }
Exemplo n.º 2
0
        void CheckFrame()
        {
            if (_readBuffer.Count < 4) // we don't have the start byte, the length and the checksum
            {
                return;
            }

            if (_readBuffer[0] != XBeePacket.PACKET_STARTBYTE)
            {
                return;
            }

            ByteReader br = new ByteReader(_readBuffer.ToArray(), ByteOrder.BigEndian);

            br.ReadByte();      // start byte

            short length = br.ReadInt16();

            if (br.AvailableBytes < length + 1) // the frame data and checksum
            {
                return;
            }

            // verify checksum
            XBeeChecksum checksum = new XBeeChecksum();

            byte[] bytes = new byte[length + 1];
            Array.Copy(_readBuffer.ToArray(), 3, bytes, 0, length + 1);
            checksum.AddBytes(bytes);

            Console.WriteLine(checksum.Verify());

            XBeeApiType  apiId = (XBeeApiType)br.Peek();
            XBeeResponse res   = null;

            Console.WriteLine("ApiID = " + apiId.ToString());
            Console.WriteLine("length = " + length);

            switch (apiId)
            {
            case XBeeApiType.ATCommandResponse:
                res = new AtCommandResponse(length, br);
                break;

            case XBeeApiType.NodeIdentificationIndicator:
                res = new NodeIdentification(length, br);
                break;

            case XBeeApiType.ZigBeeReceivePacket:
                res = new ZigbeeReceivePacket(length, br);
                break;

            case XBeeApiType.XBeeSensorReadIndicator:
                res = new XBeeSensorRead(length, br);
                break;

            case XBeeApiType.RemoteCommandResponse:
                res = new AtRemoteCommandResponse(length, br);
                break;

            case XBeeApiType.ZigBeeIODataSampleRxIndicator:
                res = new ZigBeeIODataSample(length, br);
                break;

            default:
                Console.WriteLine("This API frame is unknown.");
                break;
            }

            if (res != null)
            {
                Console.WriteLine(res);
            }

            _readBuffer.RemoveRange(0, length + 1 + 2 + 1);

            Console.WriteLine("waiting " + _readBuffer.Count + " bytes");
        }