From 1cc79a6ad6e7fa13f6c33d7135417623bd11d634 Mon Sep 17 00:00:00 2001 From: rain Date: Sat, 16 May 2026 10:55:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A1=B5=E9=9D=A2=E9=80=BB=E8=BE=91=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Models/PlcConfiguration.cs | 10 +++++++++- Services/IPlcService.cs | 8 +++++++- ViewModels/StationViewModel.cs | 22 +++++++++++++++++++--- Views/MainWindow.xaml.cs | 28 +--------------------------- appsettings.json | 7 ++++++- 5 files changed, 42 insertions(+), 33 deletions(-) diff --git a/Models/PlcConfiguration.cs b/Models/PlcConfiguration.cs index 776e4f8..fec9ee0 100644 --- a/Models/PlcConfiguration.cs +++ b/Models/PlcConfiguration.cs @@ -12,11 +12,19 @@ public ushort HardnessStartCoil2 { get; set; } public ushort HardnessStartCoil3 { get; set; } public ushort HardnessCompleteCoil { get; set; } - + public ushort HardnessStartReset { get; set; } + public ushort HardnessStartStop { get; set; } + // 脆碎度 public ushort FriabilityStartCoil { get; set; } public ushort WeightBefore { get; set; } // 天平重量寄存器(可选) public ushort WeightAfter { get; set; } + public ushort FriabilityStartCoil2 { get; set; } + public ushort FriabilityStartCoil3 { get; set; } + + public ushort FriabilityStartCoilStop { get; set; } + public ushort FriabilityStartCoilReset { get; set; } + // 崩解 public ushort DisintegrationTemp { get; set; } diff --git a/Services/IPlcService.cs b/Services/IPlcService.cs index 5359fb5..30be2b3 100644 --- a/Services/IPlcService.cs +++ b/Services/IPlcService.cs @@ -3,13 +3,19 @@ namespace TabletTester2025.Services { public interface IPlcService - { + { //建立通讯链接 Task ConnectAsync(); + //从 PLC 的指定起始地址,读取 1 个 32 位浮点型(float)数据。 Task ReadFloatAsync(ushort startAddress); + //从 PLC 的指定起始地址,读取 1 个 32 位整型(int)数据。 Task ReadIntAsync(ushort startAddress); + //向 PLC 的指定线圈地址,写入一个布尔值(开关量)。 Task WriteCoilAsync(ushort coilAddress, bool value); + //读取 PLC 指定线圈地址的布尔状态,是 WriteCoilAsync 的配套读取方法。 Task ReadCoilAsync(ushort coilAddress); + //向 PLC 的指定寄存器地址,写入 1 个 16 位无符号整型(ushort)数据。 Task WriteRegisterAsync(ushort registerAddress, ushort value); + //批量读取 PLC 的保持寄存器数据,是工业通信中最高效的批量读取方法。 Task ReadHoldingRegistersAsync(ushort startAddress, ushort count); bool IsConnected { get; } } diff --git a/ViewModels/StationViewModel.cs b/ViewModels/StationViewModel.cs index cc9a466..200ce2f 100644 --- a/ViewModels/StationViewModel.cs +++ b/ViewModels/StationViewModel.cs @@ -78,7 +78,7 @@ namespace TabletTester2025.ViewModels [ObservableProperty] private bool _friabilityCounterClockwise; [ObservableProperty] private double _friabilityCurrentRpm; [ObservableProperty] private int _friabilityRemainingRounds = 100; - + public IAsyncRelayCommand StopHardnessCommand { get; } public IAsyncRelayCommand StopFriabilityCommand { get; } public IAsyncRelayCommand ResetFriabilityCommand { get; } public IAsyncRelayCommand PrintFriabilityCommand { get; } @@ -153,8 +153,13 @@ namespace TabletTester2025.ViewModels await _plc.WriteCoilAsync(0x21, false); }); - HardnessResetCommand = new AsyncRelayCommand(() => + //复位 + HardnessResetCommand = new AsyncRelayCommand(async () => { + // 1. 标准PLC按钮脉冲逻辑:置1 → 延时 → 置0(模拟按下再松开按钮) + await _plc.WriteCoilAsync(_plcConfig.HardnessStartReset, true); + await Task.Delay(100); // 脉冲宽度,可根据PLC程序调整20~100ms + await _plc.WriteCoilAsync(_plcConfig.HardnessStartReset, false); _hardnessResults.Clear(); HardnessValue = 0; HardnessAvg = 0; @@ -163,14 +168,25 @@ namespace TabletTester2025.ViewModels HardnessMax = 0; HardnessMin = 0; Phase = TestPhase.Idle; - return Task.CompletedTask; + }); PrintHardnessCommand = new AsyncRelayCommand(async () => await PrintReport("硬度")); + // 硬度命令停止 + StopHardnessCommand = new AsyncRelayCommand(async() => { + + await _plc.WriteCoilAsync(_plcConfig.HardnessStartStop, true); + await Task.Delay(100); // 脉冲宽度,可根据PLC程序调整20~100ms + await _plc.WriteCoilAsync(_plcConfig.HardnessStartStop, false); + + Phase = TestPhase.Idle; + }); // 脆碎度命令 StopFriabilityCommand = new AsyncRelayCommand(() => { + //测试停止 + Phase = TestPhase.Idle; return Task.CompletedTask; }); ResetFriabilityCommand = new AsyncRelayCommand(() => diff --git a/Views/MainWindow.xaml.cs b/Views/MainWindow.xaml.cs index 6b06c25..023db6d 100644 --- a/Views/MainWindow.xaml.cs +++ b/Views/MainWindow.xaml.cs @@ -15,33 +15,7 @@ namespace TabletTester2025 } - //硬度测试 - // 启动测试按钮点击事件 - private async void btnStartTest_Click(object sender, RoutedEventArgs e) - { - - } - - // 模拟PLC读取硬度值(实际项目替换为你的ReadFloatAsync调用) - private async Task ReadPlcHardnessAsync() - { - await Task.Delay(100); // 模拟通信延迟 - // 这里可以直接用你之前写好的PLC读取方法: - // return await _plc.ReadFloatAsync((ushort)你的硬度地址); - return new Random().Next(80, 120) + (float)new Random().NextDouble() * 10; - } - - // 复位按钮点击事件 - private void btnReset_Click(object sender, RoutedEventArgs e) - { - - } - - // 梁杆上升按钮(示例) - private void btnUp_Click(object sender, RoutedEventArgs e) - { - - } + } } \ No newline at end of file diff --git a/appsettings.json b/appsettings.json index eeb32a1..f56a56e 100644 --- a/appsettings.json +++ b/appsettings.json @@ -11,11 +11,16 @@ "HardnessStartCoil": 70, //硬度工位1启动测试M70 "HardnessStartCoil2": 70, //硬度工位1启动测试M70 "HardnessStartCoil3": 70, //硬度工位1启动测试M70 + "HardnessStartStop": 73, // 硬度停止 + "HardnessStartReset": 90, // 硬复位启动 + "FriabilityStartCoil": 80, //脆碎工位1启动测试M70 "FriabilityStartCoil2": 80, //脆碎工位1启动测试M70 "FriabilityStartCoil3": 80, //脆碎工位1启动测试M70 + "FriabilityStartCoilStop": 83, // 脆碎停止 + "FriabilityStartCoilReset": 95, // 脆碎复位启动 "HardnessCompleteCoil": 11, - //"FriabilityStartCoil": 20, + "WeightBefore": 200, "WeightAfter": 202, "DisintegrationTemp": 300,