diff --git a/docs/drivers/Modbus.md b/docs/drivers/Modbus.md index 3a05d0231..675510e28 100644 --- a/docs/drivers/Modbus.md +++ b/docs/drivers/Modbus.md @@ -69,7 +69,7 @@ appear across consecutive registers when decoding a multi-register value. - **`Generic`** (default) — only Modicon (`4xxxx`) and mnemonic (`HR1`, `C100`) forms are accepted. - **`DL205`** — AutomationDirect DirectLOGIC. V-memory (octal) → HoldingRegisters, `Y`/`C` → Coils, `X`/`SP` → DiscreteInputs. A `C` relay with a non-octal digit (`C18`) is a parse error - it never falls back to the generic `C` coil mnemonic (#981). Strings can be packed low-byte-first via `ModbusTagDefinition.StringByteOrder` (the grammar can't express this — see `ModbusStringByteOrder`). -- **`MELSEC`** — Mitsubishi. D-registers → HoldingRegisters, `X` → DiscreteInputs, `Y`/`M` → Coils; the `MelsecSubFamily` selector switches Q/L/iQR (hex) vs FX (octal) X/Y interpretation. +- **`MELSEC`** — Mitsubishi. D-registers → HoldingRegisters, `X` → DiscreteInputs, `Y`/`M` → Coils; the `MelsecSubFamily` selector switches Q/L/iQR (hex) vs FX (octal) X/Y interpretation. MELSEC has no `DI` device, so `DI` stays the generic discrete-input mnemonic (#981 review — the D-register grammar briefly claimed it and rejected it as a malformed register). Per-family register caps are honoured through `MaxRegistersPerRead` / `MaxRegistersPerWrite` / `MaxCoilsPerRead` (e.g. DL205/DL260 cap reads at 128, diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing/ModbusAddressParser.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing/ModbusAddressParser.cs index a0ca61d7e..0ff245443 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing/ModbusAddressParser.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing/ModbusAddressParser.cs @@ -346,6 +346,13 @@ public static class ModbusAddressParser // The MelsecAddress helpers honour the sub-family (Q hex vs F octal) and use // bank base 0; users with non-zero assignment bases must use the structured // tag form to override. The grammar string covers the common base-0 path. + // + // #981 review: MELSEC has no `DI` device, so `DI…` is NOT claimed here — it is the generic + // discrete-input mnemonic, which the S-8 gate (no generic fallback once a family grammar claimed an + // address) would otherwise reject as a malformed D-register. Narrow on purpose: every other `D…` + // stays native, and DL205's non-octal rejection is untouched. + if (text.StartsWith("DI", StringComparison.OrdinalIgnoreCase)) + return false; if (text.StartsWith("D", StringComparison.OrdinalIgnoreCase)) { offset = MelsecAddress.DRegisterToHolding(text); diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests/ModbusFamilyParserTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests/ModbusFamilyParserTests.cs index 6798d7530..5a194dfc4 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests/ModbusFamilyParserTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests/ModbusFamilyParserTests.cs @@ -202,6 +202,37 @@ public sealed class ModbusFamilyParserTests generic.Offset.ShouldBe((ushort)17); } + /// + /// #981 review: the S-8 gate stopped the generic mnemonic table whenever the family grammar CLAIMED an address, + /// and the MELSEC grammar claims every D prefix (D-registers) — so DI100, the generic + /// discrete-input mnemonic, stopped parsing under MELSEC (it failed as a malformed D-register). MELSEC has no + /// DI device, so the grammar must not claim it. + /// + /// A generic discrete-input mnemonic. + /// Its zero-based offset. + [Theory] + [InlineData("DI100", 99)] + [InlineData("di1", 0)] + public void MELSEC_Generic_DiscreteInput_Mnemonic_Still_Parses(string addr, int expectedOffset) + { + ModbusAddressParser.TryParse(addr, ModbusFamily.MELSEC, MelsecFamily.Q_L_iQR, out var result, out var error) + .ShouldBeTrue(error); + result!.Region.ShouldBe(ModbusRegion.DiscreteInputs); + result.Offset.ShouldBe((ushort)expectedOffset); + } + + /// Controls for the narrow MELSEC fix: a D-register still resolves natively, and a malformed one still errors. + [Fact] + public void MELSEC_D_Register_Still_Native_And_Malformed_Still_Errors() + { + var d = ModbusAddressParser.Parse("D100", ModbusFamily.MELSEC); + d.Region.ShouldBe(ModbusRegion.HoldingRegisters); + d.Offset.ShouldBe((ushort)100); + + ModbusAddressParser.TryParse("DX12", ModbusFamily.MELSEC, MelsecFamily.Q_L_iQR, out _, out var error).ShouldBeFalse(); + error.ShouldNotBeNull(); + } + /// Verifies suffix grammar composes correctly with native addresses. [Fact] public void Suffix_Grammar_Composes_With_Native_Address()