Пример #1
0
 public void TestDigitalByteCount()
 {
     Assert.That(DataFileHandler.GetDigitalByteCount(7), Is.EqualTo(2));
     Assert.That(DataFileHandler.GetDigitalByteCount(16), Is.EqualTo(2));
     Assert.That(DataFileHandler.GetDigitalByteCount(17), Is.EqualTo(4));
     Assert.That(DataFileHandler.GetDigitalByteCount(32), Is.EqualTo(4));
 }
Пример #2
0
 public void TestByteCount()
 {
     Assert.That(DataFileHandler.GetByteCount(1, 1, DataFileType.Binary), Is.EqualTo(12));
     Assert.That(DataFileHandler.GetByteCount(5, 17, DataFileType.Binary), Is.EqualTo(22));
     Assert.That(DataFileHandler.GetByteCount(5, 17, DataFileType.Float32), Is.EqualTo(32));
     Assert.That(DataFileHandler.GetByteCount(5, 17, DataFileType.Binary32), Is.EqualTo(32));
 }
Пример #3
0
        public byte[] ToByteDAT(DataFileType dataFileType, IReadOnlyList <AnalogChannelInformation> analogInformations)
        {
            var result = new byte[DataFileHandler.GetByteCount(this.analogs.Length, this.digitals.Length, dataFileType)];
            int analogOneChannelLength = dataFileType == DataFileType.Binary ? 2 : 4;
            int digitalByteStart       = 8 + analogOneChannelLength * this.analogs.Length;

            System.BitConverter.GetBytes(this.number).CopyTo(result, 0);
            System.BitConverter.GetBytes(this.timestamp).CopyTo(result, 4);

            switch (dataFileType)
            {
            case DataFileType.Binary:
                this.AnalogsToBinaryDAT(result, analogInformations);
                break;

            case DataFileType.Binary32:
                this.AnalogsToBinary32DAT(result, analogInformations);
                break;

            case DataFileType.Float32:
                this.AnalogsToFloat32DAT(result);
                break;

            default:
                throw new InvalidOperationException("Not supported file type DAT");
            }

            this.DigitalsToDAT(result, digitalByteStart);
            return(result);
        }
        internal static int GetByteCount(int analogsChannelsCount, int digitalChannelsCount, DataFileType dataFileType)
        {
            int analogOneChannelLength = dataFileType == DataFileType.Binary ? 2 : 4;

            return(DataFileHandler.sampleNumberLength +
                   DataFileHandler.timeStampLength +
                   analogsChannelsCount * analogOneChannelLength +
                   DataFileHandler.GetDigitalByteCount(digitalChannelsCount));
        }
Пример #5
0
        public DataFileSample(byte[] bytes, DataFileType dataFileType, int analogCount, int digitalCount)
        {
            this.analogs  = new double[analogCount];
            this.digitals = new bool[digitalCount];

            this.number    = System.BitConverter.ToInt32(bytes, 0);
            this.timestamp = System.BitConverter.ToInt32(bytes, 4);

            int digitalByteStart;

            if (dataFileType == DataFileType.Binary)
            {
                for (int i = 0; i < analogCount; i++)
                {
                    this.analogs[i] = System.BitConverter.ToInt16(bytes, 8 + i * 2);
                }
                digitalByteStart = 8 + 2 * analogCount;
            }
            else
            {
                if (dataFileType == DataFileType.Binary32)
                {
                    for (int i = 0; i < analogCount; i++)              //TODO добавить тест
                    {
                        this.analogs[i] = System.BitConverter.ToInt32(bytes, 8 + i * 4);
                    }
                }
                else if (dataFileType == DataFileType.Float32)
                {
                    for (int i = 0; i < analogCount; i++)              //TODO добавить тест
                    {
                        this.analogs[i] = System.BitConverter.ToSingle(bytes, 8 + i * 4);
                    }
                }

                digitalByteStart = 8 + 4 * analogCount;
            }

            int digitalByteCount = DataFileHandler.GetDigitalByteCount(digitalCount);

            for (int i = 0; i < digitalCount; i++)
            {
                int digitalByteIterator = i / 8;
                this.digitals[i] = Convert.ToBoolean((bytes[digitalByteStart + digitalByteIterator] >> (i - digitalByteIterator * 8)) & 1);
            }
        }
Пример #6
0
        internal void OpenFile(string fullPathToFile)
        {
            string path = System.IO.Path.GetDirectoryName(fullPathToFile);
            string filenameWithoutExtention = System.IO.Path.GetFileNameWithoutExtension(fullPathToFile);
            string extention = System.IO.Path.GetExtension(fullPathToFile).ToLower();

            if (extention == GlobalSettings.extentionCFF)
            {
                //TODO доделать cff
                throw new NotImplementedException("*.cff not supported");
            }
            else if (extention == GlobalSettings.extentionCFG || extention == GlobalSettings.extentionDAT)
            {
                this.Configuration = new ConfigurationHandler(System.IO.Path.Combine(path, filenameWithoutExtention + ".cfg"));
                this.Data          = new DataFileHandler(System.IO.Path.Combine(path, filenameWithoutExtention + ".dat"), this.Configuration);
            }
            else
            {
                throw new InvalidOperationException("Unsupported file extentions. Must be *.cfg, *.dat, *.cff");
            }
        }
        internal DataFileHandler(string fullPathToFileDAT, ConfigurationHandler configuration)
        {
            int samplesCount = configuration.sampleRates[configuration.sampleRates.Count - 1].lastSampleNumber;

            this.samples = new DataFileSample[samplesCount];

            if (configuration.dataFileType == DataFileType.Binary ||
                configuration.dataFileType == DataFileType.Binary32 ||
                configuration.dataFileType == DataFileType.Float32)
            {
                var fileContent = System.IO.File.ReadAllBytes(fullPathToFileDAT);

                int oneSampleLength = DataFileHandler.GetByteCount(configuration.analogChannelsCount,
                                                                   configuration.digitalChannelsCount,
                                                                   configuration.dataFileType);

                for (int i = 0; i < samplesCount; i++)
                {
                    var bytes = new byte[oneSampleLength];
                    for (int j = 0; j < oneSampleLength; j++)
                    {
                        bytes[j] = fileContent[i * oneSampleLength + j];
                    }
                    this.samples[i] = new DataFileSample(bytes, configuration.dataFileType,
                                                         configuration.analogChannelsCount, configuration.digitalChannelsCount);
                }
            }
            else if (configuration.dataFileType == DataFileType.ASCII)
            {
                var strings = System.IO.File.ReadAllLines(fullPathToFileDAT);
                strings = strings.Where(x => x != string.Empty).ToArray();              //removing empty strings (when *.dat file not following Standart)
                for (int i = 0; i < samplesCount; i++)
                {
                    this.samples[i] = new DataFileSample(strings[i],
                                                         configuration.analogChannelsCount, configuration.digitalChannelsCount);
                }
            }
        }