UCMonitorData.xaml.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using MV485.helper;
  2. using MV485.model;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.IO.Ports;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace MV485.uc
  20. {
  21. /// <summary>
  22. /// UCMoniterData.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class UCMonitorData : UserControl
  25. {
  26. private bool _isMoniting = false; //正在监听
  27. private ObservableCollection<WMData> _deviceDataCollection { get; set; } // 用于存储多个设备的数据
  28. private ModbusRtuClient _modbusClient;
  29. public UCMonitorData()
  30. {
  31. InitializeComponent();
  32. cmbAddress.ItemsSource = Constant.AddressList;
  33. _deviceDataCollection = new ObservableCollection<WMData>();
  34. DevicesDataGrid.ItemsSource = _deviceDataCollection;
  35. }
  36. private void BtnStartMonitor_Click(object sender, RoutedEventArgs e)
  37. {
  38. if (_isMoniting)
  39. {
  40. StopMonitor();
  41. }
  42. else
  43. {
  44. StartMonitor();
  45. }
  46. }
  47. private void StartMonitor()
  48. {
  49. // 初始化多个 Modbus 客户端
  50. PortModel portModel = new PortModel()
  51. {
  52. PortName = "COM6",
  53. BaudRate = 9600,
  54. Parity = Parity.None,
  55. DataBits = 8,
  56. StopBits = StopBits.One
  57. };
  58. List<byte> deviceAddresses = new List<byte>() { 1 };
  59. _modbusClient = new ModbusRtuClient(deviceAddresses, portModel);
  60. // 订阅数据更新事件
  61. _modbusClient.DataReceived += UpdateDeviceData;
  62. _modbusClient.ConnectionStatusChanged += _modbusClient_ConnectionStatusChanged;
  63. // 启动读取数据线程
  64. _modbusClient.StartAsync(10);
  65. _isMoniting = true;
  66. btnStartMonitor.Content = "停止监控";
  67. }
  68. private void StopMonitor()
  69. {
  70. _modbusClient?.StopAsync().GetAwaiter().GetResult();
  71. _isMoniting = false;
  72. btnStartMonitor.Content = "开始监控";
  73. }
  74. private void UpdateDeviceData(WMData data)
  75. {
  76. Application.Current.Dispatcher.Invoke(() =>
  77. {
  78. _deviceDataCollection.Add(data);
  79. });
  80. }
  81. private void _modbusClient_ConnectionStatusChanged(ModbusRtuClient modbusRtuClient, bool blStatus)
  82. {
  83. string portName = modbusRtuClient.GetPortMode().PortName;
  84. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  85. {
  86. txtComStauts.Text = $"{portName}: {(blStatus ? "连接" : "断开")}";
  87. }));
  88. }
  89. private void UserControl_Unloaded(object sender, RoutedEventArgs e)
  90. {
  91. //让异步操作以同步方式执行,会阻塞UI线程
  92. _modbusClient?.StopAsync().GetAwaiter().GetResult();
  93. }
  94. private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
  95. {
  96. if (e.Text.All(char.IsDigit))
  97. {
  98. e.Handled = false; // 如果是数字,允许输入
  99. }
  100. else
  101. {
  102. // 如果当前输入的字符不是数字,并且是中文输入法的拼音或候选字,禁止输入
  103. e.Handled = true;
  104. }
  105. }
  106. //设置设备地址
  107. private async void BtnSetAddress_Click(object sender, RoutedEventArgs e)
  108. {
  109. if (ushort.TryParse(cmbAddress.Text, out ushort address))
  110. {
  111. bool setResult = await _modbusClient.SetModleID(1, address);
  112. }
  113. else
  114. {
  115. MessageBox.Show("设备地址错误!");
  116. }
  117. }
  118. private void BtnGetPortNames_Click(object sender, RoutedEventArgs e)
  119. {
  120. List<SPortModel> portModels = SerialHelper.GetSerialPortsWithDetails();
  121. foreach (var port in portModels)
  122. {
  123. Console.WriteLine($"端口: {port.PortName} - 设备名称: {port.DeviceName}");
  124. }
  125. }
  126. //-------------------------------------------------------------------
  127. }
  128. }