public MaxDataLinkPacket(MaxServiceType serviceType, MaxServiceResponseFormat serviceResponseFormat) { _serviceType = serviceType; _serviceResponseFormat = serviceResponseFormat; _dataSize = 0; _dataOffset = 0; //_processState = ProcessState.Idle; _packetState = PACKET_STATE.STX; _dataLength = new byte[2]; _recievedData = new Queue <byte>(); _packetData = new Queue <byte>(); }
public void ProcessPacket() { if (_packetState == PACKET_STATE.STX) // (_processState == ProcessState.Idle) { _packetData.Clear(); byte first = _recievedData.Dequeue(); _packetData.Enqueue(first); if (first == 0x02) { PacketCompleted = false; //_processState = ProcessState.InProcess; _packetState = PACKET_STATE.STYPE; } } while ((_recievedData.Count > 0) && (_packetState != PACKET_STATE.STX)) // (_processState == ProcessState.InProcess)) { byte data = _recievedData.Dequeue(); switch (_packetState) { //case PACKET_STATE.STX: // _packetState = PACKET_STATE.STYPE; // break; case PACKET_STATE.STYPE: //if ((byte)_serviceType != data) // Throw exception!! _packetState = PACKET_STATE.ACK; break; case PACKET_STATE.ACK: _packetState = PACKET_STATE.STAT; break; case PACKET_STATE.STAT: _packetState = (_serviceResponseFormat == MaxServiceResponseFormat.ResponseWithData) ? PACKET_STATE.LEN : PACKET_STATE.CRCH; break; case PACKET_STATE.LEN: _dataSize = data; _dataOffset = 0; _packetState = PACKET_STATE.DATA; break; case PACKET_STATE.DATA: if (_dataOffset < _dataSize) { _dataOffset++; } if (_dataOffset == _dataSize) { _dataOffset = 0; _packetState = PACKET_STATE.CRCH; } break; case PACKET_STATE.CRCH: _packetState = PACKET_STATE.CRCL; break; case PACKET_STATE.CRCL: PacketCompleted = true; //_processState = ProcessState.Idle; _packetState = PACKET_STATE.STX; break; } _packetData.Enqueue(data); } }
public void ProcessPacket() { if (_processState == ProcessState.Idle) { _packetData.Clear(); byte first = _recievedData.Dequeue(); _packetData.Enqueue(first); if (first == 0x06) // ACK Response { Type = PacketType.Ack; PacketCompleted = true; } else if (first == 0xEE) { Type = PacketType.Response; PacketCompleted = false; _processState = ProcessState.InProcess; _packetState = PACKET_STATE.STP; } } while ((_recievedData.Count > 0) && (_processState == ProcessState.InProcess)) { byte data = _recievedData.Dequeue(); switch (_packetState) { case PACKET_STATE.STP: _packetState = PACKET_STATE.IDENTITY; break; case PACKET_STATE.IDENTITY: _packetState = PACKET_STATE.CTRL; break; case PACKET_STATE.CTRL: _packetState = PACKET_STATE.SEQ; break; case PACKET_STATE.SEQ: _dataLength[0] = data; _packetState = PACKET_STATE.LENL; break; case PACKET_STATE.LENL: _dataLength[1] = data; if (BitConverter.IsLittleEndian) { Array.Reverse(_dataLength); } _dataSize = BitConverter.ToInt16(_dataLength, 0); _packetState = PACKET_STATE.LENH; break; case PACKET_STATE.LENH: if (_dataOffset < _dataSize) { _dataOffset++; } if (_dataOffset == _dataSize) { _dataOffset = 0; _packetState = PACKET_STATE.DATA; } break; case PACKET_STATE.DATA: _packetState = PACKET_STATE.CRCL; break; case PACKET_STATE.CRCL: _packetState = PACKET_STATE.CRCH; PacketCompleted = true; _processState = ProcessState.Idle; break; } _packetData.Enqueue(data); } }