internal static ChannelGroupBlock Read(Mdf mdf, Stream stream, uint position) { stream.Position = position; var block = new ChannelGroupBlock(mdf); block.Read(stream); block.stream = stream; var data = new byte[block.Size - 4]; var read = stream.Read(data, 0, data.Length); if (read != data.Length) { throw new FormatException(); } block.next = null; block.Channels = new ChannelCollection(mdf); block.Comment = null; block.SampleReductions = null; block.ptrNextChannelGroup = BitConverter.ToUInt32(data, 0); block.ptrFirstChannelBlock = BitConverter.ToUInt32(data, 4); block.ptrTextBlock = BitConverter.ToUInt32(data, 8); block.RecordID = BitConverter.ToUInt16(data, 12); block.NumChannels = BitConverter.ToUInt16(data, 14); block.RecordSize = BitConverter.ToUInt16(data, 16); block.NumRecords = BitConverter.ToUInt32(data, 18); if (data.Length >= 26) { block.ptrFirstSampleReductionBlock = BitConverter.ToUInt32(data, 22); } if (block.ptrTextBlock != 0) { stream.Position = block.ptrTextBlock; block.Comment = TextBlock.Read(mdf, stream); } if (block.ptrFirstChannelBlock != 0) { block.Channels.Read(ChannelBlock.Read(mdf, stream, block.ptrFirstChannelBlock)); } //if (m_ptrFirstSampleReductionBlock != 0) //{ // mdf.Data.Position = m_ptrFirstSampleReductionBlock; // SampleReductions = new SampleReductionCollection(mdf, new SampleReductionBlock(mdf)); //} return(block); }
public object GetValue(ChannelBlock channel) { if (ChannelGroup.Channels.Contains(channel) == false) { throw new ArgumentException("channel"); } var byteOffset = channel.BitOffset / 8; var value = (object)null; // TODO: BigEndian byte order not supported yet. switch (channel.SignalType) { case SignalType.UInt: { var result = 0u; for (int i = 0; i < channel.NumberOfBits; i++) { result |= (uint)((GetBit(Data, channel.BitOffset + i) ? 1 : 0) << i); } value = result; break; } case SignalType.Int: { var result = 0; for (int i = 0; i < channel.NumberOfBits; i++) { result |= (GetBit(Data, channel.BitOffset + i) ? 1 : 0) << i; } var maxValue = Math.Pow(2, channel.NumberOfBits) / 2; if (result > maxValue) { result -= (int)(maxValue * 2); } value = result; break; } case SignalType.IEEE754Float: if (channel.NumberOfBits == 32) { value = BitConverter.ToSingle(Data, byteOffset); } if (channel.NumberOfBits == 64) { value = (float)BitConverter.ToDouble(Data, byteOffset); } break; case SignalType.IEEE754Double: if (channel.NumberOfBits == 32) { value = (double)BitConverter.ToSingle(Data, byteOffset); } if (channel.NumberOfBits == 64) { value = BitConverter.ToDouble(Data, byteOffset); } break; case SignalType.String: value = ChannelGroup.Mdf.IDBlock.Encoding.GetString(Data, byteOffset, channel.NumberOfBits / 8); break; } if (channel.ChannelConversion != null && value != null) { return(channel.ChannelConversion.AdditionalConversionData.GetPhyValue(Convert.ToDouble(value))); } return(value); }