private void RunPollig() { ModBusDriver?.Dispose(); try { CreateModbusDriver(); } catch (Exception e) { if (!_linkRecoveryTimer.IsEnabled) { StatusString = e.Message; AvaliablePorts = SerialPort.GetPortNames().ToList(); } return; } _timer.Interval = TimeSpan.FromMilliseconds(ScanRate); ModbusConfig.Save(ModbusConfig); _retriesToRecoverLink = 0; _linkRecoveryTimer.Stop(); _linkRecoveryTimer.Tick -= _connectionRecoveryHandler; _timer.Tick += _dataUpdateHandler; _timer.Start(); IsStopped = false; }
public override void OnNavigatedFrom() { Settings.Default.ModBusConfig = _modbusCfg; ModbusConfig.Save(ModbusConfig); }
public MainViewModel() { IsStopped = true; _modbusCfg = Settings.Default.ModBusConfig != null ? new ModbusConfig(Settings.Default.ModBusConfig) : new ModbusConfig(); ScanRate = 250; ModbusData = ModbusDataTable.CreateMbTable("Данные"); ReadData = new MBRequestData(); WriteData = new MBRequestData(); _tabsView = new TabDataView(); Navigator.Navigate(_tabsView, this); ReadClearCommand = new RelayCommand(p => ReadData.ClearRequestData()); WriteClearCommand = new RelayCommand(p => WriteData.ClearRequestData()); SaveCommand = new RelayCommand(p => { try { Settings.Default.ModBusConfig = _modbusCfg; ModbusConfig.Save(ModbusConfig); Navigator.Navigate(_tabsView, this); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.InnerException?.Message); } }); CancelCommand = new RelayCommand(p => Navigator.Navigate(_tabsView, this)); ExtensiveWindowCommand = new RelayCommand(p => { ExtWindow.Show(); ExtWindow.Activate(); }); SettingsViewCommand = new RelayCommand(p => Navigator.Navigate(new SettingsView(), this)); RunCommand = new RelayCommand(p => { var metroWindowCol = Application.Current.Windows; _tokenSrc = new CancellationTokenSource(); RunPollig(); }); StopCommand = new RelayCommand(p => { _timer.Stop(); _timer.Tick -= _dataUpdateHandler; _tokenSrc?.Cancel(); IsStopped = true; }); UpdatePorts = new RelayCommand(p => { AvaliablePorts = SerialPort.GetPortNames().ToList(); }); DataTab = new BindingList <ModbusDataPoint>(); DataControlViewModel = new DataControlViewModel(_driver, _modbusCfg); GraphModel = new GraphViewModel(this); DataControlViewModel.WriteSingleEvent += OnSingleRegWrite; DataControlViewModel.WriteMultipleEvent += OnMultipleWrite; _dataUpdateHandler = (sender, e) => UpdateData(); _connectionRecoveryHandler = (sender, e) => LinkRecoveryTimerOnTick(); }