fix(modbus): #981 review — MELSEC no longer claims the generic DI<n> discrete-input mnemonic

The S-8 gate (no generic mnemonic fallback once a family grammar claimed the address) made
the MELSEC D-register grammar, which claimed every D prefix, reject DI100 as a malformed
D-register. MELSEC has no DI device, so DI... is no longer claimed; every other D... stays
native and DL205's non-octal C rejection is untouched.
This commit is contained in:
Joseph Doherty
2026-12-03 11:58:15 -05:00
parent b74b5162d2
commit 7374fdd0e4
3 changed files with 39 additions and 1 deletions
+1 -1
View File
@@ -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<n>` 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<n>` 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,
@@ -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);
@@ -202,6 +202,37 @@ public sealed class ModbusFamilyParserTests
generic.Offset.ShouldBe((ushort)17);
}
/// <summary>
/// #981 review: the S-8 gate stopped the generic mnemonic table whenever the family grammar CLAIMED an address,
/// and the MELSEC grammar claims every <c>D</c> prefix (D-registers) — so <c>DI100</c>, the generic
/// discrete-input mnemonic, stopped parsing under MELSEC (it failed as a malformed D-register). MELSEC has no
/// <c>DI</c> device, so the grammar must not claim it.
/// </summary>
/// <param name="addr">A generic discrete-input mnemonic.</param>
/// <param name="expectedOffset">Its zero-based offset.</param>
[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);
}
/// <summary>Controls for the narrow MELSEC fix: a D-register still resolves natively, and a malformed one still errors.</summary>
[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();
}
/// <summary>Verifies suffix grammar composes correctly with native addresses.</summary>
[Fact]
public void Suffix_Grammar_Composes_With_Native_Address()