diff --git a/Services/PlcSimulator.cs b/Services/PlcSimulator.cs index c870b4d..7d4360c 100644 --- a/Services/PlcSimulator.cs +++ b/Services/PlcSimulator.cs @@ -19,6 +19,7 @@ namespace TabletTester2025.Services { 72 => 40 + (float)_rand.NextDouble() * 20, // 硬度最大采集力 100 => 40 + (float)_rand.NextDouble() * 20, // 硬度 40~60N + 82 => _rand.Next(0, 101), // 脆碎实时圈数 410 => 100f, // 脆碎圈数 412 => 5.0f + (float)_rand.NextDouble() * 2, // 脆碎度前重 414 => 4.9f + (float)_rand.NextDouble() * 2, // 后重 @@ -41,7 +42,6 @@ namespace TabletTester2025.Services { int value = startAddress switch { - 82 => _rand.Next(0, 101), // 脆碎实时圈数 410 => 100, // 脆碎圈数 430 => 30, // 溶出1时间(min) 440 => 30, // 溶出2时间(min) diff --git a/ViewModels/StationViewModel.cs b/ViewModels/StationViewModel.cs index d11e456..ef25fc5 100644 --- a/ViewModels/StationViewModel.cs +++ b/ViewModels/StationViewModel.cs @@ -149,7 +149,7 @@ namespace TabletTester2025.ViewModels [ObservableProperty] private bool _friabilityClockwise = true; [ObservableProperty] private bool _friabilityCounterClockwise; [ObservableProperty] private double _friabilityCurrentRpm; - [ObservableProperty] private int _friabilityRealtimeRounds; + [ObservableProperty] private double _friabilityRealtimeRounds; [ObservableProperty] private int _friabilityRemainingRounds = 100; public IAsyncRelayCommand StopHardnessCommand { get; } public IAsyncRelayCommand StopFriabilityCommand { get; } @@ -1885,36 +1885,35 @@ namespace TabletTester2025.ViewModels throw new InvalidOperationException("脆碎前重量必须大于0"); double rpm = FriabilityTargetRpm > 0 ? FriabilityTargetRpm : 25; - double testTimeMin = FriabilityTargetTimeMin > 0 ? FriabilityTargetTimeMin : 4; int totalRounds = Math.Max(1, FriabilityTargetRounds); FriabilityRemainingRounds = totalRounds; FriabilityCurrentRpm = rpm; await RefreshFriabilityRealtimeRoundsAsync(); await PulseCoilAsync(startCoil); - int durationMs = (int)Math.Ceiling(testTimeMin * 60 * 1000); // 总运行时间(毫秒) + bool targetRoundsReached = false; - for (int i = 0; i < durationMs; i += 100) + while (_isFriabilityRunning) { - // 如果用户点了停止,只结束脆碎度测试,不影响其它测试 - if (!_isFriabilityRunning) + await RefreshFriabilityRealtimeRoundsAsync(); + + double completedRounds = Math.Max(0, FriabilityRealtimeRounds); + FriabilityRemainingRounds = (int)Math.Ceiling(Math.Max(0, totalRounds - completedRounds)); + if (completedRounds >= totalRounds) + { + targetRoundsReached = true; + FriabilityRemainingRounds = 0; break; - - // 计算当前剩余圈数 - double elapsedMs = i; - double elapsedRounds = rpm / 60 / 1000 * elapsedMs; - int remainingRounds = (int)Math.Ceiling(totalRounds - elapsedRounds); - - // 更新界面绑定的剩余圈数 - FriabilityRemainingRounds = remainingRounds; - if (i % 500 == 0) - await RefreshFriabilityRealtimeRoundsAsync(); + } // 等待100ms,再更新下一次 await Task.Delay(100); } - if (!_isFriabilityRunning) + if (!_isFriabilityRunning || !targetRoundsReached) throw new InvalidOperationException("脆碎度测试已停止,未保存结果"); + if (_plcConfig.FriabilityStartCoilStop != 0) + await PulseCoilAsync(_plcConfig.FriabilityStartCoilStop); + double weightAfter = await ReadFriabilityWeightAsync(ResolveFriabilityWeightAfterRegister(), "脆碎后重量"); SetFriabilityWeightFromPlc(weightAfter: weightAfter); FriabilityCurrentRpm = rpm; @@ -1982,8 +1981,8 @@ namespace TabletTester2025.ViewModels try { - int value = await _plc.ReadIntAsync(registerAddress); - if (value >= 0) + double value = await _plc.ReadFloatAsync(registerAddress); + if (double.IsFinite(value) && value >= 0) FriabilityRealtimeRounds = value; } catch diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index 84c297c..ae26524 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -494,7 +494,7 @@ - + diff --git a/appsettings.json b/appsettings.json index b80f9a2..dc59634 100644 --- a/appsettings.json +++ b/appsettings.json @@ -29,7 +29,7 @@ "FriabilityRpm": 320, // 脆碎度转速 r/min "FriabilityRounds": 410, // 脆碎圈数 "FriabilityRoundsBox": 410, // 兼容旧字段:脆碎圈数 - "FriabilityRealtimeRounds": 82, // 脆碎实时圈数 + "FriabilityRealtimeRounds": 82, // 脆碎实时圈数,32位浮点 "DisintegrationSeconds": 420, //崩解时间