diff --git a/ViewModels/StationViewModel.cs b/ViewModels/StationViewModel.cs index 80b9d74..cc9a466 100644 --- a/ViewModels/StationViewModel.cs +++ b/ViewModels/StationViewModel.cs @@ -127,7 +127,9 @@ namespace TabletTester2025.ViewModels StartHardnessCommand = new AsyncRelayCommand(RunHardnessAsync); StartFriabilityCommand = new AsyncRelayCommand(RunFriabilityAsync); StartDisintegrationCommand = new AsyncRelayCommand(RunDisintegrationAsync); + StartDissolutionCommand = new AsyncRelayCommand(RunDissolutionAsync); + ExportHistoryCommand = new AsyncRelayCommand(ExportHistoryAsync); // 溶出曲线 @@ -167,13 +169,16 @@ namespace TabletTester2025.ViewModels PrintHardnessCommand = new AsyncRelayCommand(async () => await PrintReport("硬度")); // 脆碎度命令 - StopFriabilityCommand = new AsyncRelayCommand(() => { Phase = TestPhase.Idle; return Task.CompletedTask; }); + StopFriabilityCommand = new AsyncRelayCommand(() => { + //测试停止 + Phase = TestPhase.Idle; return Task.CompletedTask; + }); ResetFriabilityCommand = new AsyncRelayCommand(() => { - FriabilityRemainingRounds = 100; - LossPercent = 0; - WeightBefore = 0; - WeightAfter = 0; + FriabilityRemainingRounds = 100; // 剩余圈数重置为默认值(通常是100圈) + LossPercent = 0; // 失重率清零 + WeightBefore = 0; // 脆碎前重量清零 + WeightAfter = 0;// 脆碎后重量清零 return Task.CompletedTask; }); PrintFriabilityCommand = new AsyncRelayCommand(async () => await PrintReport("脆碎度")); @@ -251,30 +256,34 @@ namespace TabletTester2025.ViewModels double max = App.CurrentPharmaParams.HardnessMax_N; for (int i = 0; i < count; i++) - { + + { // 1. 给PLC发指令:启动单次硬度测试 await _plc.WriteCoilAsync((ushort)(StationId == 1 ? _plcConfig.HardnessStartCoil : StationId == 2 ? _plcConfig.HardnessStartCoil2 : StationId == 3 ? _plcConfig.HardnessStartCoil3 : 0), true); bool completed = false; while (!completed && Phase == TestPhase.Running) { - await Task.Delay(200); + await Task.Delay(200);// 每200ms轮询一次状态 + completed = await _plc.ReadCoilAsync(_plcConfig.HardnessCompleteCoil); } double val = await _plc.ReadFloatAsync(_plcConfig.HardnessValue); - _hardnessResults.Add(val); - HardnessValue = val; - await Task.Delay(1000); + _hardnessResults.Add(val); // 把结果存到列表里 + + HardnessValue = val; // 更新界面上的实时测试力值 + + await Task.Delay(1000); // 间隔1秒,再进行下一次测试 } HardnessAvg = _hardnessResults.Average(); HardnessMax= _hardnessResults.Max(); HardnessMin = _hardnessResults.Min(); - + HardnessCurrentCount = count; HardnessRSD = (StandardDeviation(_hardnessResults) / HardnessAvg) * 100; HardnessPass = HardnessAvg >= min && HardnessAvg <= max; - Phase = TestPhase.Completed; + Phase = TestPhase.Completed;//恢复启动按钮 } catch (Exception ex) { @@ -293,38 +302,75 @@ namespace TabletTester2025.ViewModels } } + /// 脆碎度测试主逻辑(适配3工位、实时状态显示) + private async Task RunFriabilityAsync() { - if (Phase != TestPhase.Idle) return; + // 1. 防并发:如果设备不是空闲状态,直接退出 + if (Phase != TestPhase.Idle) + return; + + // 2. 标记当前正在运行的是脆碎度测试 CurrentTest = TestType.Friability; Phase = TestPhase.Running; - FriabilityPass = false; // 添加这一行 + FriabilityPass = false; + try { - // 通过天平读取前重 + ushort startCoil = StationId switch + { + 1 => _plcConfig.FriabilityStartCoil, // 工位1启动线圈 + 2 => _plcConfig.FriabilityStartCoil2, // 工位2启动线圈 + 3 => _plcConfig.FriabilityStartCoil3, // 工位3启动线圈 + _ => 0 + }; + + if (startCoil == 0) + { + throw new InvalidOperationException("当前工位未配置脆碎度启动线圈地址"); + } WeightBefore = await _balance.ReadWeightAsync(); - await _plc.WriteCoilAsync(_plcConfig.FriabilityStartCoil, true); + await _plc.WriteCoilAsync(startCoil, true); + int totalRounds = 100; // 药典标准脆碎度总圈数:100圈 + double rpm = FriabilityTargetRpm; // 界面设置的目标转速(r/min) + int durationMs = (int)((totalRounds / rpm) * 60 * 1000); // 总运行时间(毫秒) - // 根据设定的转速和总圈数计算运行时间 - int totalRounds = 100; - double rpm = FriabilityTargetRpm; - int durationMs = (int)((totalRounds / rpm) * 60 * 1000); - await Task.Delay(durationMs); + for (int i = 0; i < durationMs; i += 100) + { + // 如果用户点了停止,状态会被设为Idle,直接跳出循环 + if (Phase != TestPhase.Running) + break; + // 计算当前剩余圈数 + double elapsedMs = i; + double elapsedRounds = rpm / 60 / 1000 * elapsedMs; + int remainingRounds = (int)Math.Ceiling(totalRounds - elapsedRounds); + + // 更新界面绑定的剩余圈数 + FriabilityRemainingRounds = remainingRounds; + + // 等待100ms,再更新下一次 + await Task.Delay(100); + } WeightAfter = await _balance.ReadWeightAsync(); - LossPercent = (WeightBefore - WeightAfter) / WeightBefore * 100; - FriabilityPass = LossPercent <= App.CurrentPharmaParams.FriabilityMaxLossPercent; + + FriabilityCurrentRpm = FriabilityTargetRpm; + LossPercent = (WeightBefore - WeightAfter) / WeightBefore * 100;//失重率 + FriabilityPass = LossPercent <= App.CurrentPharmaParams.FriabilityMaxLossPercent; //标准值 + // 标记测试为已完成 Phase = TestPhase.Completed; } catch (Exception ex) { - await App.Current.Dispatcher.InvokeAsync(() => MessageBox.Show($"脆碎度测试出错: {ex.Message}")); + + await App.Current.Dispatcher.InvokeAsync(() => + MessageBox.Show($"脆碎度测试出错: {ex.Message}")); Phase = TestPhase.Error; } finally { Phase = TestPhase.Idle; - + FriabilityRemainingRounds = 100; await SaveBatchResult(); } } diff --git a/Views/MainWindow.xaml b/Views/MainWindow.xaml index b77bb3a..8fbcff1 100644 --- a/Views/MainWindow.xaml +++ b/Views/MainWindow.xaml @@ -128,6 +128,7 @@