From 3edf2b4e3922464d50753af26e3f07fa07eb77a2 Mon Sep 17 00:00:00 2001 From: "GukSang.Jin" Date: Thu, 29 Jan 2026 20:24:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CSI-H238M/CSI-H238M/Services/ModbusService.cs | 30 ++++++++++ CSI-H238M/CSI-H238M/ViewModels/ViewModel.cs | 57 +++++++++++++++---- 2 files changed, 76 insertions(+), 11 deletions(-) diff --git a/CSI-H238M/CSI-H238M/Services/ModbusService.cs b/CSI-H238M/CSI-H238M/Services/ModbusService.cs index 4d2c086..52a3d34 100644 --- a/CSI-H238M/CSI-H238M/Services/ModbusService.cs +++ b/CSI-H238M/CSI-H238M/Services/ModbusService.cs @@ -440,6 +440,36 @@ namespace COFTester.Services OnErrorOccurred($"停止测试失败: {ex.Message}"); } } + + /// + /// 写入启动/停止寄存器 M31 + /// M30 复归型, M31 标注为 1=开始,0=停止 + /// + /// 1=开始测试, 0=停止测试 + public virtual async Task WriteStartStopRegisterAsync(ushort value) + { + try + { + if (_modbusMaster != null && _isConnected) + { + const ushort START_STOP_REGISTER = 31; // M31 + + System.Diagnostics.Debug.WriteLine($"[Modbus] 写入启动/停止寄存器 M{START_STOP_REGISTER} = {value}"); + await _modbusMaster.WriteSingleCoilAsync(1, START_STOP_REGISTER, value == 1); + + // 延迟确保 PLC 处理完成 + await Task.Delay(100); + + System.Diagnostics.Debug.WriteLine($"[Modbus] M{START_STOP_REGISTER} 写入完成"); + } + } + catch (Exception ex) + { + OnErrorOccurred($"写入启动/停止寄存器失败: {ex.Message}"); + System.Diagnostics.Debug.WriteLine($"[Modbus] 写入 M31 异常: {ex.Message}"); + throw; + } + } protected abstract Task AcquisitionLoopAsync(TestParameters parameters, CancellationToken token); protected abstract Task ReadSensorDataAsync(); diff --git a/CSI-H238M/CSI-H238M/ViewModels/ViewModel.cs b/CSI-H238M/CSI-H238M/ViewModels/ViewModel.cs index 80fcfd8..4365c51 100644 --- a/CSI-H238M/CSI-H238M/ViewModels/ViewModel.cs +++ b/CSI-H238M/CSI-H238M/ViewModels/ViewModel.cs @@ -613,12 +613,12 @@ namespace COFTester.ViewModels return; } - if (string.IsNullOrEmpty(_selectedDirection)) - { - StatusMessage = "请先选择测试方向"; - MessageBox.Show("请先选择测试方向(上升/下降/向右/向左)", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); - return; - } + //if (string.IsNullOrEmpty(_selectedDirection)) + //{ + // StatusMessage = "请先选择测试方向"; + // MessageBox.Show("请先选择测试方向(上升/下降/向右/向左)", "提示", MessageBoxButton.OK, MessageBoxImage.Warning); + // return; + //} _realTimePoints.Clear(); OnPropertyChanged(nameof(DataPointsCount)); @@ -627,17 +627,52 @@ namespace COFTester.ViewModels StatusMessage = $"开始测试 - {_selectedDirection}方向"; UpdateScottPlot(); - // 向选定方向的寄存器写入1,开始测试 - _daqService.StartDirectionTest(_selectedDirection); + // M30 复归型 M31标注为 1开始,0停止 + // 向 M31 写入 1 开始测试 + if (_daqService is ModbusServiceBase modbusService) + { + Task.Run(async () => + { + try + { + await modbusService.WriteStartStopRegisterAsync(1); // M31 = 1 开始 + System.Diagnostics.Debug.WriteLine("[ViewModel] M31 = 1 (开始测试)"); + } + catch (Exception ex) + { + Application.Current?.Dispatcher.InvokeAsync(() => + { + StatusMessage = $"启动测试失败: {ex.Message}"; + }); + } + }); + } + + // _daqService.StartDirectionTest(_selectedDirection); // 暂时注释掉 _daqService.StartAcquisition(Parameters); } private void StopTest() { - if (!string.IsNullOrEmpty(_selectedDirection)) + // M30 复归型 M31标注为 1开始,0停止 + // 向 M31 写入 0 停止测试 + if (_daqService is ModbusServiceBase modbusService) { - // 向选定方向的寄存器写入0,停止测试 - _daqService.StopDirectionTest(_selectedDirection); + Task.Run(async () => + { + try + { + await modbusService.WriteStartStopRegisterAsync(0); // M31 = 0 停止 + System.Diagnostics.Debug.WriteLine("[ViewModel] M31 = 0 (停止测试)"); + } + catch (Exception ex) + { + Application.Current?.Dispatcher.InvokeAsync(() => + { + StatusMessage = $"停止测试失败: {ex.Message}"; + }); + } + }); } _daqService.StopAcquisition();