更新1222

This commit is contained in:
GukSang.Jin
2026-06-09 14:24:49 +08:00
parent 07a652c65c
commit d9fe314e84

View File

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