|
- using ModbusDemo.Event;
- using ModbusDemo.Uitls;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Regions;
- using System;
- using System.Collections.Generic;
- using System.IO.Ports;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Timers;
- using Timer = System.Timers.Timer;
- using ModbusDemo.Device;
- namespace ModbusDemo.VIewModel
- {
-
- class SettingsUCViewModel : BindableBase
- {
- //
- private ModbusRTU _modbusRTU;
- //定时器
- private readonly Timer _checkTimer;
- //事件聚合器
- private readonly IEventAggregator _eventAggregator;
- //控制页面跳转
- private readonly IRegionManager _regionManager;
- //断开连接的命令
- public DelegateCommand DisConnectionCmm { get; set; }
- //链接窗口的命令
- public DelegateCommand ConnectionCmm { get; set; }
-
- //获取串口
- private SerialPort _serialPort;
- //设置串口名字
- private string _portName = "COM3";
-
- public string PortName
- {
- get { return _portName; }
- set
- {
- _portName = value;
- RaisePropertyChanged();
- }
- }
-
- //设置波特率
- private string _baudRate = "9600";
-
- public string BaudRate
- {
- get { return _baudRate; }
- set
- {
- _baudRate = value;
- RaisePropertyChanged();
- }
- }
-
- //设置数据位
- private string _dataBits = "8";
-
- public string DataBits
- {
- get { return _dataBits; }
- set
- {
- _dataBits = value;
-
- RaisePropertyChanged();
- }
- }
- //设置校验位
- private Parity _parity = Parity.Even;
-
- public Parity Parity
- {
- get { return _parity; }
- set
- {
- _parity = value;
- RaisePropertyChanged();
- }
- }
-
- //设置停止位
- private StopBits _stopBits = StopBits.One;
-
- public StopBits StopBits
- {
- get { return _stopBits; }
- set
- {
- _stopBits = value;
- RaisePropertyChanged();
- }
- }
-
- //设置读取和写入时间
- private string _timeOut = "200";
-
- public string TimeOut
- {
- get { return _timeOut; }
- set
- {
- _timeOut = value;
- RaisePropertyChanged();
- }
- }
-
-
- public SettingsUCViewModel()
- {
-
- }
- /// <summary>
- /// 从容器中获取创建的窗口
- /// </summary>
- /// <param name="serialPort"></param>
- public SettingsUCViewModel(SerialPort serialPort, IRegionManager regionManager, IEventAggregator eventAggregator, ModbusRTU modbusRTU)
- {
- _serialPort = serialPort;
- ConnectionCmm = new DelegateCommand(Connection);
- DisConnectionCmm = new DelegateCommand(DisConnection);
- _regionManager = regionManager;
- _eventAggregator = eventAggregator;
- _modbusRTU = modbusRTU;
- _checkTimer = new Timer(5000); // 每秒检查一次
- _checkTimer.Elapsed += CheckPortStatus;
- _checkTimer.AutoReset = true;
-
- }
- private void CheckPortStatus(object sender, ElapsedEventArgs e)
- {
- //bool isConnected = SerialPort.GetPortNames().Contains(PortName);
- byte[] bytes = new byte[] { 0x01, 0x01, 0x01, 0x2C, 0x00, 0x01, 0x3D, 0xFF };
- byte[] response = _modbusRTU.SendMessage(bytes);
- if (response ==null)
- {
- _checkTimer.Stop();
- _serialPort.Close();
- MessageBox.Show("当前连接已经断开");
- }
- }
-
- private void DisConnection()
- {
- if (_serialPort.IsOpen)
- {
- _checkTimer.Stop();
- _serialPort.Close();
-
- }
- MessageBox.Show("连接已经断开");
- }
-
- /// <summary>
- /// 串口参数设置
- /// </summary>
- private void Connection()
- {
- //TODO,做数据的判空处理
- if (!_serialPort.IsOpen)
- {
-
- _serialPort.PortName = GetComboBoxItemValue(this.PortName);
- _serialPort.BaudRate = int.Parse(GetComboBoxItemValue(this.BaudRate));
- _serialPort.Parity = this.Parity;
- if (this.StopBits == StopBits.None)
- {
- this.StopBits = StopBits.One;
- MessageBox.Show("当前设备不支持无停止位(None)模式,已自动切换为 1 个停止位。");
- }
- _serialPort.StopBits = this.StopBits;
- _serialPort.DataBits = int.Parse(GetComboBoxItemValue(this.DataBits));
-
- //读取超时时间
- _serialPort.ReadTimeout = int.Parse(GetComboBoxItemValue(this.TimeOut));
- //写入超时时间
- _serialPort.WriteTimeout = int.Parse(GetComboBoxItemValue(this.TimeOut));
- try
- {
- _serialPort.Open();
- // 发布事件通知其他ViewModel
- _eventAggregator.GetEvent<SerialPortSettingsChangedEvent>().Publish();
- MessageBox.Show("串口链接成功");
- _regionManager.Regions["ModbusRegion"].RequestNavigate("CoilUC");
- _checkTimer.Start();
- }
- catch (Exception)
- {
-
- MessageBox.Show("串口已经链接或者断开,请重新检查在尝试", "warning", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
- else
- {
- MessageBox.Show("当前串口已经连接,请先断开,再连接");
- }
-
- }
- /// <summary>
- /// 处理下拉框的选择信息
- /// </summary>
- /// <param name="ComboBoxItem"></param>
- /// <returns></returns>
- private string GetComboBoxItemValue(string ComboBoxItem)
- {
-
- string displayText = ComboBoxItem.ToString();
- string cleanText = displayText.Replace("System.Windows.Controls.ComboBoxItem: ", "");
- return cleanText;
- }
-
-
- }
- }
|