예제 #1
0
        const int ADCmax = 0xB96400;//0xC35000;//

        public MainPresenter(ISerial serial, IMainView view, IFileWorker fileWorker)
        {
            Serial     = serial;
            View       = view;
            FileWorker = fileWorker;

            Requests = new Requests(Serial);
            Record   = new Record();

            var Ch1Packs = new List <double>();
            var Ch2Packs = new List <double>();

            Requests.MeasureDataHandler += data =>
            {
                Ch1Packs.Add(((data[0] - ADCmax / 2) * View.K_Ch1) / ADCmax);
                Ch2Packs.Add(((data[1] - ADCmax / 2) * View.K_Ch2) / ADCmax);
            };

            #region Serial Port
            int counter = 0;
            var timer   = new Timer();
            timer.Interval = (int)(TimeStep * 1000);
            timer.Tick    += (sender, args) =>
            {
                Ch1Packs.Clear();
                Ch2Packs.Clear();
                int d = (int)(TimeStep * Fd);
                for (int i = 0; i < d; i++)
                {
                    counter++;
                    Ch1Packs.Add(Math.Sin(counter * 4 * Math.PI / Record.Ch1.Length));
                    Ch2Packs.Add(Math.Cos(counter * 4 * Math.PI / Record.Ch1.Length));
                }
                for (int i = 0; i < Record.Ch1.Length - d; i++)
                {
                    Record.Ch1[i] = Record.Ch1[i + d];
                    Record.Ch2[i] = Record.Ch2[i + d];
                }
                for (int i = 0; i < d; i++)
                {
                    Record.Ch1[Record.Ch1.Length - d + i] = Ch1Packs[i];
                    Record.Ch2[Record.Ch1.Length - d + i] = Ch2Packs[i];
                }

                View.UpdateChart(Record);
            };

            View.AvailablePorts = Serial.AvailablePorts;
            View.RefreshPorts  += () => View.AvailablePorts = Serial.AvailablePorts;

            View.ConnectToDevice += () =>
            {
                if (!Serial.IsConnected)
                {// Открытие порта
                    Serial.Connect(View.ComPort, View.BaudRate,
                                   error => View.ErrorHandler("Ошибка открытия порта", error));
                }
                else
                {// Закрытие порта
                    Serial.Disconnect(
                        error => View.ErrorHandler("Ошибка закрытия порта", error));
                }
            };
            Serial.ConnectionChanged += state => View.IsConnected = state;
            #endregion

            View.Exit += () =>
            {
                Serial.Disconnect(error => View.ErrorHandler("Ошибка закрытия порта", error));
                View.Close();
            };

            View.LoadRecord += () =>
            {
                var record = FileWorker.LoadRecord(
                    error => View.ErrorHandler("Ошибка открытия файла", error));
                if (record == null)
                {
                    return;
                }
                Record = record;
                View.UpdateChart(Record);
            };

            View.SaveRecord += () =>
            {
                FileWorker.SaveSeries(Record,
                                      error => View.ErrorHandler("Ошибка сохранения файла", error));
            };

            View.StartRecord += () =>
            {
                var T = View.RecordDuration;
                Record.Ch1 = new double[(int)(T * Fd)];
                Record.Ch2 = new double[(int)(T * Fd)];
                Serial.FlushRxBuffer();
                Requests.StartMeasure();
                timer.Start();
            };

            View.StopRecord += () =>
            {
                Requests.StopMeasure();
                timer.Stop();
            };
        }