Пример #1
0
        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;
        }
Пример #2
0
        private void update(string key, string value, string des = "")
        {
            var temp = config.FirstOrDefault(m => m.key == key);

            if (temp != null)
            {
                des = des == "" ? temp.des : des;
                ModbusConfig u = new ModbusConfig()
                {
                    Id = temp.Id, value = value, key = temp.key, des = temp.des
                };
                db.Entry <ModbusConfig>(u).State = EntityState.Modified;
                db.SaveChanges();
            }
            else
            {
                temp = new ModbusConfig
                {
                    key   = key,
                    value = value,
                    des   = des
                };
                db.ModbusConfig.Add(temp);
                db.SaveChanges();
            }
        }
Пример #3
0
        /// <summary>
        /// 初始化运行环境
        /// </summary>
        /// <param name="modbusConfigFile">配置文件物理路径(包含名称+后缀):例如:Config/ModbusConfig.xml</param>
        /// <param name="serialPortConfigFile">配置文件获路径(包含文件名+后缀)例如:Config/SerialPortConfig.xml</param>
        public void InitializeFromConfigFile(string modbusConfigFile, string serialPortConfigFile)
        {
            ModbusConfig modbusConfig = new ModbusConfig();

            modbusConfig            = GetModbusConfigFromFile(modbusConfigFile);
            dataAnalyzeMode         = modbusConfig.DataAnalyzeMode;
            allDataPoints           = modbusConfig.DataPointsFromConfigFileList.ToList();
            allReadRegisterCommands = GetReadRegisterCommands(allDataPoints);

            //初始化串口
            var serialPorObj = CreateSerialPortFromConfigFile(serialPortConfigFile);

            this.serialPort = new SerialPortHelper(serialPorObj);
        }
Пример #4
0
 public EditionViewModel(ModbusConfig config)
 {
     _config = config;
 }
Пример #5
0
 public override void OnNavigatedFrom()
 {
     Settings.Default.ModBusConfig = _modbusCfg;
     ModbusConfig.Save(ModbusConfig);
 }
Пример #6
0
        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();
        }
 public void ApplyConfig(ModBusDriver driver, ModbusConfig config)
 {
     _config = config;
     _driver = driver;
 }
        public DataControlViewModel(ModBusDriver driver, ModbusConfig config)
        {
            _config = config;
            _config.SelectedMemTypeChanged += OnSelectedMemTypeChanged;
            _driver = driver;
            //_lastCount = _config.Amount;
            //_lastIntro = _config.StartAddress;

            SetUshortFormat = new RelayCommand(o => {
                _mode      = 0;
                _lastCount = 0;
                UpdateTableSource(_mode);
            });
            SetUIntFormat = new RelayCommand(o => {
                _mode      = 1;
                _lastCount = 0;
                UpdateTableSource(_mode);
            });
            SetULongFormat = new RelayCommand(o => {
                _mode      = 2;
                _lastCount = 0;
                UpdateTableSource(_mode);
            });
            SetHexFormat = new RelayCommand(o => {
                _mode      = 3;
                _lastCount = 0;
                UpdateTableSource(_mode);
            });
            SetBinFormat = new RelayCommand(o => {
                _lastCount = 0;
                if (_config.SelectedMemType == Memtype.Coils | _config.SelectedMemType == Memtype.Inputs)
                {
                    _mode = 7;
                    UpdateTableSource(_mode);
                }
                else
                {
                    _mode = 4;
                    UpdateTableSource(_mode);
                }
            });
            SetFloatFormat = new RelayCommand(o => {
                _mode      = 5;
                _lastCount = 0;
                UpdateTableSource(_mode);
            });
            SetDoubleFormat = new RelayCommand(o => {
                _mode      = 6;
                _lastCount = 0;
                UpdateTableSource(_mode);
            });
            OpenEditDialog = new RelayCommand(o => {
                if (ControlTableSource.Count == 0)
                {
                    return;
                }
                EditRegister.BeginEdit(_mode, ControlTableSource[ControlPoint]);
            });

            WriteSingleCommand = new RelayCommand(o => {
                WriteSingleEvent?.Invoke(this, null);
            }, p => CanExecuteSingle());

            WriteMultipleCommand = new RelayCommand(o =>
            {
                WriteMultipleEvent?.Invoke(this, null);
            });
            AutoWriteMultipleCommand = new RelayCommand(o =>
            {
                if (EditRegister.AutoWrite)
                {
                    WriteMultipleEvent?.Invoke(this, null);
                }
            });

            CoilList   = new BindingList <CoilPoint>();
            UshortList = new BindingList <UshortPoint>();
            UIntList   = new BindingList <UIntPoint>();
            ULongList  = new BindingList <ULongPoint>();
            FloatList  = new BindingList <FloatPoint>();
            DoubleList = new BindingList <DoublePoint>();
            BitList    = new BindingList <BitPoint>();
            HexList    = new BindingList <HexPoint>();

            if (_config.SelectedMemType == Memtype.Coils | _config.SelectedMemType == Memtype.Inputs)
            {
                _mode = 7;
                ControlTableSource = CoilList;
            }
            else
            {
                _mode = 0;
                ControlTableSource = UshortList;
            }
            EditRegister = new EditionViewModel(_config);
            ControlTableSource.ListChanged += OnListChanged;
        }