예제 #1
0
        public void Stop_measurement()
        {
            if (Modbus != null)
            {
                // ОТПИСЫВАЕМСЯ ОТ ОБРАБОТЧИКОВ СОБЫТИЯ ResposeReceived
                Modbus.ResponseReceived         -= this.IdentifyStatus;
                Modbus.ResponseReceived         -= this.Get_R;
                Modbus.ResponseReceived         -= this.Get_L;
                Modbus.ResponseReceived         -= this.Get_C;
                Modbus.ResponseReceived         -= this.Get_M;
                Modbus.ResponseReceived         -= this.Get_F;
                Modbus.ResponseReceived         -= this.Get_tgR;
                Modbus.ResponseReceived         -= this.Get_tgL;
                Modbus.ResponseReceived         -= this.Get_tgC;
                Modbus.ResponseReceived         -= this.Get_tgM;
                Modbus.ResponseReceived         -= this.DisplayResponseMessageInConsole;
                Modbus.RequestSent              -= this.DisplayRequestMessageInConsole;
                Modbus.CRC_Error                -= this.ProcessMissedResult;
                Modbus.SlaveError               -= this.ProcessMissedResult;
                Modbus.DeviceNotRespondingError -= this.ProcessMissedResult;

                // ПРИОСТАНАВЛИВАЕМ ТАЙМЕР
                Timer.Change(Timeout.Infinite, 0); // Приостанавливаем вызов метода GetSlaveState
                Thread.Sleep(200);

                Modbus.Close(); // Закрываем COM порт
                Modbus = null;  // Ссылка в null

                DisplayInactiveMesResults();
            }
        }
예제 #2
0
        private void Start_measurement()
        {
            if (Modbus == null)
            {
                CurrentModbusRTUSettings = new ModbusRTUSettings();                              // Создаем объект настроек
                CurrentModbusRTUSettings.SettingsFileNotFoundError += this.DisplayErrorOccurred; // Подписываемся на обработчик события "не найден файл настроек"
                CurrentModbusRTUSettings.SettingsFileReadingError  += this.DisplayErrorOccurred; // Подписываемся на обработчик события "ошибка при чтении файла настроек"

                CurrentModbusRTUSettings.GetCurrentSettings();                                   // Считываем настройки из файла настроек

                Modbus = new ModbusRTU(CurrentModbusRTUSettings);                                // Создаем объект ModbusRTU

                // Modbus.DeviceNotRespondingError += this.DisplayErrorOccurred; // Подписываемся на обработчик события "Устройство не отвечает"
                Modbus.SerialPortOpeningError += this.DisplayErrorOccurred;            // Подписываемся на обработчик события "Ошибка открытия порта"
                Modbus.RequestSent            += this.DisplayRequestMessageInConsole;  // Подписываемся на обработчик события "Отправлена команда"
                Modbus.ResponseReceived       += this.DisplayResponseMessageInConsole; // Подписываемся на обработчик события "Получен ответ"
                Modbus.CRC_Error  += this.ProcessMissedResult;
                Modbus.SlaveError += this.ProcessMissedResult;
                Modbus.DeviceNotRespondingError += this.ProcessMissedResult;

                // Создаем функцию обратного вызова по таймеру
                Timer = new Timer(new TimerCallback(GetSlaveState), null, 0, CurrentModbusRTUSettings.PollingInterval * 1000);
            }
        }