Lifts the previous "one driver = one slave" assumption so a single Modbus driver instance can front N RTU slaves behind one Ethernet gateway (Anybus, ProSoft, Lantronix style). Each tag carries an optional UnitId that drives the MBAP unit-id byte per-PDU, and the IPerCallHostResolver contract surfaces per-slave host strings so per-PLC circuit breakers fire per-slave (matches the AB CIP template documented in docs/v2/multi-host-dispatch.md). Changes: - ModbusTagDefinition gains optional UnitId (byte?). Null = use driver-level ModbusDriverOptions.UnitId (preserves single-slave deployments verbatim). - ResolveUnitId(tag) helper computed once per ReadOneAsync / WriteOneAsync call; passed through ReadRegisterBlockAsync / ReadBitBlockAsync / ReadRegisterBlockChunkedAsync / ReadBitBlockChunkedAsync explicitly. The probe loop continues using driver-level UnitId (the probe is a connection-health check, not slave-specific). - ModbusDriver implements IPerCallHostResolver. ResolveHost(fullReference) returns "host:port/unitN" — distinct strings per slave so the resilience pipeline keys breakers on the right granularity. Unknown references fall back to the bare HostName (single-slave behaviour). - BitInRegister RMW path also threads the per-tag UnitId through both the read and write halves so a multi-slave deployment stays correct under bit- level writes. - Factory DTO + JSON binding extended with the per-tag UnitId field. Tests (4 new ModbusMultiUnitTests): - Per-tag UnitId routes to the correct slave in the MBAP header (driver-level UnitId=99 must NOT appear when both tags override). - Tag without override falls back to driver-level UnitId. - IPerCallHostResolver returns distinct "host:port/unitN" strings per slave. - Unknown reference returns the bare HostName fallback. Existing 220 unit tests + 107 addressing tests still green. Per-PLC breaker isolation under simulated dead slaves is verifiable via the existing AB CIP test infra; live coverage lands as an integration test in the #138 docs/e2e refresh.
102 lines
4.5 KiB
C#
102 lines
4.5 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Tests;
|
|
|
|
/// <summary>
|
|
/// #142 multi-unit-ID gateway support: per-tag UnitId override + IPerCallHostResolver +
|
|
/// wire-level routing of UnitId in the MBAP header per-PDU.
|
|
/// </summary>
|
|
[Trait("Category", "Unit")]
|
|
public sealed class ModbusMultiUnitTests
|
|
{
|
|
private sealed class UnitCapturingTransport : IModbusTransport
|
|
{
|
|
public readonly List<byte> SeenUnitIds = new();
|
|
public Task ConnectAsync(CancellationToken ct) => Task.CompletedTask;
|
|
public Task<byte[]> SendAsync(byte unitId, byte[] pdu, CancellationToken ct)
|
|
{
|
|
SeenUnitIds.Add(unitId);
|
|
switch (pdu[0])
|
|
{
|
|
case 0x03: case 0x04:
|
|
{
|
|
var qty = (ushort)((pdu[3] << 8) | pdu[4]);
|
|
var resp = new byte[2 + qty * 2];
|
|
resp[0] = pdu[0]; resp[1] = (byte)(qty * 2);
|
|
return Task.FromResult(resp);
|
|
}
|
|
default: return Task.FromResult(new byte[] { pdu[0], 0, 0 });
|
|
}
|
|
}
|
|
public ValueTask DisposeAsync() => ValueTask.CompletedTask;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task PerTag_UnitId_Routes_To_Correct_Slave_In_MBAP()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var tagSlave1 = new ModbusTagDefinition("S1Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 1);
|
|
var tagSlave5 = new ModbusTagDefinition("S5Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 5);
|
|
var opts = new ModbusDriverOptions { Host = "f", UnitId = 99, Tags = [tagSlave1, tagSlave5],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.ReadAsync(["S1Temp", "S5Temp"], CancellationToken.None);
|
|
|
|
// Two reads: one for slave 1, one for slave 5. Driver-level UnitId=99 must NOT appear.
|
|
fake.SeenUnitIds.ShouldContain((byte)1);
|
|
fake.SeenUnitIds.ShouldContain((byte)5);
|
|
fake.SeenUnitIds.ShouldNotContain((byte)99);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Tag_Without_UnitId_Falls_Back_To_DriverLevel()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var tag = new ModbusTagDefinition("T", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16); // no UnitId override
|
|
var opts = new ModbusDriverOptions { Host = "f", UnitId = 7, Tags = [tag],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
await drv.ReadAsync(["T"], CancellationToken.None);
|
|
|
|
fake.SeenUnitIds.ShouldContain((byte)7);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task IPerCallHostResolver_Returns_Per_Slave_Host_String()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var t1 = new ModbusTagDefinition("S1Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 1);
|
|
var t5 = new ModbusTagDefinition("S5Temp", ModbusRegion.HoldingRegisters, 0, ModbusDataType.Int16, UnitId: 5);
|
|
var opts = new ModbusDriverOptions { Host = "10.1.2.3", Port = 502, Tags = [t1, t5],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
// The pipeline keys breakers on these strings; distinct slave IDs must produce distinct
|
|
// host strings so per-PLC isolation works.
|
|
var resolver = (IPerCallHostResolver)drv;
|
|
resolver.ResolveHost("S1Temp").ShouldBe("10.1.2.3:502/unit1");
|
|
resolver.ResolveHost("S5Temp").ShouldBe("10.1.2.3:502/unit5");
|
|
resolver.ResolveHost("S1Temp").ShouldNotBe(resolver.ResolveHost("S5Temp"));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task IPerCallHostResolver_Unknown_Tag_Falls_Back_To_HostName()
|
|
{
|
|
var fake = new UnitCapturingTransport();
|
|
var opts = new ModbusDriverOptions { Host = "10.1.2.3", Port = 502, Tags = [],
|
|
Probe = new ModbusProbeOptions { Enabled = false } };
|
|
var drv = new ModbusDriver(opts, "m1", _ => fake);
|
|
await drv.InitializeAsync("{}", CancellationToken.None);
|
|
|
|
var resolver = (IPerCallHostResolver)drv;
|
|
resolver.ResolveHost("never-defined").ShouldBe("10.1.2.3:502");
|
|
}
|
|
}
|