fix(modbus): multi-write PDUs carry no trailing byte past their byte count
The FC15, FC16, BitInRegister[] RMW FC16 and FC23 builders allocated one byte more than they filled (6 + 1 + n / 10 + 1 + n, data copied at offset 6 / 10), so every multi-write request ended in a stray zero byte beyond the length its byte-count field declares. A strict device or gateway that checks the frame length against the byte count refuses it. Each PDU is now exactly 6 + n (FC15/FC16) or 10 + n (FC23). Tests: ModbusWritePduFramingTests asserts the exact length and byte-count field for FC15 (1/8/9 coils), FC16 (array, Float64 array, scalar multi-register), the BitInRegister RMW FC16 and FC23 (all red before the fix); the parity suite's conformant device now refuses a frame-length mismatch on FC15/FC16/FC23. Live: Driver.Modbus.IntegrationTests against 10.100.0.35:5020 (standard profile) passes, including the FC16 and FC23 multi-register round trips. Refs #741 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
+5
-3
@@ -341,7 +341,8 @@ public sealed class ModbusArrayWriteSupportParityTests
|
||||
/// <summary>
|
||||
/// A spec-conformant Modbus device over the whole 0-65535 address space. It enforces the protocol quantity
|
||||
/// ceilings (FC01/02 <= 2000, FC03/04 <= 125, FC15 <= 1968, FC16 <= 123, FC23 write <= 121 / read <= 125) and the
|
||||
/// FC15 / FC16 byte-count field with exception 03 (Illegal Data Value), and a span past address 65535 with
|
||||
/// FC15 / FC16 / FC23 byte-count field AND the exact frame length it implies (a strict device refuses a trailing
|
||||
/// byte) with exception 03 (Illegal Data Value), and a span past address 65535 with
|
||||
/// exception 02 (Illegal Data Address) - what a real device answers. Everything else it answers.
|
||||
/// </summary>
|
||||
private sealed class ConformantDevice : IModbusTransport
|
||||
@@ -392,13 +393,13 @@ public sealed class ModbusArrayWriteSupportParityTests
|
||||
return Task.FromResult(pdu);
|
||||
case 0x0F:
|
||||
if (Check(addr, qty, 1968) is { } ce) return Refuse(fc, ce);
|
||||
if (pdu[5] != (qty + 7) / 8 || pdu.Length < 6 + pdu[5]) return Refuse(fc, 0x03);
|
||||
if (pdu[5] != (qty + 7) / 8 || pdu.Length != 6 + pdu[5]) return Refuse(fc, 0x03); // strict frame length
|
||||
WritePdus++;
|
||||
for (var i = 0; i < qty; i++) Coils[addr + i] = ((pdu[6 + i / 8] >> (i % 8)) & 1) == 1;
|
||||
return Task.FromResult(new byte[] { 0x0F, pdu[1], pdu[2], pdu[3], pdu[4] });
|
||||
case 0x10:
|
||||
if (Check(addr, qty, 123) is { } we) return Refuse(fc, we);
|
||||
if (pdu[5] != qty * 2 || pdu.Length < 6 + pdu[5]) return Refuse(fc, 0x03);
|
||||
if (pdu[5] != qty * 2 || pdu.Length != 6 + pdu[5]) return Refuse(fc, 0x03); // strict frame length
|
||||
WritePdus++;
|
||||
for (var i = 0; i < qty; i++) Holding[addr + i] = (ushort)((pdu[6 + i * 2] << 8) | pdu[7 + i * 2]);
|
||||
return Task.FromResult(new byte[] { 0x10, pdu[1], pdu[2], pdu[3], pdu[4] });
|
||||
@@ -408,6 +409,7 @@ public sealed class ModbusArrayWriteSupportParityTests
|
||||
var writeQty = (pdu[7] << 8) | pdu[8];
|
||||
if (Check(addr, qty, 125) is { } r23) return Refuse(fc, r23);
|
||||
if (Check(writeAddr, writeQty, 121) is { } w23) return Refuse(fc, w23);
|
||||
if (pdu[9] != writeQty * 2 || pdu.Length != 10 + pdu[9]) return Refuse(fc, 0x03); // strict frame length
|
||||
WritePdus++;
|
||||
for (var i = 0; i < writeQty; i++) Holding[writeAddr + i] = (ushort)((pdu[10 + i * 2] << 8) | pdu[11 + i * 2]);
|
||||
return Task.FromResult(ReadRegs(0x17, addr, qty));
|
||||
|
||||
Reference in New Issue
Block a user