/// <summary> /// Processes a byte received on the bus. /// </summary> /// <param name="data">Byte recevied on the bus</param> public bool ProcessData(byte data) { //Console.Write("{0,2:x} ", data); bool packetReady = false; switch (_state) { case BUSRECIEVESTATE.PACKET_S_START: if (data == START_BYTE) { //Console.WriteLine("Start"); _state = BUSRECIEVESTATE.PACKET_S_LEADIN; } else { //Console.WriteLine("Skip"); } break; case BUSRECIEVESTATE.PACKET_S_LEADIN: if (data == LEADIN_BYTE) { //Console.WriteLine("Leadin"); _state = BUSRECIEVESTATE.PACKET_S_ADDRESS; } else { //Console.WriteLine("Reset"); _state = BUSRECIEVESTATE.PACKET_S_START; } break; case BUSRECIEVESTATE.PACKET_S_ADDRESS: //Console.WriteLine("Destination"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket = new EosPacket(); _currentPacket.Destination = data; _state = BUSRECIEVESTATE.PACKET_S_SRC; break; case BUSRECIEVESTATE.PACKET_S_SRC: //Console.WriteLine("Source"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket.Source = data; _state = BUSRECIEVESTATE.PACKET_S_COMMAND; break; case BUSRECIEVESTATE.PACKET_S_COMMAND: //Console.WriteLine("Command"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket.Command = data; _state = BUSRECIEVESTATE.PACKET_S_DATALEN; break; case BUSRECIEVESTATE.PACKET_S_DATALEN: //Console.WriteLine("Data Length"); _parseTimer.Stop(); _parseTimer.Start(); _dataRemainig = data; if (_dataRemainig > 0) { _state = BUSRECIEVESTATE.PACKET_S_DATA; } else { _state = BUSRECIEVESTATE.PACKET_S_CHKSUM; } break; case BUSRECIEVESTATE.PACKET_S_DATA: //Console.WriteLine("Data"); _parseTimer.Stop(); _parseTimer.Start(); _currentPacket.Add(data); if (--_dataRemainig == 0) { _state = BUSRECIEVESTATE.PACKET_S_CHKSUM; } break; case BUSRECIEVESTATE.PACKET_S_CHKSUM: _parseTimer.Stop(); if (_parseTimer != null) { _parseTimer.Stop(); } if (data == _currentPacket.Checksum) { //Console.WriteLine("Good Packet"); OnPacketReceived(_currentPacket); } else { //Console.WriteLine("Bad Packet"); OnParesError(EosParserError.CorruptPacket); } Reset(); packetReady = true; break; } return packetReady; }
/// <summary> /// Resets the parser to start looking for the packet start. /// </summary> public void Reset() { _parseTimer.Stop(); _state = BUSRECIEVESTATE.PACKET_S_START; _currentPacket = null; }