From e25f4b0e2fb24076d4fefa935406d3179300318a Mon Sep 17 00:00:00 2001 From: xyy <544939200@qq.com> Date: Thu, 2 Apr 2026 10:13:01 +0800 Subject: [PATCH] --- Helpers/PlcConfiguration.cs | 2 ++ Service/ModbusTcpPlcService.cs | 2 +- ViewModels/MainViewModel.cs | 59 +++++++++++++++++++++++++++++++++- Views/MainWindow.xaml | 16 ++++++++- Views/ParameterWindow.xaml | 20 +----------- Views/ParameterWindow.xaml.cs | 4 +-- 6 files changed, 79 insertions(+), 24 deletions(-) diff --git a/Helpers/PlcConfiguration.cs b/Helpers/PlcConfiguration.cs index 023e91e..ea86359 100644 --- a/Helpers/PlcConfiguration.cs +++ b/Helpers/PlcConfiguration.cs @@ -113,5 +113,7 @@ namespace MembranePoreTester.Communication Task WriteMultipleRegistersAsync(ushort registerAddress, float value); float UshortToFloat(ushort P1, ushort P2); + + Task ReadFloatAsync(ushort startAddress); } } \ No newline at end of file diff --git a/Service/ModbusTcpPlcService.cs b/Service/ModbusTcpPlcService.cs index dddf5f6..fefd33b 100644 --- a/Service/ModbusTcpPlcService.cs +++ b/Service/ModbusTcpPlcService.cs @@ -28,7 +28,7 @@ namespace MembranePoreTester.Communication } // 读取两个连续的保持寄存器,转换为32位浮点数(假设大端模式) - private async Task ReadFloatAsync(ushort startAddress) + public async Task ReadFloatAsync(ushort startAddress) { await EnsureConnectedAsync(); var registers = await ReadHoldingRegistersAsync(startAddress, 2); diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 3caed85..64aea12 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -110,6 +110,57 @@ namespace MembranePoreTester.ViewModels + + private double _pressureUpperLimit; + private double _pressureRate; + + public double PressureUpperLimit + { + get => _pressureUpperLimit; + set + { + if (SetProperty(ref _pressureUpperLimit, value)) + { + // 值改变时写入PLC + _ = WriteFloatAsync(_plcConfig.PressureUpperLimit, (float)value); + } + } + } + + public double PressureRate + { + get => _pressureRate; + set + { + if (SetProperty(ref _pressureRate, value)) + { + // 值改变时写入PLC + _ = WriteFloatAsync(_plcConfig.PressureRate, (float)value); + } + } + } + + // 添加读取这两个参数的方法 + private async Task ReadPressureParametersAsync() + { + try + { + float upperLimit = await _plcService.ReadFloatAsync(_plcConfig.PressureUpperLimit); + float rate = await _plcService.ReadFloatAsync(_plcConfig.PressureRate); + + Application.Current.Dispatcher.Invoke(() => + { + PressureUpperLimit = upperLimit; + PressureRate = rate; + }); + } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"读取压力参数失败: {ex.Message}"); + } + } + + public StationItem() { @@ -153,13 +204,19 @@ namespace MembranePoreTester.ViewModels // 启动定时器,每秒读取一次 M21 状态 _timer = new System.Windows.Threading.DispatcherTimer(); _timer.Interval = TimeSpan.FromSeconds(1); - _timer.Tick += async (s, e) => await ReadEnableStatusAsync(); + _timer.Tick += async (s, e) => + { + await ReadEnableStatusAsync(); + await ReadPressureParametersAsync(); // 新增:读取压力参数 + }; _timer.Start(); // 延迟2秒后读取,确保连接稳定 Task.Delay(2000).ContinueWith(async _ => { await ReadPressureModeAsync(); + await ReadPressureParametersAsync(); // 新增:读取压力参数 + }, TaskScheduler.Default); } diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index f095b07..c4e6dd4 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -5,7 +5,7 @@ xmlns:viewModels="clr-namespace:MembranePoreTester.ViewModels" xmlns:conv="clr-namespace:MembranePoreTester.Converters" Title="膜孔径测试系统 (GB/T 32361-2015)" - Width="1024" Height="768" WindowState="Maximized" + Width="1024" MinHeight="768" WindowState="Maximized" WindowStartupLocation="CenterScreen" KeyDown="Window_KeyDown" Background="#F5F7FA" FontFamily="Segoe UI"> @@ -138,6 +138,20 @@ + + + + + + + + + + diff --git a/Views/ParameterWindow.xaml b/Views/ParameterWindow.xaml index 4dcdf90..70f75ac 100644 --- a/Views/ParameterWindow.xaml +++ b/Views/ParameterWindow.xaml @@ -5,25 +5,7 @@ WindowStartupLocation="CenterOwner"> - - - - - - - - - - - - - - - - - - + diff --git a/Views/ParameterWindow.xaml.cs b/Views/ParameterWindow.xaml.cs index 527cd45..cdded0b 100644 --- a/Views/ParameterWindow.xaml.cs +++ b/Views/ParameterWindow.xaml.cs @@ -28,8 +28,8 @@ namespace MembranePoreTester.Views // 初始化文本框到寄存器地址的映射 _textBoxMapping = new Dictionary { - [txtPressureUpper] = (_config.PressureUpperLimit, "加压上限"), - [txtPressureRate] = (_config.PressureRate, "加压速率"), + //[txtPressureUpper] = (_config.PressureUpperLimit, "加压上限"), + //[txtPressureRate] = (_config.PressureRate, "加压速率"), //[txtPressureCoeff] = (_config.PressureCoeff, "加压系数"), [txtHPCoeff1] = (_config.HPCoeff1, "工位1高压系数"),