Foundation for the Modbus addressing-grammar work tracked in #137-#145. Adds ModbusModiconAddress.Parse / TryParse that turns classic Modicon strings (40001 / 400001 / 30001 / 00001 / 10001) into (Region, ushort PduOffset). Also extracts ModbusRegion to a new Driver.Modbus.Addressing assembly so the Admin UI (#145) can reference the addressing surface without taking a dep on the wire driver. The new assembly intentionally extends the same ZB.MOM.WW.OtOpcUa.Driver.Modbus namespace as the driver — callers see the type as if it lived in one place; only the project layout changes. No existing call site needed editing (zero-churn move). Behaviour: - Single leading digit selects region (0=Coils, 1=DiscreteInputs, 3=InputRegisters, 4=HoldingRegisters). - 5-digit form: trailing 4 digits are 1-based register, supports 1..9999. - 6-digit form: trailing 5 digits are 1-based register, supports 1..65536 (full PDU address space). - Strict 5-or-6 length check; whitespace trimmed; clear FormatException diagnostics for every malformed shape (wrong length, non-digit body, illegal leading digit, register zero, register overflow). 29/29 new unit tests pass. Full Driver.Modbus suite (182 tests) and the solution-wide build still green after the ModbusRegion move.
87 lines
3.8 KiB
C#
87 lines
3.8 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Modbus;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests;
|
|
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ModbusModiconAddressTests
|
|
{
|
|
[Theory]
|
|
// 5-digit form, one per region. Trailing 4 digits = 1-based register; PDU offset is one less.
|
|
[InlineData("00001", ModbusRegion.Coils, (ushort)0)]
|
|
[InlineData("09999", ModbusRegion.Coils, (ushort)9998)]
|
|
[InlineData("10001", ModbusRegion.DiscreteInputs, (ushort)0)]
|
|
[InlineData("19999", ModbusRegion.DiscreteInputs, (ushort)9998)]
|
|
[InlineData("30001", ModbusRegion.InputRegisters, (ushort)0)]
|
|
[InlineData("39999", ModbusRegion.InputRegisters, (ushort)9998)]
|
|
[InlineData("40001", ModbusRegion.HoldingRegisters, (ushort)0)]
|
|
[InlineData("49999", ModbusRegion.HoldingRegisters, (ushort)9998)]
|
|
// 6-digit form unlocks the full 16-bit address space — 1..65536 → PDU 0..65535.
|
|
[InlineData("400001", ModbusRegion.HoldingRegisters, (ushort)0)]
|
|
[InlineData("410000", ModbusRegion.HoldingRegisters, (ushort)9999)]
|
|
[InlineData("465536", ModbusRegion.HoldingRegisters, (ushort)65535)]
|
|
[InlineData("000001", ModbusRegion.Coils, (ushort)0)]
|
|
[InlineData("100001", ModbusRegion.DiscreteInputs, (ushort)0)]
|
|
[InlineData("365536", ModbusRegion.InputRegisters, (ushort)65535)]
|
|
public void Parse_Valid(string address, ModbusRegion expectedRegion, ushort expectedOffset)
|
|
{
|
|
var (region, offset) = ModbusModiconAddress.Parse(address);
|
|
region.ShouldBe(expectedRegion);
|
|
offset.ShouldBe(expectedOffset);
|
|
}
|
|
|
|
[Theory]
|
|
[InlineData("", "null or empty")]
|
|
[InlineData(" ", "null or empty")]
|
|
[InlineData("4001", "5 or 6 digits")] // 4 chars
|
|
[InlineData("4000001", "5 or 6 digits")] // 7 chars
|
|
[InlineData("4000A", "only digits")] // letter in body
|
|
[InlineData("X0001", "only digits")] // letter leading
|
|
[InlineData("20001", "leading digit must be 0/1/3/4")] // region 2 doesn't exist
|
|
[InlineData("50001", "leading digit must be 0/1/3/4")]
|
|
[InlineData("90001", "leading digit must be 0/1/3/4")]
|
|
[InlineData("40000", "must be >= 1")] // 0-based register number
|
|
[InlineData("400000", "must be >= 1")] // 6-digit zero
|
|
public void Parse_Invalid_Surfaces_Diagnostic(string address, string fragment)
|
|
{
|
|
Should.Throw<FormatException>(() => ModbusModiconAddress.Parse(address))
|
|
.Message.ShouldContain(fragment, Case.Insensitive);
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_Returns_False_With_Diagnostic_On_Invalid()
|
|
{
|
|
var ok = ModbusModiconAddress.TryParse("not-an-address", out _, out _, out var error);
|
|
ok.ShouldBeFalse();
|
|
error.ShouldNotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_Returns_True_With_Null_Error_On_Valid()
|
|
{
|
|
var ok = ModbusModiconAddress.TryParse("40001", out var region, out var offset, out var error);
|
|
ok.ShouldBeTrue();
|
|
region.ShouldBe(ModbusRegion.HoldingRegisters);
|
|
offset.ShouldBe((ushort)0);
|
|
error.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_Handles_Null()
|
|
{
|
|
ModbusModiconAddress.TryParse(null, out _, out _, out var error).ShouldBeFalse();
|
|
error.ShouldNotBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public void TryParse_Trims_Whitespace()
|
|
{
|
|
// Tag spreadsheets often arrive with stray padding; the parser tolerates it rather than
|
|
// forcing every importer to trim — but stays strict on the 5/6-digit length once trimmed.
|
|
ModbusModiconAddress.TryParse(" 40001 ", out var region, out var offset, out _).ShouldBeTrue();
|
|
region.ShouldBe(ModbusRegion.HoldingRegisters);
|
|
offset.ShouldBe((ushort)0);
|
|
}
|
|
}
|