Esempio n. 1
0
        //Compare the FCS value to the measured one and see if the packet is valid
        public static bool isPacketValid(byte[] packet)
        {
            byte[] justGFF = new byte[packet.Length - 2];
            for (int i = 1; i < packet.Length - 2; i++)             //Isolate the GFF to its own array (not the most efficient way to do this)
            {
                justGFF[i - 1] = packet[i];
            }
            byte outputVal = AltCOM.FCSgenerate(justGFF);     //Calculate the FCS

            if (outputVal == packet[packet.Length - 1])
            {
                return(true);                                //If the calculated value matches the read value, the packet is valid
            }
            return(false);                                   //Otherwise return false
        }
Esempio n. 2
0
 public static void wirelessTransmit()
 {
     Console.WriteLine("Transmit Thread Started");
     while (Program.runTranceiver)
     {
         Thread.Sleep(800);
         AltCOM.genCom(wirelessIn, AltCOM.ZB_SEND_DATA(0xFFFD, 2, 0, 1, 1, Encoding.UTF8.GetBytes(deviceData)));
         while (!AltGET.isMessage(rxPacket, mDef.ZB_SEND_DATA_RSP))
         {
             ;
         }
         while (!AltGET.isMessage(rxPacket, mDef.ZB_SEND_DATA_CONFIRM))
         {
             ;
         }
     }
 }
Esempio n. 3
0
        public static void initALT5801(bool isCoordinator)
        {
            //Run the full initialization procedure for the ALT5801
            AltCOM.genCom(wirelessIn, AltCOM.ZB_WRITE_CFG(0x03, new byte[1] {
                3
            }));
            while (!AltGET.isMessage(rxPacket, mDef.ZB_WRITE_CFG_RSP))
            {
                ;
            }
            Console.WriteLine("Config Reset");
            AltCOM.genCom(wirelessIn, AltCOM.SYS_RESET());
            while (!AltGET.isMessage(rxPacket, mDef.SYS_RESET_IND))
            {
                ;
            }
            Console.WriteLine("Device Reset");
            AltCOM.genCom(wirelessIn, AltCOM.ZB_WRITE_CFG(0x83, new byte[2] {
                0x12, PANID
            }));
            while (!AltGET.isMessage(rxPacket, mDef.ZB_WRITE_CFG_RSP))
            {
                ;
            }
            Console.WriteLine("Set PANID of the device");
            AltCOM.genCom(wirelessIn, AltCOM.ZB_WRITE_CFG(0x84, new byte[4] {
                0, 0, 0, channel
            }));
            while (!AltGET.isMessage(rxPacket, mDef.ZB_WRITE_CFG_RSP))
            {
                ;
            }
            Console.WriteLine("Set the channel of the device to " + channel);
            AltCOM.genCom(wirelessIn, AltCOM.ZB_READ_CFG(0x84));
            while (!AltGET.isMessage(rxPacket, mDef.ZB_READ_CFG_RSP))
            {
                ;
            }
            Console.WriteLine("//////CHANGES CONFIRMED//////");

            /*////////////COPY PASTABLE DEBUG BLOCK FOR PRINTING THE SERIAL TERMINAL/////////
            *  while (true)
            *  {
            *   string hexOutput = String.Format("{0:X}", wirelessIn.ReadByte());
            *   Console.Write("{0} ", hexOutput);
            *  }
            *  ///////////////////////////////////////////////////////////////////////////////*/
            if (isCoordinator)
            {
                Console.WriteLine("Configuring Coordinator");
                byte[] cmd_in = new byte[2] {
                    0x01, 0x00
                };
                byte[] cmd_out = { 0x02, 0x00 };
                AltCOM.genCom(wirelessIn, AltCOM.ZB_APP_REGISTER(1, 1, 1, 0, 1, cmd_in, 1, cmd_out));
                AltCOM.genCom(wirelessIn, AltCOM.ZB_START_REQ());
                while (!AltGET.isMessage(rxPacket, mDef.ZB_START_REQUEST_RSP))
                {
                    ;
                }
                while (!AltGET.isMessage(rxPacket, mDef.ZB_START_CONFIRM))
                {
                    ;
                }
                Console.WriteLine("Configured Coordinator");
                isCoord = true;
            }
            else
            {
                Console.WriteLine("Configuring Router");

                int routerWait = 0;

                AltCOM.genCom(wirelessIn, AltCOM.ZB_WRITE_CFG(0x87, new byte[1] {
                    1
                }));
                while (!AltGET.isMessage(rxPacket, mDef.ZB_WRITE_CFG_RSP))
                {
                    ;
                }
                Console.WriteLine("Configured Device Type");

                AltCOM.genCom(wirelessIn, AltCOM.SYS_RESET());
                while (!AltGET.isMessage(rxPacket, mDef.SYS_RESET_IND))
                {
                    ;
                }

                AltCOM.genCom(wirelessIn, AltCOM.ZB_READ_CFG(0x87));
                while (!AltGET.isMessage(rxPacket, mDef.ZB_READ_CFG_RSP))
                {
                    ;
                }

                byte[] cmd_in = new byte[2] {
                    0x02, 0x00
                };
                byte[] cmd_out = { 0x01, 0x00 };
                AltCOM.genCom(wirelessIn, AltCOM.ZB_APP_REGISTER(1, 1, 0, 0, 1, cmd_in, 1, cmd_out));
                while (!AltGET.isMessage(rxPacket, mDef.ZB_REGISTER_RSP))
                {
                    ;
                }
                AltCOM.genCom(wirelessIn, AltCOM.ZB_START_REQ());
                while (!AltGET.isMessage(rxPacket, mDef.ZB_START_REQUEST_RSP))
                {
                    ;
                }
                while (!AltGET.isMessage(rxPacket, mDef.ZB_START_CONFIRM))
                {
                    if (routerWait > 20)
                    {
                        break;              //Stop waiting for a response if you waited longer than 5 seconds
                    }
                    routerWait++;
                    Thread.Sleep(250);
                }
                ;
                if (routerWait < 21)
                {
                    Console.WriteLine("Configured Router");
                    isCoord = false;
                }
                else
                {
                    initALT5801(true);
                }
            }
        }