public MuComFrame(byte[] inputBuffer) { if (inputBuffer is null) { throw new ArgumentNullException(nameof(inputBuffer)); } if (inputBuffer.Length < 2) { throw new ArgumentException("Input buffer must contain at least two elements"); } //Copy buffer this.RawBuffer = new byte[inputBuffer.Length]; inputBuffer.CopyTo(this.RawBuffer, 0); //Decode header this.Description = MuComFrame.GetFrameDescriptionFromHeader(this.RawBuffer[0]); this.ID = MuComFrame.GetIdFromFrame(this.RawBuffer); this.DataCount = MuComFrame.GetDataByteCountFromHeader(this.RawBuffer[0]); //A read request frame is only two bytes long and does not if (this.Description == MuComFrameDesc.ReadRequest) { this.DataBytes = new byte[0]; return; } //Check input frame size int expectedLength = MuComFrame.GetFrameSizeFromDataCount(this.Description, this.DataCount); if (inputBuffer.Length < expectedLength) { throw new ArgumentException("Input buffer size too small! " + expectedLength.ToString() + " expected but input buffer contains only " + inputBuffer.Length.ToString() + " bytes."); } //Create buffer for data bytes and intermediate buffer byte[] buffer = new byte[this.DataCount + 1]; this.DataBytes = new byte[this.DataCount]; //Reconstruct data from frame //Buffer[0..8] = Data bytes (first byte will be index) int dataPos = 0; int bytePos = 1; for (int i = 0; i < (this.DataBytes.Length + 1); i++) { if (bytePos < 0) { bytePos = 6; dataPos++; } buffer[i] = (byte)(this.RawBuffer[dataPos] << (7 - bytePos)); dataPos++; buffer[i] |= (byte)(this.RawBuffer[dataPos] >> bytePos); bytePos--; } //Copy data this.ID = buffer[0]; Array.Copy(buffer, 1, this.DataBytes, 0, this.DataBytes.Length); }