using MV485.helper; using MV485.model; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MV485.uc { /// /// UCMoniterData.xaml 的交互逻辑 /// public partial class UCMonitorData : UserControl { private bool _isMoniting = false; //正在监听 private ObservableCollection _deviceDataCollection { get; set; } // 用于存储多个设备的数据 private ModbusRtuClient _modbusClient; public UCMonitorData() { InitializeComponent(); cmbAddress.ItemsSource = Constant.AddressList; _deviceDataCollection = new ObservableCollection(); DevicesDataGrid.ItemsSource = _deviceDataCollection; } private void BtnStartMonitor_Click(object sender, RoutedEventArgs e) { if (_isMoniting) { StopMonitor(); } else { StartMonitor(); } } private void StartMonitor() { // 初始化多个 Modbus 客户端 PortModel portModel = new PortModel() { PortName = "COM6", BaudRate = 9600, Parity = Parity.None, DataBits = 8, StopBits = StopBits.One }; List deviceAddresses = new List() { 1 }; _modbusClient = new ModbusRtuClient(deviceAddresses, portModel); // 订阅数据更新事件 _modbusClient.DataReceived += UpdateDeviceData; _modbusClient.ConnectionStatusChanged += _modbusClient_ConnectionStatusChanged; // 启动读取数据线程 _modbusClient.StartAsync(10); _isMoniting = true; btnStartMonitor.Content = "停止监控"; } private void StopMonitor() { _modbusClient?.StopAsync().GetAwaiter().GetResult(); _isMoniting = false; btnStartMonitor.Content = "开始监控"; } private void UpdateDeviceData(WMData data) { Application.Current.Dispatcher.Invoke(() => { _deviceDataCollection.Add(data); }); } private void _modbusClient_ConnectionStatusChanged(ModbusRtuClient modbusRtuClient, bool blStatus) { string portName = modbusRtuClient.GetPortMode().PortName; Application.Current.Dispatcher.BeginInvoke(new Action(() => { txtComStauts.Text = $"{portName}: {(blStatus ? "连接" : "断开")}"; })); } private void UserControl_Unloaded(object sender, RoutedEventArgs e) { //让异步操作以同步方式执行,会阻塞UI线程 _modbusClient?.StopAsync().GetAwaiter().GetResult(); } private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e) { if (e.Text.All(char.IsDigit)) { e.Handled = false; // 如果是数字,允许输入 } else { // 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入 e.Handled = true; } } //设置设备地址 private async void BtnSetAddress_Click(object sender, RoutedEventArgs e) { if (ushort.TryParse(cmbAddress.Text, out ushort address)) { bool setResult = await _modbusClient.SetModleID(1, address); } else { MessageBox.Show("设备地址错误!"); } } private void BtnGetPortNames_Click(object sender, RoutedEventArgs e) { List portModels = SerialHelper.GetSerialPortsWithDetails(); foreach (var port in portModels) { Console.WriteLine($"端口: {port.PortName} - 设备名称: {port.DeviceName}"); } } //------------------------------------------------------------------- } }