更新
This commit is contained in:
@@ -440,6 +440,36 @@ namespace COFTester.Services
|
||||
OnErrorOccurred($"停止测试失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 写入启动/停止寄存器 M31
|
||||
/// M30 复归型, M31 标注为 1=开始,0=停止
|
||||
/// </summary>
|
||||
/// <param name="value">1=开始测试, 0=停止测试</param>
|
||||
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<TestDataPoint?> ReadSensorDataAsync();
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user