Esempio n. 1
0
        static void xbee_OnPacketReceived(object sender, FrameReceivedEventArgs e)
        {
            XBeeResponse response = e.Response;

            if (response != null)
            {
                Console.WriteLine(response.ToString() + "\r\n==============================");
            }

            AtCommandResponse res = response as AtCommandResponse;

            if (res != null)
            {
                //if (res.ParseValue() is ZNetNodeDiscover)
                //{
                //    ZNetNodeDiscover nd = res.ParseValue() as ZNetNodeDiscover;

                //    if (nd.NodeIdentifier == "SENSOR")
                //    {
                //        (sender as XBee).Execute(new RemoteAtRequest(nd.SerialNumber, nd.ShortAddress, new ForceSample()));
                //        //sender.SendCommand(new AtRemoteCommand(nd.SerialNumber, nd.ShortAddress, new XBeeSensorSample()));
                //    }
                //    else
                //    {
                //        ZNetTxRequest x = new ZNetTxRequest(nd.SerialNumber, nd.ShortAddress, Encoding.ASCII.GetBytes(DateTime.Now.ToLongTimeString()));
                //        (sender as XBee).Execute(x);
                //    }

                //}
                return;
            }

            RemoteAtResponse res2 = response as RemoteAtResponse;

            if (res2 != null)
            {
                //if (res2.ParseValue() is ForceSampleData)
                //{
                //    ForceSampleData d = res2.ParseValue() as ForceSampleData;

                //    double mVanalog = (((float)d.AD2) / 1023.0) * 1200.0;
                //    double temp_C = (mVanalog - 500.0) / 10.0 - 4.0;
                //    double lux = (((float)d.AD1) / 1023.0) * 1200.0;

                //    mVanalog = (((float)d.AD3) / 1023.0) * 1200.0;
                //    double hum = ((mVanalog * (108.2 / 33.2)) - 0.16) / (5 * 0.0062 * 1000.0);

                //    temperature = temp_C;
                //}
            }
        }
Esempio n. 2
0
        public bool SpoolMessage(MailMessage message, out string reply)
        {
            reply = null;

            string subject = message.Subject;

            if (String.IsNullOrEmpty(subject))
            {
                reply = "Missing subject";
                return(false);
            }

            string[] parts = subject.Split('|');

            if (parts.Length == 0)
            {
                reply = "Missing command arguments seperator";
                return(false);
            }

            if (parts[0] == "NI")
            {
                using (XBee xbee = new XBee("COM4", ApiType.Enabled))
                {
                    xbee.Open();

                    XBeeResponse res = xbee.Execute(new NodeIdentifierCommand());

                    if (res == null)
                    {
                        reply = "Could not execute NI command";
                        return(false);
                    }

                    AtCommandResponse atr = res as AtCommandResponse;

                    if (atr != null)
                    {
                        NodeIdentifier ni = NodeIdentifier.Parse(atr);

                        if (ni != null)
                        {
                            reply = "XBee module response: " + ni.GetType() + " = " + ni.Identifier;
                            return(false);
                        }
                    }
                }
            }

            return(true);
        }
Esempio n. 3
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);
        }