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)) { ; } } }
//This function runs in a separate thread and handles all receiving and processisng of the received data public static void wirelessReceive() { Console.WriteLine("Receive Thread Started"); while (Program.runTranceiver) { rxPacket = AltGET.getPacket(wirelessIn); /*Console.WriteLine("Packet Received"); //Uncomment this to print all incoming transmissions to the console for debugging * for (int i=0;i<rxPacket.Length;i++) * { * Console.Write("{0} ", String.Format("{0:X}", rxPacket[i])); * } * Console.WriteLine(" ");*/ if (AltGET.isMessage(rxPacket, 0x4687)) { String newDat = Encoding.ASCII.GetString(AltGET.getRx(rxPacket)); //Store the packet locally String[] cotSet = newDat.Split(','); //Look up the devID in the index and see if it's already been entered int wasEntered = -1; for (int i = 0; i < devIndex; i++) { if (devLookup[i] == cotSet[0]) { wasEntered = i; } } if (wasEntered != -1) //If it's already registered, override the current data { receivedData[wasEntered] = newDat; devRefresh[wasEntered] = true; } else { //If not, register a new entry in the table and increase the index devIndex++; devLookup[devIndex] = cotSet[0]; receivedData[devIndex] = newDat; devRefresh[devIndex] = true; } } } }
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); } } }