/// <summary> /// State Handler for the state machine /// </summary> //Arun Geo Thomas public void stateHandler() { ReadData(); switch (currentStateR) { case States.checkStartByte: if (currentByte != Packet.START_BYTE) { nextState = States.checkStartByte; } else { nextState = States.getMsgSize; } break; case States.getMsgSize: msgSize = currentByte; nextState = States.checkMessageType; if ((msgSize >= maxMsgSize) | msgSize <= 0) { if (currentByte == Packet.START_BYTE) { nextState = States.getMsgSize; } else { nextState = States.checkStartByte; } } break; case States.checkMessageType: if ((currentByte == Packet.T_MODE) || (currentByte == Packet.T_CONTROL) || (currentByte == Packet.T_DATA) || (currentByte == Packet.T_EXIT) || (currentByte == Packet.T_adMSG) || (currentByte == Packet.T_HEARTBEAT) || (currentByte == Packet.T_FLASHMEM)) { msgType = currentByte; nextState = States.setupMsg; } else { if (msgSize == Packet.START_BYTE) { msgSize = currentByte; nextState = States.checkMessageType; } else if (currentByte == Packet.START_BYTE) { nextState = States.getMsgSize; } else { nextState = States.checkStartByte; } } break; case States.setupMsg: counter = 0; storeValues(); break; case States.getMsg: storeValues(); break; case States.checkCRC0: counter = 0; pkt_R = Packet.Create_Packet(msgType, (byte)(msgSize - 1), ptr); if (pkt_R.CRC[0] == (byte)currentByte) { nextState = States.checkCRC1; } else { pkt_R.CRC[0] = currentByte; SearchforStartByte(0); } break; case States.checkCRC1: if (pkt_R.CRC[1] == currentByte) { nextState = States.checkStartByte; callBackObject.ProcessPacket(pkt_R); } else { pkt_R.CRC[1] = currentByte; SearchforStartByte(1); } break; } currentStateR = nextState; }