更新20260519

This commit is contained in:
GukSang.Jin
2026-05-19 16:55:00 +08:00
parent 57ccef3f5b
commit b80edaea78
3 changed files with 39 additions and 23 deletions

View File

@@ -95,7 +95,7 @@ namespace TabletTester2025.Services
public async Task<float> ReadFloatAsync(ushort startAddress) public async Task<float> ReadFloatAsync(ushort startAddress)
{ {
var registers = await ReadHoldingRegistersAsync(startAddress, 2); var registers = await ReadHoldingRegistersAsync(startAddress, 2);
return UshortToFloat(registers[1], registers[0]); return RegistersToFloat(registers[0], registers[1]);
} }
public async Task<int> ReadIntAsync(ushort startAddress) public async Task<int> ReadIntAsync(ushort startAddress)
@@ -122,13 +122,10 @@ namespace TabletTester2025.Services
public Task WriteFloatAsync(ushort startAddress, float value) public Task WriteFloatAsync(ushort startAddress, float value)
{ {
byte[] bytes = BitConverter.GetBytes(value); if (!float.IsFinite(value))
ushort[] registers = throw new ArgumentOutOfRangeException(nameof(value), "PLC浮点写入值不能是NaN或Infinity。");
{
(ushort)((bytes[2] << 8) | bytes[3]),
(ushort)((bytes[0] << 8) | bytes[1])
};
ushort[] registers = FloatToRegisters(value);
return ExecuteAsync(master => master.WriteMultipleRegistersAsync(_config.SlaveId, startAddress, registers)); return ExecuteAsync(master => master.WriteMultipleRegistersAsync(_config.SlaveId, startAddress, registers));
} }
@@ -168,16 +165,28 @@ namespace TabletTester2025.Services
} }
} }
private static float UshortToFloat(ushort high, ushort low) private static float RegistersToFloat(ushort highWord, ushort lowWord)
{ {
byte[] bytes = new byte[4]; byte[] bytes =
bytes[0] = (byte)(high >> 8); {
bytes[1] = (byte)(high & 0xFF); (byte)(lowWord & 0xFF),
bytes[2] = (byte)(low >> 8); (byte)(lowWord >> 8),
bytes[3] = (byte)(low & 0xFF); (byte)(highWord & 0xFF),
(byte)(highWord >> 8)
};
return BitConverter.ToSingle(bytes, 0); return BitConverter.ToSingle(bytes, 0);
} }
private static ushort[] FloatToRegisters(float value)
{
byte[] bytes = BitConverter.GetBytes(value);
return new[]
{
(ushort)((bytes[3] << 8) | bytes[2]),
(ushort)((bytes[1] << 8) | bytes[0])
};
}
private void CloseConnection() private void CloseConnection()
{ {
try { _master?.Dispose(); } catch { } try { _master?.Dispose(); } catch { }

View File

@@ -1039,8 +1039,8 @@ namespace TabletTester2025.ViewModels
try try
{ {
_isLoadingFriabilityTime = true; _isLoadingFriabilityTime = true;
float value = await _plc.ReadFloatAsync(registerAddress); int value = await _plc.ReadIntAsync(registerAddress);
if (float.IsFinite(value) && value > 0) if (value > 0)
{ {
FriabilityTargetTimeMin = value; FriabilityTargetTimeMin = value;
} }
@@ -1108,7 +1108,7 @@ namespace TabletTester2025.ViewModels
try try
{ {
await _plc.WriteFloatAsync(registerAddress, (float)value); await _plc.WriteRegisterAsync(registerAddress, ToPlcTimeRegisterValue(value));
} }
catch { } catch { }
} }
@@ -1233,16 +1233,24 @@ namespace TabletTester2025.ViewModels
private async Task WriteDisintegrationTimeAsync(double value) private async Task WriteDisintegrationTimeAsync(double value)
{ {
if (_plcConfig.DisintegrationTime == 0 || value <= 0) if (_plcConfig.DisintegrationTime == 0 || value <= 0 || !double.IsFinite(value))
return; return;
try try
{ {
await _plc.WriteFloatAsync(_plcConfig.DisintegrationTime, (float)value); await _plc.WriteRegisterAsync(_plcConfig.DisintegrationTime, ToPlcTimeRegisterValue(value));
} }
catch { } catch { }
} }
private static ushort ToPlcTimeRegisterValue(double value)
{
return (ushort)Math.Clamp(
(int)Math.Round(value, MidpointRounding.AwayFromZero),
0,
ushort.MaxValue);
}
private async Task WriteDisintegrationSpeedAsync(double value) private async Task WriteDisintegrationSpeedAsync(double value)
{ {
if (_plcConfig.DisintegrationSpeed == 0 || value <= 0) if (_plcConfig.DisintegrationSpeed == 0 || value <= 0)

View File

@@ -60,8 +60,7 @@ namespace 片剂四用仪.Views
else else
{ {
float value = await _plc.ReadFloatAsync((ushort)m.Address); float value = await _plc.ReadFloatAsync((ushort)m.Address);
Dispatcher.Invoke(() => tb.Text = float.IsFinite(value) ? value.ToString("0.###") : string.Empty);
Dispatcher.Invoke(() => tb.Text = value.ToString("F2"));
} }
} }
@@ -114,9 +113,9 @@ namespace 片剂四用仪.Views
} }
else else
{ {
if (double.TryParse(textBox.Text, out double val)) if (double.TryParse(textBox.Text, out double val) && double.IsFinite(val))
{ {
textBox.Text = val.ToString("F3"); textBox.Text = val.ToString("0.###");
await _plc.WriteFloatAsync((ushort)mapping.Address, (float)val); await _plc.WriteFloatAsync((ushort)mapping.Address, (float)val);
} }
} }
@@ -139,7 +138,7 @@ namespace 片剂四用仪.Views
new PlcParamMapping("txt_WeightLossRate", 416, PlcParamType.Int), new PlcParamMapping("txt_WeightLossRate", 416, PlcParamType.Int),
new PlcParamMapping("txt_DisintegrationSpeed", 330, PlcParamType.Float), new PlcParamMapping("txt_DisintegrationSpeed", 330, PlcParamType.Float),
new PlcParamMapping("txt_DisintegrationTime", 420, PlcParamType.Float), new PlcParamMapping("txt_DisintegrationTime", 420, PlcParamType.Int),
new PlcParamMapping("txt_DissolutionTime", 430, PlcParamType.Int), new PlcParamMapping("txt_DissolutionTime", 430, PlcParamType.Int),
new PlcParamMapping("txt_Dissolution1SamplingInterval", 432, PlcParamType.Float), new PlcParamMapping("txt_Dissolution1SamplingInterval", 432, PlcParamType.Float),