예제 #1
0
        public void TestXBeeChecksumVerifyFail()
        {
            var packet = new byte[] { 0x83, 0x56, 0x78, 0x24, 0x00, 0x01, 0x02, 0x00, 0x03, 0xff, 0x83 };

            Assert.That(XBeeChecksum.Verify(packet), Is.False);
        }
예제 #2
0
        private 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);

            if (!checksum.Verify())
            {
                //TODO: ERRO no quadro. Limpar os recursos e retornar função
                return;
            }

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

            //TODO: implementar a descoberta de dispositivos
            switch (apiId)
            {
            case XBeeApiType.ATCommandResponse:
                res = new AtCommandResponse(length, br);

                if (res.Command == "ND")
                {
                    byte[] dados = res.Value;
                }

                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);

                if (res.Command == "IS")
                {
                    XBee.XBee newDev;

                    if (devices.TryGetValue(res.Address64, out newDev))
                    {
                        devices[res.Address64].SetIOStatus(res.Value);
                    }
                    else
                    {
                        newDev = new XBee.XBee(res.Address64, res.Address16);
                        devices.Add(res.Address64, newDev);
                        devices[res.Address64].SetIOStatus(res.Value);     //repetido
                    }
                }

                break;

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

            case XBeeApiType.APIIOSupportReceivePacket64bits:
                res = new APIIOSupportReceivePacket64bits(length, br);
                devices[res.Address64].SetIOStatus(res.Value);
                break;

            default:
                //TODO: tratar erro na leitura do pacote
                break;
            }

            if (res != null)
            {
                objAtual = res.ToString();
                DataReceived(res.ToString());
            }

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