Exemple #1
0
        unsafe public static Tuple <FrameHeader, ChannelCollection> LoadTestLabFrame(string path, FileReader reader, FileReadMode readMode)
        {
            TestLabFrameHeader frameHeader = new TestLabFrameHeader();
            ChannelCollection  channels    = new ChannelCollection();

            reader.Position = 8;

            frameHeader.Title     = ReadASCIIString(reader, 61);
            frameHeader.Character = ReadASCIIString(reader, 121);
            frameHeader.Region    = ReadASCIIString(reader, 121);

            try
            {
                frameHeader.Time = DateTime.Parse(ReadASCIIString(reader, 11) + " " + ReadASCIIString(reader, 9));
            }
            catch
            {
                throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
            }


            int numberOfChannels = reader.ReadUInt16();

            reader.Position += 17;

            if (numberOfChannels == 0)
            {
                if (reader.FileSize != 350)
                {
                    throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                }
            }
            else
            {
                long fullSize = 350 + 192 * numberOfChannels;
                if (reader.FileSize < fullSize)
                {
                    throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                }

                for (int i = 0; i != numberOfChannels; ++i)
                {
                    TestLabChannelHeader channelHeader = new TestLabChannelHeader();
                    channelHeader.Name        = ReadASCIIString(reader, 13);
                    channelHeader.Description = ReadASCIIString(reader, 121);
                    channelHeader.Unit        = ReadASCIIString(reader, 13);
                    channelHeader.Offset      = reader.ReadFloat32();
                    channelHeader.Scale       = reader.ReadFloat32();
                    channelHeader.Cutoff      = reader.ReadFloat32();
                    channelHeader.Sampling    = reader.ReadUInt16();
                    channelHeader.Type        = TestLabChannelHeader.Validation((TestLabChannelType)reader.ReadUInt8());
                    channelHeader.DataFormat  = TestLabChannelHeader.Validation((TestLabDataFormat)reader.ReadUInt8());
                    uint channelLength = reader.ReadUInt32();
                    if (channelLength > int.MaxValue)
                    {
                        throw new InvalidOperationException("Файл содержит канал слишеом большой длины.");
                    }
                    Channel channel = new Channel(channelHeader, new Algebra.RealVector((int)channelLength));
                    channels.Add(channel);
                    reader.Position += 25;

                    fullSize += channelLength * TestLabChannelHeader.GetItemSize(channelHeader.DataFormat) + 2;
                }

                if ((readMode & FileReadMode.DisableCheckExceedingFileSize) == 0)
                {
                    if (reader.FileSize != fullSize)
                    {
                        throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                    }
                }
                else
                {
                    if (reader.FileSize < fullSize)
                    {
                        throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                    }
                }

                for (int i = 0; i != numberOfChannels; ++i)
                {
                    Channel channel             = channels[i];
                    TestLabChannelHeader header = (TestLabChannelHeader)channel.Header;
                    int     length      = channel.Length;
                    byte[]  buffer      = reader.ReadBytes(length * TestLabChannelHeader.GetItemSize(header.DataFormat));
                    double  offset      = header.Offset;
                    double  scale       = header.Scale;
                    double *destination = (double *)channel.Vector.Pointer;

                    switch (header.DataFormat)
                    {
                    case TestLabDataFormat.UInt8:
                        fixed(void *pointer = buffer)
                        {
                            byte *source = (byte *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.UInt16:
                        fixed(void *pointer = buffer)
                        {
                            ushort *source = (ushort *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.UInt32:
                        fixed(void *pointer = buffer)
                        {
                            uint *source = (uint *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.Int8:
                        fixed(void *pointer = buffer)
                        {
                            sbyte *source = (sbyte *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.Int16:
                        fixed(void *pointer = buffer)
                        {
                            short *source = (short *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.Int32:
                        fixed(void *pointer = buffer)
                        {
                            int *source = (int *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.Float32:
                        fixed(void *pointer = buffer)
                        {
                            float *source = (float *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    case TestLabDataFormat.Float64:
                        fixed(void *pointer = buffer)
                        {
                            double *source = (double *)pointer;

                            for (int j = 0; j != length; ++j)
                            {
                                destination[j] = scale * (source[j] - offset);
                            }
                        }

                        break;

                    default:
                        throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                    }

                    if (reader.ReadUInt16() != 65535)
                    {
                        throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                    }
                }
            }

            return(new Tuple <FrameHeader, ChannelCollection>(frameHeader, channels));
        }
Exemple #2
0
        public static Tuple <FrameHeader, ChannelCollection> LoadCatmanFrame(string path, FileReader reader, FileReadMode readMode)
        {
            try
            {
                reader.Position = 2;
                CatmanFrameHeader frameHeader = new CatmanFrameHeader();
                ChannelCollection channels    = new ChannelCollection();

                int dataOffset = reader.ReadInt32();
                frameHeader.Comment = ReadASCIIString(reader, reader.ReadInt16());
                for (int i = 0; i != 32; ++i)
                {
                    frameHeader.Reserve[i] = ReadASCIIString(reader, reader.ReadInt16());
                }

                int   numberOfChannels    = reader.ReadInt16();
                int[] offsetChannelLength = new int[numberOfChannels];

                frameHeader.MaximumChannelLength = reader.ReadInt32();
                for (int i = 0; i != numberOfChannels; ++i)
                {
                    offsetChannelLength[i] = reader.ReadInt32();
                }

                //  Проверка компресии данных.
                if (reader.ReadInt32() != 0)
                {
                    throw new NotSupportedException("Произошла попытка чтения файла с компресией данных.");
                }

                if (numberOfChannels > 0)
                {
                    for (int i = 0; i != numberOfChannels; ++i)
                    {
                        int channelLength = 0;
                        reader.Position = offsetChannelLength[i] - 2;
                        CatmanChannelHeader channelHeader = new CatmanChannelHeader();


                        channelHeader.LocationInDatabase = reader.ReadInt16();
                        channelLength                      = reader.ReadInt32();
                        channelHeader.Name                 = ReadASCIIString(reader, reader.ReadInt16());
                        channelHeader.Unit                 = ReadASCIIString(reader, reader.ReadInt16());
                        channelHeader.Comment              = ReadASCIIString(reader, reader.ReadInt16());
                        channelHeader.DataFormat           = (CatmanDataFormat)reader.ReadInt16();
                        channelHeader.DataWidth            = reader.ReadInt16();
                        channelHeader.Time                 = DateTime.FromOADate(reader.ReadFloat64());
                        channelHeader.SizeOfExtendedHeader = reader.ReadInt32();

                        if (channelHeader.SizeOfExtendedHeader < 148)
                        {
                            throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                        }

                        channelHeader.StartTime                   = DateTime.FromOADate(reader.ReadFloat64());
                        channelHeader.SamplingTimeStep            = reader.ReadFloat64();
                        channelHeader.Sampling                    = 1000 / channelHeader.SamplingTimeStep;
                        channelHeader.CodeOfSensorType            = (CatmanCodeOfSensorType)reader.ReadInt16();
                        channelHeader.CodeOfSupplyVoltage         = (CatmanCodeOfSupplyVoltage)reader.ReadInt16();
                        channelHeader.CodeOfFilterCharacteristics = (CatmanCodeOfFilterCharacteristics)reader.ReadInt16();
                        channelHeader.CodeOfFilterFrequency       = (CatmanCodeOfFilterFrequency)reader.ReadInt16();
                        channelHeader.TareValue                   = reader.ReadFloat32();
                        channelHeader.ZeroValue                   = reader.ReadFloat32();
                        channelHeader.CodeOfMeasuringRange        = (CatmanCodeOfMeasuringRange)reader.ReadFloat32();
                        channelHeader.InputCharacteristics        = new double[4];
                        channelHeader.InputCharacteristics[0]     = reader.ReadFloat32();
                        channelHeader.InputCharacteristics[1]     = reader.ReadFloat32();
                        channelHeader.InputCharacteristics[2]     = reader.ReadFloat32();
                        channelHeader.InputCharacteristics[3]     = reader.ReadFloat32();
                        channelHeader.AmplifierSerialNumber       = ReadASCIIString(reader, 32);
                        channelHeader.PhysicalUnit                = ReadASCIIString(reader, 8);
                        channelHeader.NativeUnit                  = ReadASCIIString(reader, 8);
                        channelHeader.HardwareSlotNumber          = reader.ReadInt16();
                        channelHeader.HardwareSubSlotNumber       = reader.ReadInt16();
                        channelHeader.CodeOfAmplifierType         = reader.ReadInt16();
                        channelHeader.CodeOfAPConnectorType       = reader.ReadInt16();
                        channelHeader.GageFactor                  = reader.ReadFloat32();
                        channelHeader.BridgeFactor                = reader.ReadFloat32();
                        channelHeader.CodeOfMeasurementSignal     = reader.ReadInt16();
                        channelHeader.CodeOfAmplifierInput        = reader.ReadInt16();
                        channelHeader.CodeOfHighpassFilter        = reader.ReadInt16();
                        channelHeader.OnlineImportInfo            = reader.ReadUInt8();
                        channelHeader.CodeOfScaleType             = reader.ReadUInt8();
                        channelHeader.SoftwareZeroValue           = reader.ReadFloat32();
                        channelHeader.WriteProtected              = reader.ReadUInt8();
                        channelHeader.Alignment                   = new byte[3];
                        channelHeader.Alignment[0]                = reader.ReadUInt8();
                        channelHeader.Alignment[1]                = reader.ReadUInt8();
                        channelHeader.Alignment[2]                = reader.ReadUInt8();
                        channelHeader.NominalRange                = reader.ReadFloat32();
                        channelHeader.CableLengthCompensation     = reader.ReadFloat32();
                        channelHeader.ExportFormat                = reader.ReadUInt8();
                        channelHeader.ChannelType                 = reader.ReadInt8();
                        channelHeader.EDaqConnectorOnLayer        = reader.ReadUInt8();
                        channelHeader.EDaqLayer                   = reader.ReadUInt8();
                        channelHeader.ContentType                 = reader.ReadUInt8();
                        channelHeader.Reserved                    = new byte[3];
                        channelHeader.Reserved[0]                 = reader.ReadUInt8();
                        channelHeader.Reserved[1]                 = reader.ReadUInt8();
                        channelHeader.Reserved[2]                 = reader.ReadUInt8();

                        channelHeader.LinearisationMode        = reader.ReadUInt8();
                        channelHeader.UserScaleType            = reader.ReadUInt8();
                        channelHeader.NumberOfPointsScaleTable = reader.ReadUInt8();
                        if (channelHeader.NumberOfPointsScaleTable != 0)
                        {
                            channelHeader.PointsScaleTable = new double[channelHeader.NumberOfPointsScaleTable];
                            for (int j = 0; j != channelHeader.NumberOfPointsScaleTable; ++j)
                            {
                                channelHeader.PointsScaleTable[j] = reader.ReadFloat64();
                            }
                        }

                        channelHeader.ThermoType = reader.ReadInt16();
                        channelHeader.Formula    = ReadASCIIString(reader, reader.ReadInt16());
                        channelHeader.SensorInfo = new CatmanSensorInfo();
                        int sensorInfoSize = reader.ReadInt32();
                        if (sensorInfoSize != 68)
                        {
                            throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                        }
                        channelHeader.SensorInfo.InUse       = reader.ReadInt16() != 0;
                        channelHeader.SensorInfo.Description = ReadASCIIString(reader, 50);
                        channelHeader.SensorInfo.Tid         = ReadASCIIString(reader, 16);

                        RealVector vector = new RealVector(channelLength);
                        channels.Add(new Channel(channelHeader, vector));
                    }

                    reader.Position = dataOffset;
                    foreach (Channel channel in channels)
                    {
                        CatmanChannelHeader channelHeader = (CatmanChannelHeader)channel.Header;
                        switch (channelHeader.ExportFormat)
                        {
                        case 0:
                            for (int i = 0; i != channel.Length; ++i)
                            {
                                channel[i] = reader.ReadFloat64();
                            }
                            break;

                        case 1:
                            for (int i = 0; i != channel.Length; ++i)
                            {
                                channel[i] = reader.ReadFloat32();
                            }
                            break;

                        case 2:
                            channelHeader.MinValueFactor = reader.ReadFloat64();
                            channelHeader.MinValueFactor = reader.ReadFloat64();
                            for (int i = 0; i != channel.Length; ++i)
                            {
                                channel[i] = reader.ReadInt16() / 32767 * (channelHeader.MinValueFactor - channelHeader.MinValueFactor) + channelHeader.MinValueFactor;
                            }
                            break;

                        default:
                            throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                        }
                    }

                    //  Проверка Id Post data ares
                    if (reader.ReadInt16() != 4001)
                    {
                        throw new InvalidDataException("Произошла попытка загрузить файл некорректного формата.");
                    }

                    foreach (Channel channel in channels)
                    {
                        ((CatmanChannelHeader)channel.Header).FormatString = ReadASCIIString(reader, reader.ReadInt16());
                    }
                }
                return(new Tuple <FrameHeader, ChannelCollection>(frameHeader, channels));
            }
            catch (Exception ex)
            {
                throw new InvalidDataException("Произошла ошибка при чтении файла.", ex);
            }
        }