diff --git a/ConeCalorimeter/Services/ModbusRtuScaleService.cs b/ConeCalorimeter/Services/ModbusRtuScaleService.cs index 5e09758..8c31ed1 100644 --- a/ConeCalorimeter/Services/ModbusRtuScaleService.cs +++ b/ConeCalorimeter/Services/ModbusRtuScaleService.cs @@ -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, diff --git a/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_02.png b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_02.png new file mode 100644 index 0000000..d73b4ad Binary files /dev/null and b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_02.png differ diff --git a/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_04.png b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_04.png new file mode 100644 index 0000000..8fc56e3 Binary files /dev/null and b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_04.png differ diff --git a/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_08.png b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_08.png new file mode 100644 index 0000000..2fbe4d2 Binary files /dev/null and b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_08.png differ diff --git a/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_09.png b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_09.png new file mode 100644 index 0000000..b855e0e Binary files /dev/null and b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_09.png differ diff --git a/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_11.png b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_11.png new file mode 100644 index 0000000..136419e Binary files /dev/null and b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_11.png differ diff --git a/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_12.png b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_12.png new file mode 100644 index 0000000..6bc1132 Binary files /dev/null and b/ConeCalorimeter/tmp/pdfs/mh_awc_pages/page_12.png differ