chore: organize solution into module folders (Core/Server/Drivers/Client/Tooling)
Group all 69 projects into category subfolders under src/ and tests/ so the Rider Solution Explorer mirrors the module structure. Folders: Core, Server, Drivers (with a nested Driver CLIs subfolder), Client, Tooling. - Move every project folder on disk with git mv (history preserved as renames). - Recompute relative paths in 57 .csproj files: cross-category ProjectReferences, the lib/ HintPath+None refs in Driver.Historian.Wonderware, and the external mxaccessgw refs in Driver.Galaxy and its test project. - Rebuild ZB.MOM.WW.OtOpcUa.slnx with nested solution folders. - Re-prefix project paths in functional scripts (e2e, compliance, smoke SQL, integration, install). Build green (0 errors); unit tests pass. Docs left for a separate pass. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,313 @@
|
||||
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 ModbusAddressParserTests
|
||||
{
|
||||
// ----- Bare Modicon-only forms inherit #136 behaviour; one sanity row per region. -----
|
||||
|
||||
[Theory]
|
||||
[InlineData("40001", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16)]
|
||||
[InlineData("400001", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16)]
|
||||
[InlineData("30001", ModbusRegion.InputRegisters, 0, ModbusDataType.Int16)]
|
||||
[InlineData("00001", ModbusRegion.Coils, 0, ModbusDataType.Bool)]
|
||||
[InlineData("10001", ModbusRegion.DiscreteInputs, 0, ModbusDataType.Bool)]
|
||||
[InlineData("465536", ModbusRegion.HoldingRegisters, 65535, ModbusDataType.Int16)]
|
||||
public void Bare_Modicon_Defaults_DataType_From_Region(string addr, ModbusRegion region, int offset, ModbusDataType type)
|
||||
{
|
||||
var p = ModbusAddressParser.Parse(addr);
|
||||
p.Region.ShouldBe(region);
|
||||
p.Offset.ShouldBe((ushort)offset);
|
||||
p.DataType.ShouldBe(type);
|
||||
p.Bit.ShouldBeNull();
|
||||
p.ArrayCount.ShouldBeNull();
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.BigEndian);
|
||||
}
|
||||
|
||||
// ----- Mnemonic forms — HR / IR / C / DI -----
|
||||
|
||||
[Theory]
|
||||
[InlineData("HR1", ModbusRegion.HoldingRegisters, 0)]
|
||||
[InlineData("HR65536", ModbusRegion.HoldingRegisters, 65535)]
|
||||
[InlineData("IR1", ModbusRegion.InputRegisters, 0)]
|
||||
[InlineData("C100", ModbusRegion.Coils, 99)]
|
||||
[InlineData("DI1", ModbusRegion.DiscreteInputs, 0)]
|
||||
[InlineData("hr1", ModbusRegion.HoldingRegisters, 0)] // lowercase
|
||||
[InlineData("Ir50", ModbusRegion.InputRegisters, 49)] // mixed case
|
||||
public void Mnemonic_Region_Forms_Parse(string addr, ModbusRegion region, int offset)
|
||||
{
|
||||
var p = ModbusAddressParser.Parse(addr);
|
||||
p.Region.ShouldBe(region);
|
||||
p.Offset.ShouldBe((ushort)offset);
|
||||
}
|
||||
|
||||
// ----- Bit suffix .N -----
|
||||
|
||||
[Theory]
|
||||
[InlineData("40001.0", 0)]
|
||||
[InlineData("40001.5", 5)]
|
||||
[InlineData("40001.15", 15)]
|
||||
[InlineData("HR1.7", 7)]
|
||||
public void Bit_Suffix_Implies_BitInRegister(string addr, int expectedBit)
|
||||
{
|
||||
var p = ModbusAddressParser.Parse(addr);
|
||||
p.Bit.ShouldBe((byte)expectedBit);
|
||||
p.DataType.ShouldBe(ModbusDataType.BitInRegister);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bit_Plus_Explicit_Type_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001.5:F"))
|
||||
.Message.ShouldContain("Bit suffix");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bit_Above_15_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001.16"))
|
||||
.Message.ShouldContain("0..15");
|
||||
}
|
||||
|
||||
// ----- Type codes -----
|
||||
|
||||
// #146 — codes verified against current Wonderware DASMBTCP + Ignition Modbus docs
|
||||
// (2026-04-25). `:I` = Int32 (was Int16) per Wonderware; `:S` is the explicit Int16 code.
|
||||
// `:US` for UInt16 (Ignition HRUS). `:I_64` / `:UI_64` for 64-bit (Ignition HRI_64).
|
||||
// `:BCD_32` for 32-bit BCD (Ignition HRBCD_32). The pre-#146 `:DI`/`:L`/`:UDI`/`:UL`/
|
||||
// `:LI`/`:ULI`/`:LBCD` aliases are removed — they conflict with the Wonderware mapping
|
||||
// and have no clear vendor precedent.
|
||||
[Theory]
|
||||
[InlineData("40001:BOOL", ModbusDataType.Bool)]
|
||||
[InlineData("40001:S", ModbusDataType.Int16)]
|
||||
[InlineData("40001:US", ModbusDataType.UInt16)]
|
||||
[InlineData("40001:I", ModbusDataType.Int32)]
|
||||
[InlineData("40001:UI", ModbusDataType.UInt32)]
|
||||
[InlineData("40001:I_64", ModbusDataType.Int64)]
|
||||
[InlineData("40001:UI_64", ModbusDataType.UInt64)]
|
||||
[InlineData("40001:F", ModbusDataType.Float32)]
|
||||
[InlineData("40001:D", ModbusDataType.Float64)]
|
||||
[InlineData("40001:BCD", ModbusDataType.Bcd16)]
|
||||
[InlineData("40001:BCD_32", ModbusDataType.Bcd32)]
|
||||
[InlineData("40001:f", ModbusDataType.Float32)] // lowercase
|
||||
[InlineData("40001:i_64", ModbusDataType.Int64)] // lowercase + underscore form
|
||||
public void Type_Codes_Parse(string addr, ModbusDataType expected)
|
||||
{
|
||||
ModbusAddressParser.Parse(addr).DataType.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("40001:DI")] // pre-#146 alias removed
|
||||
[InlineData("40001:L")]
|
||||
[InlineData("40001:UDI")]
|
||||
[InlineData("40001:UL")]
|
||||
[InlineData("40001:LI")]
|
||||
[InlineData("40001:ULI")]
|
||||
[InlineData("40001:LBCD")]
|
||||
public void Removed_Aliases_Are_Rejected(string addr)
|
||||
{
|
||||
// Defensive — these used to be accepted; now they fail with a clear "Unknown type code"
|
||||
// diagnostic so users with legacy spreadsheets get a fast surface-level error rather
|
||||
// than silent wrong-typed reads.
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse(addr))
|
||||
.Message.ShouldContain("Unknown type code");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("40001:STR1", 1)]
|
||||
[InlineData("40001:STR20", 20)]
|
||||
[InlineData("40001:STR255", 255)]
|
||||
public void STR_Type_Carries_Length(string addr, int expectedLen)
|
||||
{
|
||||
var p = ModbusAddressParser.Parse(addr);
|
||||
p.DataType.ShouldBe(ModbusDataType.String);
|
||||
p.StringLength.ShouldBe((ushort)expectedLen);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void STR_Without_Length_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:STR"))
|
||||
.Message.ShouldContain("STR");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void STR_Length_Zero_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:STR0"))
|
||||
.Message.ShouldContain("positive");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unknown_Type_Code_Rejected_With_Catalog()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:WIDGET"))
|
||||
.Message.ShouldContain("Valid: BOOL, S, US, I,");
|
||||
}
|
||||
|
||||
// ----- Region-type compatibility -----
|
||||
|
||||
[Fact]
|
||||
public void Coils_With_Float_Type_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("00001:F"))
|
||||
.Message.ShouldContain("only supports Bool");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DiscreteInputs_With_Int_Type_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("10001:I"))
|
||||
.Message.ShouldContain("only supports Bool");
|
||||
}
|
||||
|
||||
// ----- Byte order modifiers — all four -----
|
||||
|
||||
[Theory]
|
||||
[InlineData("40001:F:ABCD", ModbusByteOrder.BigEndian)]
|
||||
[InlineData("40001:F:CDAB", ModbusByteOrder.WordSwap)]
|
||||
[InlineData("40001:F:BADC", ModbusByteOrder.ByteSwap)]
|
||||
[InlineData("40001:F:DCBA", ModbusByteOrder.FullReverse)]
|
||||
[InlineData("40001:F:cdab", ModbusByteOrder.WordSwap)] // lowercase
|
||||
public void Byte_Order_Modifiers_Parse(string addr, ModbusByteOrder expected)
|
||||
{
|
||||
ModbusAddressParser.Parse(addr).ByteOrder.ShouldBe(expected);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Unknown_Byte_Order_Rejected_With_Catalog()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:F:WXYZ"))
|
||||
.Message.ShouldContain("Valid: ABCD, CDAB, BADC, DCBA");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Empty_Order_Field_Means_Default()
|
||||
{
|
||||
// 40001:I::5 → Int32 array (5 elements), no order override, default (BigEndian).
|
||||
// The empty middle field is the strict-mode marker that "third position is array count,
|
||||
// not byte order" — exercises the 4-field positional parse without a byte-order value.
|
||||
var p = ModbusAddressParser.Parse("40001:I::5");
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.BigEndian);
|
||||
p.ArrayCount.ShouldBe(5);
|
||||
p.DataType.ShouldBe(ModbusDataType.Int32);
|
||||
}
|
||||
|
||||
// ----- Array count -----
|
||||
|
||||
[Theory]
|
||||
[InlineData("40001:I:ABCD:1", 1)]
|
||||
[InlineData("40001:F:5", 5)]
|
||||
[InlineData("40001:F:CDAB:10", 10)]
|
||||
[InlineData("40001:S:100", 100)] // Int16[100] via the post-#146 :S code
|
||||
public void Array_Count_Parses(string addr, int expectedCount)
|
||||
{
|
||||
ModbusAddressParser.Parse(addr).ArrayCount.ShouldBe(expectedCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Array_Count_Zero_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:F:ABCD:0"))
|
||||
.Message.ShouldContain("positive");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Array_Count_NonNumeric_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:F:ABCD:five"))
|
||||
.Message.ShouldContain("positive");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Bit_Plus_Array_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001.5:::5"))
|
||||
.Message.ShouldContain("Bit suffix and array count");
|
||||
}
|
||||
|
||||
// ----- Composition / examples -----
|
||||
|
||||
[Fact]
|
||||
public void Worked_Example_Float_With_Word_Swap()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("40001:F:CDAB");
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)0);
|
||||
p.DataType.ShouldBe(ModbusDataType.Float32);
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.WordSwap);
|
||||
p.ArrayCount.ShouldBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Worked_Example_Int16_Array()
|
||||
{
|
||||
// Post-#146: :S is the explicit Int16 code. (`:I` is now Int32 per Wonderware.)
|
||||
var p = ModbusAddressParser.Parse("40001:S::10");
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.DataType.ShouldBe(ModbusDataType.Int16);
|
||||
p.ArrayCount.ShouldBe(10);
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.BigEndian);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Worked_Example_Int32_Array_Via_I_Code()
|
||||
{
|
||||
// Companion to the above — `:I` now means Int32 (matches Wonderware DASMBTCP and
|
||||
// Ignition HRI). Anyone with `:I` in legacy spreadsheets gets a different type than
|
||||
// pre-#146; that's the intended semantic alignment.
|
||||
var p = ModbusAddressParser.Parse("40001:I::10");
|
||||
p.DataType.ShouldBe(ModbusDataType.Int32);
|
||||
p.ArrayCount.ShouldBe(10);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Worked_Example_Float_Array_Word_Swap_6_Digit()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("465500:F:CDAB:5");
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)65499);
|
||||
p.DataType.ShouldBe(ModbusDataType.Float32);
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.WordSwap);
|
||||
p.ArrayCount.ShouldBe(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Worked_Example_String_With_Length()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("40001:STR20");
|
||||
p.DataType.ShouldBe(ModbusDataType.String);
|
||||
p.StringLength.ShouldBe((ushort)20);
|
||||
p.ArrayCount.ShouldBeNull(); // strings ARE multi-register but they are not "array of string"
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParse_Returns_Diagnostic_On_Failure()
|
||||
{
|
||||
ModbusAddressParser.TryParse("garbage", out var p, out var err).ShouldBeFalse();
|
||||
p.ShouldBeNull();
|
||||
err.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryParse_Returns_Result_On_Success()
|
||||
{
|
||||
ModbusAddressParser.TryParse("HR1:F:CDAB:3", out var p, out var err).ShouldBeTrue();
|
||||
p.ShouldNotBeNull();
|
||||
err.ShouldBeNull();
|
||||
p!.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.DataType.ShouldBe(ModbusDataType.Float32);
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.WordSwap);
|
||||
p.ArrayCount.ShouldBe(3);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Too_Many_Colons_Rejected()
|
||||
{
|
||||
Should.Throw<FormatException>(() => ModbusAddressParser.Parse("40001:F:CDAB:5:extra"))
|
||||
.Message.ShouldContain("too many");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Driver.Modbus;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// #144 family-native parser branch — DL205 + MELSEC. Family flag drives the parser to
|
||||
/// try the family's native syntax (V2000, D100, X20 hex/octal) before falling back to
|
||||
/// Modicon / mnemonic.
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class ModbusFamilyParserTests
|
||||
{
|
||||
// ----- DL205 native: V-memory (octal), Y/C/X/SP coils + discrete -----
|
||||
|
||||
[Theory]
|
||||
[InlineData("V0", 0)]
|
||||
[InlineData("V2000", 1024)] // octal 2000 = decimal 1024
|
||||
[InlineData("V40400", 16640)] // octal 40400 = decimal 16640 (system bank in user mapping per the helper)
|
||||
public void DL205_VMemory_To_HoldingRegisters(string addr, int expectedOffset)
|
||||
{
|
||||
var p = ModbusAddressParser.Parse(addr, ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)expectedOffset);
|
||||
p.DataType.ShouldBe(ModbusDataType.Int16);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DL205_Y_Output_Maps_To_Coils_Bank()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("Y0", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.Coils);
|
||||
p.Offset.ShouldBe((ushort)2048); // YOutputBaseCoil
|
||||
p.DataType.ShouldBe(ModbusDataType.Bool);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DL205_C_Relay_Maps_To_Coils_Bank_NotModiconCoil()
|
||||
{
|
||||
// Cross-family ambiguity check: under Generic, "C100" is mnemonic Coils[99].
|
||||
// Under DL205 family, "C100" is a control relay → CRelayBaseCoil + octal(100) = 3072 + 64.
|
||||
var p = ModbusAddressParser.Parse("C100", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.Coils);
|
||||
p.Offset.ShouldBe((ushort)(3072 + 64));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DL205_X_Input_Maps_To_DiscreteInputs()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("X17", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.DiscreteInputs);
|
||||
p.Offset.ShouldBe((ushort)15); // octal 17 = decimal 15
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DL205_SP_Special_Relay_Maps_To_DiscreteInputs()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("SP10", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.DiscreteInputs);
|
||||
p.Offset.ShouldBe((ushort)(1024 + 8)); // SpecialBaseDiscrete + octal(10)
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DL205_Falls_Back_To_Modicon_When_Native_Misses()
|
||||
{
|
||||
// 40001 isn't a DL205 native form — falls through to the Modicon parser, returns
|
||||
// HoldingRegisters[0]. Important for users mixing legacy Modicon entries with native.
|
||||
var p = ModbusAddressParser.Parse("40001", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)0);
|
||||
}
|
||||
|
||||
// ----- MELSEC native: D / X / Y / M with sub-family-aware X/Y parsing -----
|
||||
|
||||
[Fact]
|
||||
public void MELSEC_D_Register_Maps_To_HoldingRegisters()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("D100", ModbusFamily.MELSEC);
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)100); // base 0 + decimal 100
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MELSEC_M_Relay_Maps_To_Coils_Decimal()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("M50", ModbusFamily.MELSEC);
|
||||
p.Region.ShouldBe(ModbusRegion.Coils);
|
||||
p.Offset.ShouldBe((ushort)50);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MELSEC_Q_Family_Treats_X20_As_Hex()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("X20", ModbusFamily.MELSEC, MelsecFamily.Q_L_iQR);
|
||||
p.Region.ShouldBe(ModbusRegion.DiscreteInputs);
|
||||
p.Offset.ShouldBe((ushort)0x20); // hex 20 = decimal 32
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void MELSEC_F_Family_Treats_X20_As_Octal()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("X20", ModbusFamily.MELSEC, MelsecFamily.F_iQF);
|
||||
p.Region.ShouldBe(ModbusRegion.DiscreteInputs);
|
||||
p.Offset.ShouldBe((ushort)16); // octal 20 = decimal 16
|
||||
}
|
||||
|
||||
// ----- Cross-family safety / Generic regression -----
|
||||
|
||||
[Fact]
|
||||
public void Generic_Family_Does_Not_Try_DL205_Branch()
|
||||
{
|
||||
// "V2000" under Generic isn't a known mnemonic OR a Modicon address → parse fails.
|
||||
// (Only DL205 / MELSEC families know V-memory.)
|
||||
ModbusAddressParser.TryParse("V2000", ModbusFamily.Generic, MelsecFamily.Q_L_iQR, out _, out var error)
|
||||
.ShouldBeFalse();
|
||||
error.ShouldNotBeNull();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void C100_Under_Generic_Means_Modicon_Coil_99()
|
||||
{
|
||||
// Regression guard against the cross-family ambiguity: Generic must keep mnemonic "C"
|
||||
// mapping (Coil at offset = decimal-100 - 1).
|
||||
var p = ModbusAddressParser.Parse("C100", ModbusFamily.Generic);
|
||||
p.Region.ShouldBe(ModbusRegion.Coils);
|
||||
p.Offset.ShouldBe((ushort)99);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Suffix_Grammar_Composes_With_Native_Address()
|
||||
{
|
||||
// V2000:F:CDAB:5 should parse end-to-end: DL205 V2000 → HR[1024], Float32, word-swap, array of 5.
|
||||
var p = ModbusAddressParser.Parse("V2000:F:CDAB:5", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)1024);
|
||||
p.DataType.ShouldBe(ModbusDataType.Float32);
|
||||
p.ByteOrder.ShouldBe(ModbusByteOrder.WordSwap);
|
||||
p.ArrayCount.ShouldBe(5);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void DL205_Bit_Suffix_On_VMemory()
|
||||
{
|
||||
var p = ModbusAddressParser.Parse("V2000.7", ModbusFamily.DL205);
|
||||
p.Region.ShouldBe(ModbusRegion.HoldingRegisters);
|
||||
p.Offset.ShouldBe((ushort)1024);
|
||||
p.Bit.ShouldBe((byte)7);
|
||||
p.DataType.ShouldBe(ModbusDataType.BitInRegister);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
<RootNamespace>ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.Tests</RootNamespace>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="xunit.v3" Version="1.1.0"/>
|
||||
<PackageReference Include="Shouldly" Version="4.3.0"/>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0"/>
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Drivers\ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing\ZB.MOM.WW.OtOpcUa.Driver.Modbus.Addressing.csproj"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user