/// <summary> /// Read from the serial line if a useful chunk is present. /// </summary> /// <remarks> /// In this case a "useful chunk" means that either the payload size /// and endpoint of a new message or the complete payload of a message /// are present. /// </remarks> /// <returns> /// True if there was enough data to read, otherwise false. /// </returns> private bool ReadAndProcessBytes() { switch (_WaitingState) { case WaitingStates.NewMessage: if (_byteStream.Count >= 4) { // Read new payload size and endpoint _CurrentPayloadSize = Util.GetUInt16(ReadBytes(2)); _CurrentEndpoint = Util.GetUInt16(ReadBytes(2)); _WaitingState = WaitingStates.Payload; return(true); } break; case WaitingStates.Payload: if (_byteStream.Count >= _CurrentPayloadSize) { // All of the payload's been received, so read it. var buffer = ReadBytes(_CurrentPayloadSize); RawMessageReceived(this, new RawMessageReceivedEventArgs(_CurrentEndpoint, buffer)); // Reset state _WaitingState = WaitingStates.NewMessage; _CurrentEndpoint = 0; _CurrentPayloadSize = 0; return(true); } break; } // If this hasn't returned yet there wasn't anything interesting to read. return(false); }
/// <summary> Read from the serial line if a useful chunk is present. /// </summary> /// <remarks> /// In this case a "useful chunk" means that either the payload size /// and endpoint of a new message or the complete payload of a message /// are present. /// </remarks> /// <returns> /// True if there was enough data to read, otherwise false. /// </returns> private bool readAndProcess() { var endpoint = new byte[2]; var payloadSize = new byte[2]; switch (_WaitingState) { case WaitingStates.NewMessage: if (_SerialPort.BytesToRead >= 4) { // Read new payload size and endpoint _SerialPort.Read(payloadSize, 0, 2); _SerialPort.Read(endpoint, 0, 2); if (BitConverter.IsLittleEndian) { // Data is transmitted big-endian, so flip. Array.Reverse(payloadSize); Array.Reverse(endpoint); } _CurrentPayloadSize = BitConverter.ToUInt16(payloadSize, 0); _CurrentEndpoint = BitConverter.ToUInt16(endpoint, 0); //Debug.WriteLine( "Message metadata received:" ); //Debug.WriteLine( "\tPLS: " + _CurrentPayloadSize.ToString() ); //Debug.WriteLine( "\tEP: " + _CurrentEndpoint.ToString() ); _WaitingState = WaitingStates.Payload; return(true); } break; case WaitingStates.Payload: if (_SerialPort.BytesToRead >= _CurrentPayloadSize) { // All of the payload's been received, so read it. var buffer = new byte[_CurrentPayloadSize]; _SerialPort.Read(buffer, 0, _CurrentPayloadSize); RawMessageReceived(this, new RawMessageReceivedEventArgs(_CurrentEndpoint, buffer)); // Reset state _WaitingState = WaitingStates.NewMessage; _CurrentEndpoint = 0; _CurrentPayloadSize = 0; return(true); } break; } // If this hasn't returned yet there wasn't anything interesting to read. return(false); }
/// <summary> /// Read from the serial line if a useful chunk is present. /// </summary> /// <remarks> /// In this case a "useful chunk" means that either the payload size /// and endpoint of a new message or the complete payload of a message /// are present. /// </remarks> /// <returns> /// True if there was enough data to read, otherwise false. /// </returns> private bool ReadAndProcessBytes() { switch (_WaitingState) { case WaitingStates.NewMessage: if (_byteStream.Count >= 4) { // Read new payload size and endpoint _CurrentPayloadSize = Util.GetUInt16(ReadBytes(2)); _CurrentEndpoint = Util.GetUInt16(ReadBytes(2)); _WaitingState = WaitingStates.Payload; return(true); } break; case WaitingStates.Payload: if (_byteStream.Count >= _CurrentPayloadSize) { // All of the payload's been received, so read it. var buffer = ReadBytes(_CurrentPayloadSize); //byte[] fullPacket = Util.CombineArrays(Util.GetBytes(_CurrentPayloadSize),Util.GetBytes(_CurrentEndpoint),buffer); //System.Console.WriteLine("RECV:" + ((Endpoint)_CurrentEndpoint)); //LogPacket("RECV", fullPacket); //Console.WriteLine("RECV:" + ((Endpoint)_CurrentEndpoint).ToString()+"["+buffer.Length+"]"); RawMessageReceived(this, new RawMessageReceivedEventArgs(_CurrentEndpoint, buffer)); // Reset state _WaitingState = WaitingStates.NewMessage; _CurrentEndpoint = 0; _CurrentPayloadSize = 0; return(true); } break; } // If this hasn't returned yet there wasn't anything interesting to read. return(false); }
/// <summary> /// Read from the serial line if a useful chunk is present. /// </summary> /// <remarks> /// In this case a "useful chunk" means that either the payload size /// and endpoint of a new message or the complete payload of a message /// are present. /// </remarks> /// <returns> /// True if there was enough data to read, otherwise false. /// </returns> private bool ReadAndProcessBytes() { switch ( _WaitingState ) { case WaitingStates.NewMessage: if ( _byteStream.Count >= 4 ) { // Read new payload size and endpoint _CurrentPayloadSize = Util.GetUInt16(ReadBytes(2)); _CurrentEndpoint = Util.GetUInt16(ReadBytes(2)); //Debug.WriteLine( "Message metadata received:" ); //Debug.WriteLine( "\tPLS: " + _CurrentPayloadSize.ToString() ); //Debug.WriteLine( "\tEP: " + _CurrentEndpoint.ToString() ); _WaitingState = WaitingStates.Payload; return true; } break; case WaitingStates.Payload: if ( _byteStream.Count >= _CurrentPayloadSize ) { // All of the payload's been received, so read it. var buffer = ReadBytes(_CurrentPayloadSize); RawMessageReceived( this, new RawMessageReceivedEventArgs( _CurrentEndpoint, buffer ) ); // Reset state _WaitingState = WaitingStates.NewMessage; _CurrentEndpoint = 0; _CurrentPayloadSize = 0; return true; } break; } // If this hasn't returned yet there wasn't anything interesting to read. return false; }
/// <summary> Read from the serial line if a useful chunk is present. /// </summary> /// <remarks> /// In this case a "useful chunk" means that either the payload size /// and endpoint of a new message or the complete payload of a message /// are present. /// </remarks> /// <returns> /// True if there was enough data to read, otherwise false. /// </returns> private bool readAndProcess() { var endpoint = new byte[2]; var payloadSize = new byte[2]; switch ( _WaitingState ) { case WaitingStates.NewMessage: if ( _SerialPort.BytesToRead >= 4 ) { // Read new payload size and endpoint _SerialPort.Read( payloadSize, 0, 2 ); _SerialPort.Read( endpoint, 0, 2 ); if ( BitConverter.IsLittleEndian ) { // Data is transmitted big-endian, so flip. Array.Reverse( payloadSize ); Array.Reverse( endpoint ); } _CurrentPayloadSize = BitConverter.ToUInt16( payloadSize, 0 ); _CurrentEndpoint = BitConverter.ToUInt16( endpoint, 0 ); //Debug.WriteLine( "Message metadata received:" ); //Debug.WriteLine( "\tPLS: " + _CurrentPayloadSize.ToString() ); //Debug.WriteLine( "\tEP: " + _CurrentEndpoint.ToString() ); _WaitingState = WaitingStates.Payload; return true; } break; case WaitingStates.Payload: if ( _SerialPort.BytesToRead >= _CurrentPayloadSize ) { // All of the payload's been received, so read it. var buffer = new byte[_CurrentPayloadSize]; _SerialPort.Read( buffer, 0, _CurrentPayloadSize ); RawMessageReceived( this, new RawMessageReceivedEventArgs( _CurrentEndpoint, buffer ) ); // Reset state _WaitingState = WaitingStates.NewMessage; _CurrentEndpoint = 0; _CurrentPayloadSize = 0; return true; } break; } // If this hasn't returned yet there wasn't anything interesting to read. return false; }