diff --git a/DentistryHandpieces/MainWindowViewModel.cs b/DentistryHandpieces/MainWindowViewModel.cs index 43d2e61..4e247af 100644 --- a/DentistryHandpieces/MainWindowViewModel.cs +++ b/DentistryHandpieces/MainWindowViewModel.cs @@ -1838,14 +1838,14 @@ public sealed class MainWindowViewModel : ObservableObject await _testPageInputWriteLock.WaitAsync(); try { - if (!TryReadParameterConfig(out TestParameterConfig config, out string error)) + if (!TryReadParameterPageConfig(out TestParameterConfig config, out string error)) { ParameterStatusText = error; Log.Warning("参数保存被阻止:{ValidationError}", error); return; } - IReadOnlyList changedPlcItems = await WriteChangedParameterConfigToPlcAsync(_parameterConfig, config); + IReadOnlyList changedPlcItems = await WriteChangedParameterPageToPlcAsync(_parameterConfig, config); bool communicationConfigChanged = HasCommunicationConfigChanged(_parameterConfig, config); _parameterConfig = config; SaveParameterConfig(); @@ -1859,19 +1859,19 @@ public sealed class MainWindowViewModel : ObservableObject if (changedPlcItems.Count > 0) { - ParameterStatusText = $"保存成功,仅写入 {changedPlcItems.Count} 项:{string.Join("、", changedPlcItems)}"; + ParameterStatusText = $"当前页保存成功,仅写入 {changedPlcItems.Count} 项:{string.Join("、", changedPlcItems)}"; } else if (communicationConfigChanged) { - ParameterStatusText = "保存成功,通信配置已更新,无需写入 PLC 参数。"; + ParameterStatusText = "当前页保存成功,通信配置已更新,无需写入 PLC 参数。"; } else { - ParameterStatusText = "参数未修改,无需写入 PLC。"; + ParameterStatusText = "当前页参数未修改,无需写入 PLC。"; } Log.Information( - "参数配置差异保存成功,PLC写入 {WriteCount} 项:{ChangedItems},通信配置修改 {CommunicationConfigChanged},地址 {IpAddress}:{Port},站号 {UnitId}", + "参数配置当前页差异保存成功,PLC写入 {WriteCount} 项:{ChangedItems},通信配置修改 {CommunicationConfigChanged},地址 {IpAddress}:{Port},站号 {UnitId}", changedPlcItems.Count, changedPlcItems.Count > 0 ? string.Join("、", changedPlcItems) : "无", communicationConfigChanged, @@ -1890,7 +1890,7 @@ public sealed class MainWindowViewModel : ObservableObject } } - private async Task> WriteChangedParameterConfigToPlcAsync(TestParameterConfig current, TestParameterConfig updated) + private async Task> WriteChangedParameterPageToPlcAsync(TestParameterConfig current, TestParameterConfig updated) { PlcConnectionConfig plcConfig = updated.ToPlcConnectionConfig(); var changedItems = new List(); @@ -1898,21 +1898,15 @@ public sealed class MainWindowViewModel : ObservableObject await WriteChangedFloatAsync(AxialDisplacementLimitRegister, "轴向位移极限", current.AxialDisplacementLimit, updated.AxialDisplacementLimit); await WriteChangedFloatAsync(AxialSpeedRegister, "轴向手/自动速度", current.AxialSpeed, updated.AxialSpeed); await WriteChangedFloatAsync(AxialManualDisplacementRegister, "轴向手动位移", current.AxialManualDisplacement, updated.AxialManualDisplacement); - await WriteChangedCoilAsync(AxialForceModeCoil, "轴向力模式", current.UseAxialPullForceSetpoint, updated.UseAxialPullForceSetpoint); - await WriteChangedFloatAsync(AxialPullForceSetpointRegister, "轴向拉力设置", current.AxialForceSetpoint, updated.AxialForceSetpoint); - await WriteChangedFloatAsync(AxialJumpForceSetpointRegister, "轴向跳动力设置", current.AxialJumpForceSetpoint, updated.AxialJumpForceSetpoint); await WriteChangedFloatAsync(AxialForceLowerLimitRegister, "轴向力下限", current.AxialForceLowerLimit, updated.AxialForceLowerLimit); await WriteChangedFloatAsync(AxialForceUpperLimitRegister, "轴向力上限", current.AxialForceUpperLimit, updated.AxialForceUpperLimit); await WriteChangedFloatAsync(AxialForceCoefficientRegister, "轴向力系数", current.AxialForceCoefficient, updated.AxialForceCoefficient); await WriteChangedFloatAsync(AxialForceProtectionRegister, "轴向力保护", current.AxialForceProtection, updated.AxialForceProtection); - await WriteChangedTenthsAsync(AxialForceHoldTimeRegister, "轴向力保持时间", current.AxialForceHoldTime, updated.AxialForceHoldTime); await WriteChangedFloatAsync(SpeedTorqueDisplacementLimitRegister, "转速/扭矩位移极限", current.SpeedTorqueDisplacementLimit, updated.SpeedTorqueDisplacementLimit); await WriteChangedFloatAsync(SpeedTorqueSpeedRegister, "转速/扭矩手/自动速度", current.SpeedTorqueSpeed, updated.SpeedTorqueSpeed); await WriteChangedFloatAsync(SpeedTorqueManualDisplacementRegister, "转速/扭矩手动位移", current.SpeedTorqueManualDisplacement, updated.SpeedTorqueManualDisplacement); await WriteChangedFloatAsync(TorqueCoefficientRegister, "扭矩系数", current.TorqueCoefficient, updated.TorqueCoefficient); await WriteChangedFloatAsync(TorqueProtectionRegister, "扭矩保护", current.TorqueProtection, updated.TorqueProtection); - await WriteChangedTenthsAsync(HoldTorqueRegister, "保持扭矩", current.HoldTorque, updated.HoldTorque); - await WriteChangedTenthsAsync(TorqueHoldTimeRegister, "扭矩保持时间", current.TorqueHoldTime, updated.TorqueHoldTime); await WriteChangedFloatAsync(SpeedCoefficientRegister, "转速系数", current.SpeedCoefficient, updated.SpeedCoefficient); await WriteChangedFloatAsync(SpeedStopThresholdRegister, "低速停止", current.SpeedStopThreshold, updated.SpeedStopThreshold); await WriteChangedFloatAsync(PressureCoefficientRegister, "压力系数", current.PressureCoefficient, updated.PressureCoefficient); @@ -1930,32 +1924,9 @@ public sealed class MainWindowViewModel : ObservableObject changedItems.Add($"{fieldName}(D{registerAddress})"); } - async Task WriteChangedTenthsAsync(ushort registerAddress, string fieldName, double oldValue, double newValue) - { - ushort oldRawValue = ScaleTenthsToPlc(oldValue, fieldName); - ushort newRawValue = ScaleTenthsToPlc(newValue, fieldName); - if (oldRawValue == newRawValue) - { - return; - } - - await _plcRegisterService.WriteUInt16Async(plcConfig, registerAddress, newRawValue); - changedItems.Add($"{fieldName}(D{registerAddress})"); - } - - async Task WriteChangedCoilAsync(ushort coilAddress, string fieldName, bool oldValue, bool newValue) - { - if (oldValue == newValue) - { - return; - } - - await _plcCoilService.WriteCoilAsync(plcConfig, coilAddress, newValue); - changedItems.Add($"{fieldName}(M{coilAddress})"); - } } - private bool TryReadParameterConfig(out TestParameterConfig config, out string error) + private bool TryReadParameterPageConfig(out TestParameterConfig config, out string error) { config = TestParameterConfig.CreateDefault(); error = string.Empty; @@ -1966,16 +1937,12 @@ public sealed class MainWindowViewModel : ObservableObject || !TryReadNonNegative(AxialForceLowerLimitInput, "轴向力下限", out double axialForceLowerLimit, out error) || !TryReadNonNegative(AxialForceUpperLimitInput, "轴向力上限", out double axialForceUpperLimit, out error) || !TryReadPositive(AxialForceCoefficientInput, "轴向力系数", out double axialForceCoefficient, out error) - || !TryReadNonNegative(AxialForceSetpointInput, GetActiveAxialForceSetpointName(), out double axialForceSetpoint, out error) || !TryReadNonNegative(AxialForceProtectionInput, "轴向力保护", out double axialForceProtection, out error) - || !TryReadNonNegative(AxialForceHoldTimeInput, "轴向力保持时间设置", out double axialForceHoldTime, out error) || !TryReadNonNegative(SpeedTorqueDisplacementLimitInput, "转速/扭矩位移极限", out double speedTorqueDisplacementLimit, out error) || !TryReadPositive(SpeedTorqueSpeedInput, "转速/扭矩手/自动速度", out double speedTorqueSpeed, out error) || !TryReadNonNegative(SpeedTorqueManualDisplacementInput, "转速/扭矩手动位移", out double speedTorqueManualDisplacement, out error) || !TryReadPositive(TorqueCoefficientInput, "扭矩系数", out double torqueCoefficient, out error) || !TryReadNonNegative(TorqueProtectionInput, "扭矩保护", out double torqueProtection, out error) - || !TryReadNonNegative(HoldTorqueInput, "保持扭矩设置", out double holdTorque, out error) - || !TryReadNonNegative(TorqueHoldTimeInput, "扭矩保持时间设置", out double torqueHoldTime, out error) || !TryReadPositive(SpeedCoefficientInput, "转速系数", out double speedCoefficient, out error) || !TryReadNonNegative(SpeedStopThresholdInput, "低速停止设置", out double speedStopThreshold, out error) || !TryReadPositive(PressureCoefficientInput, "压力系数", out double pressureCoefficient, out error) @@ -1993,13 +1960,6 @@ public sealed class MainWindowViewModel : ObservableObject return false; } - if (!CanScaleTenthsToPlc(axialForceHoldTime, "轴向力保持时间设置", out error) - || !CanScaleTenthsToPlc(holdTorque, "保持扭矩设置", out error) - || !CanScaleTenthsToPlc(torqueHoldTime, "扭矩保持时间设置", out error)) - { - return false; - } - if (axialForceUpperLimit > 0 && axialForceLowerLimit > axialForceUpperLimit) { error = "轴向力下限不能大于上限。"; @@ -2014,18 +1974,18 @@ public sealed class MainWindowViewModel : ObservableObject AxialForceLowerLimit = axialForceLowerLimit, AxialForceUpperLimit = axialForceUpperLimit, AxialForceCoefficient = axialForceCoefficient, - AxialForceSetpoint = _parameterConfig.UseAxialPullForceSetpoint ? axialForceSetpoint : _parameterConfig.AxialForceSetpoint, - AxialJumpForceSetpoint = _parameterConfig.UseAxialPullForceSetpoint ? _parameterConfig.AxialJumpForceSetpoint : axialForceSetpoint, + AxialForceSetpoint = _parameterConfig.AxialForceSetpoint, + AxialJumpForceSetpoint = _parameterConfig.AxialJumpForceSetpoint, UseAxialPullForceSetpoint = _parameterConfig.UseAxialPullForceSetpoint, AxialForceProtection = axialForceProtection, - AxialForceHoldTime = axialForceHoldTime, + AxialForceHoldTime = _parameterConfig.AxialForceHoldTime, SpeedTorqueDisplacementLimit = speedTorqueDisplacementLimit, SpeedTorqueSpeed = speedTorqueSpeed, SpeedTorqueManualDisplacement = speedTorqueManualDisplacement, TorqueCoefficient = torqueCoefficient, TorqueProtection = torqueProtection, - HoldTorque = holdTorque, - TorqueHoldTime = torqueHoldTime, + HoldTorque = _parameterConfig.HoldTorque, + TorqueHoldTime = _parameterConfig.TorqueHoldTime, SpeedCoefficient = speedCoefficient, SpeedStopThreshold = speedStopThreshold, PressureCoefficient = pressureCoefficient,