更新20260529

This commit is contained in:
GukSang.Jin
2026-05-29 15:38:07 +08:00
parent 737ef1643e
commit 57b5b09a05
7 changed files with 19 additions and 5 deletions

View File

@@ -12,6 +12,7 @@ public sealed class ModbusRtuScaleService : IScaleService
private const int ExpectedDataByteCount = 4;
private const double MinimumMass = 0;
private const double MaximumMass = 100000;
private const ushort CdabFloatRegisterStart = 0x200;
private static readonly TimeSpan ReadWriteTimeout = TimeSpan.FromMilliseconds(1000);
private readonly object _syncRoot = new();
@@ -42,7 +43,7 @@ public sealed class ModbusRtuScaleService : IScaleService
WriteReadRequest(port);
var response = ReadResponse(port);
if (!TryDecodeCurrentMass(response, _options.UnitId, out value))
if (!TryDecodeCurrentMass(response, _options.UnitId, GetFloatByteOrder(), out value))
{
return false;
}
@@ -70,6 +71,13 @@ public sealed class ModbusRtuScaleService : IScaleService
}
}
private ModbusFloatByteOrder GetFloatByteOrder()
{
return _options.RegisterAddress >= CdabFloatRegisterStart
? ModbusFloatByteOrder.Cdab
: ModbusFloatByteOrder.Abcd;
}
public void Dispose()
{
lock (_syncRoot)
@@ -194,7 +202,11 @@ public sealed class ModbusRtuScaleService : IScaleService
}
}
private static bool TryDecodeCurrentMass(byte[] response, byte unitId, out double value)
private static bool TryDecodeCurrentMass(
byte[] response,
byte unitId,
ModbusFloatByteOrder byteOrder,
out double value)
{
value = double.NaN;
@@ -207,15 +219,17 @@ public sealed class ModbusRtuScaleService : IScaleService
}
var result = new ModbusFloatReadResult(response[3], response[4], response[5], response[6]);
var candidate = result.Abcd;
var candidate = result.GetValue(byteOrder);
if (!ModbusFloatSelector.IsInRange(candidate, MinimumMass, MaximumMass))
{
Debug.WriteLine(
$"Scale mass out of range raw [{result.RawHex}], ABCD={result.Abcd:G9}, "
$"Scale mass out of range raw [{result.RawHex}], selected={byteOrder}, "
+ $"ABCD={result.Abcd:G9}, "
+ $"CDAB={result.Cdab:G9}, BADC={result.Badc:G9}, DCBA={result.Dcba:G9}.");
Log.Warning(
"Scale mass out of range. Raw={RawHex}, ABCD={Abcd}, CDAB={Cdab}, BADC={Badc}, DCBA={Dcba}, Range={MinimumMass}..{MaximumMass}.",
"Scale mass out of range. Raw={RawHex}, SelectedByteOrder={SelectedByteOrder}, ABCD={Abcd}, CDAB={Cdab}, BADC={Badc}, DCBA={Dcba}, Range={MinimumMass}..{MaximumMass}.",
result.RawHex,
byteOrder,
result.Abcd,
result.Cdab,
result.Badc,

Binary file not shown.

After

Width:  |  Height:  |  Size: 255 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 250 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 210 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 174 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 330 KiB