From aa6d93037431276d2a9368282ebf0114b0c2433d Mon Sep 17 00:00:00 2001 From: Tyrone CT Date: Fri, 27 Mar 2026 17:41:55 +0800 Subject: [PATCH] =?UTF-8?q?=E7=8E=B0=E5=9C=BA=E7=9A=84=E9=9C=80=E6=B1=82?= =?UTF-8?q?=E7=9A=84=E6=9B=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CapMachine.Wpf/Services/PPCService.cs | 130 +++++++++++++++++++++++--- CapMachine.Wpf/Views/MainView.xaml.cs | 112 +++++++++++++++++++++- 2 files changed, 227 insertions(+), 15 deletions(-) diff --git a/CapMachine.Wpf/Services/PPCService.cs b/CapMachine.Wpf/Services/PPCService.cs index 2f2ef55..9b03597 100644 --- a/CapMachine.Wpf/Services/PPCService.cs +++ b/CapMachine.Wpf/Services/PPCService.cs @@ -338,7 +338,7 @@ namespace CapMachine.Wpf.Services } else if (_superheatSubcoolCalculator.TryCalculateSuperheatK(InhPressTag.EngPvValue, InhTempTag.EngPvValue, out var superheatValue, out var superheatErr)) { - Superheat.EngPvValue =Math.Abs(superheatValue) ; + Superheat.EngPvValue = Math.Abs(superheatValue); updated = true; } else @@ -651,11 +651,11 @@ namespace CapMachine.Wpf.Services COPCooling = result.COPCooling; IsentropicEfficiencyPct = result.IsentropicEfficiencyPct; - // 再把结果回写到对应工程量标签。 - HeatingCapacity.EngPvValue = HeatingCapacityQh_kW; + // 再把结果回写到对应工程量标签 + HeatingCapacity.EngPvValue = HeatingCapacityQh_kW * 1000; COPHeat.EngPvValue = COPHeating; IsentrpEff.EngPvValue = IsentropicEfficiencyPct; - CoolCapacity.EngPvValue = CoolingCapacityQc_kW; + CoolCapacity.EngPvValue = CoolingCapacityQc_kW * 1000; COPCool.EngPvValue = COPCooling; // 容积效率在旧逻辑中允许单独失败; @@ -756,21 +756,87 @@ namespace CapMachine.Wpf.Services { displacement_cc = double.NaN; error = string.Empty; + const double defaultDisplacementCc = 35d; - displacement_cc = 35; + if (TryGetCompressorDisplacementFromCurrentExperiment(out displacement_cc, out var experimentError)) + { + return true; + } + + if (TryGetCompressorDisplacementFromConfig(out displacement_cc, out var configError)) + { + return true; + } + + displacement_cc = defaultDisplacementCc; return true; + } + + /// + /// 从当前选中的实验信息中读取并解析压缩机排量。 + /// + /// 排量输出,单位 cc。 + /// 失败原因。 + /// 是否读取成功。 + private bool TryGetCompressorDisplacementFromCurrentExperiment(out double displacement_cc, out string error) + { + displacement_cc = double.NaN; + error = string.Empty; + + if (ConfigService.CurExpInfo == null) + { + error = "当前未选择实验信息,无法从试验信息读取压缩机排量"; + return false; + } + + var displacementText = ConfigService.CurExpInfo.CapDisplacement; + if (string.IsNullOrWhiteSpace(displacementText)) + { + error = "当前试验信息未填写压缩机排量"; + return false; + } + + if (!TryParseCompressorDisplacementText(displacementText, out displacement_cc)) + { + error = $"当前试验信息压缩机排量无法解析: {displacementText}"; + return false; + } + + if (displacement_cc <= 0) + { + error = $"当前试验信息压缩机排量<=0: {displacement_cc}"; + return false; + } + + return true; + } + + /// + /// 从配置项读取并解析压缩机排量。 + /// + /// 排量输出,单位 cc。 + /// 失败原因。 + /// 是否读取成功。 + private bool TryGetCompressorDisplacementFromConfig(out double displacement_cc, out string error) + { + displacement_cc = double.NaN; + error = string.Empty; const string key = "CompressorDisplacementCc"; - //if (!ConfigHelper.IsExist(key)) - //{ - // error = $"未配置压缩机排量,请在 App.config/appSettings 增加 {key}(单位 cc)"; - // return false; - //} - - string v = ConfigHelper.GetValue(key); - if (!double.TryParse(v, out displacement_cc)) + string configValue; + try { - error = $"压缩机排量配置无法解析: {v}"; + configValue = ConfigHelper.GetValue(key); + } + catch + { + error = $"未配置压缩机排量,请在 App.config/appSettings 增加 {key}(单位 cc)"; + return false; + } + + if (!TryParseCompressorDisplacementText(configValue, out displacement_cc)) + { + error = $"压缩机排量配置无法解析: {configValue}"; return false; } @@ -783,6 +849,42 @@ namespace CapMachine.Wpf.Services return true; } + /// + /// 解析排量文本为数值。 + /// 支持纯数字与包含单位的文本(如 34.5、34.5cc、34,5 cm3)。 + /// + /// 排量文本。 + /// 解析后的排量数值,单位 cc。 + /// 是否解析成功。 + private static bool TryParseCompressorDisplacementText(string displacementText, out double displacementCc) + { + displacementCc = double.NaN; + if (string.IsNullOrWhiteSpace(displacementText)) + { + return false; + } + + var normalizedText = displacementText.Trim(); + if (double.TryParse(normalizedText, out displacementCc)) + { + return true; + } + + if (double.TryParse(normalizedText, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out displacementCc)) + { + return true; + } + + var match = System.Text.RegularExpressions.Regex.Match(normalizedText, @"[-+]?\d+(?:[\.,]\d+)?"); + if (!match.Success) + { + return false; + } + + var numericText = match.Value.Replace(',', '.'); + return double.TryParse(numericText, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out displacementCc); + } + #endregion } } diff --git a/CapMachine.Wpf/Views/MainView.xaml.cs b/CapMachine.Wpf/Views/MainView.xaml.cs index dd4dc56..be14252 100644 --- a/CapMachine.Wpf/Views/MainView.xaml.cs +++ b/CapMachine.Wpf/Views/MainView.xaml.cs @@ -1,4 +1,5 @@ using CapMachine.Wpf.Services; +using System; using System.Windows; namespace CapMachine.Wpf.Views @@ -8,10 +9,19 @@ namespace CapMachine.Wpf.Views /// public partial class MainView : Window { - public MainView(ILogService logService) + private readonly ZlgCanDriveService _zlgCanDriveService; + private readonly ZlgLinDriveService _zlgLinDriveService; + private readonly CanDriveService _canDriveService; + private readonly LinDriveService _linDriveService; + + public MainView(ILogService logService, ZlgCanDriveService zlgCanDriveService, ZlgLinDriveService zlgLinDriveService, CanDriveService canDriveService, LinDriveService linDriveService) { InitializeComponent(); LogService = logService; + _zlgCanDriveService = zlgCanDriveService; + _zlgLinDriveService = zlgLinDriveService; + _canDriveService = canDriveService; + _linDriveService = linDriveService; } public ILogService LogService { get; } @@ -42,10 +52,110 @@ namespace CapMachine.Wpf.Views } else { + StopBusCommunicationOnExit(); LogService.Info("Windows关闭"); } } + /// + /// 软件退出时停止总线通信,防止适配器在进程退出后继续自动发送。 + /// + private void StopBusCommunicationOnExit() + { + StopZlgBusCommunicationOnExit(); + StopToomossBusCommunicationOnExit(); + } + + /// + /// 停止 ZLG CAN/CANFD 与 LIN 通信。 + /// + private void StopZlgBusCommunicationOnExit() + { + try + { + if (_zlgLinDriveService.OpenState) + { + if (_zlgLinDriveService.IsCycleSend || _zlgLinDriveService.SchEnable) + { + _zlgLinDriveService.StopSchedule(); + _zlgLinDriveService.IsCycleSend = false; + } + + _zlgLinDriveService.CloseDevice(); + LogService.Info("程序退出时已停止 ZLG LIN 调度/发送并关闭设备"); + return; + } + + if (_zlgCanDriveService.OpenState) + { + if (_zlgCanDriveService.IsCycleSend || _zlgCanDriveService.SchEnable) + { + _zlgCanDriveService.StopSchedule(); + _zlgCanDriveService.IsCycleSend = false; + } + + _zlgCanDriveService.CloseDevice(); + LogService.Info("程序退出时已停止 ZLG CAN/CANFD 调度/发送并关闭设备"); + } + } + catch (Exception ex) + { + LogService.Error($"程序退出停止 ZLG 总线失败:{ex.Message}"); + } + } + + /// + /// 停止 Toomoss CAN 与 LIN 通信。 + /// + private void StopToomossBusCommunicationOnExit() + { + try + { + if (_linDriveService.ToomossLinDrive.OpenState) + { + if (_linDriveService.ToomossLinDrive.IsCycleSend) + { + _linDriveService.ToomossLinDrive.StopCycleSendMsg(); + } + + if (_linDriveService.ToomossLinDrive.SchEnable) + { + _linDriveService.ToomossLinDrive.StopSchedule(); + } + + _linDriveService.ToomossLinDrive.CloseDevice(); + LogService.Info("程序退出时已停止 Toomoss LIN 调度/发送并关闭设备"); + } + } + catch (Exception ex) + { + LogService.Error($"程序退出停止 Toomoss LIN 失败:{ex.Message}"); + } + + try + { + if (_canDriveService.ToomossCanDrive.OpenState) + { + if (_canDriveService.ToomossCanDrive.IsCycleSend) + { + _canDriveService.ToomossCanDrive.StopCycleSendMsg(); + } + + if (_canDriveService.ToomossCanDrive.SchEnable) + { + _canDriveService.ToomossCanDrive.StopSchedule(); + } + + _canDriveService.ToomossCanDrive.CloseDevice(); + LogService.Info("程序退出时已停止 Toomoss CAN 调度/发送并关闭设备"); + } + } + catch (Exception ex) + { + LogService.Error($"程序退出停止 Toomoss CAN 失败:{ex.Message}"); + } + } + /// /// 主界面完成关闭 ///