using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus;
///
/// Modbus TCP driver configuration. Bound from the driver's DriverConfig JSON at
/// DriverHost.RegisterAsync. Every register the driver exposes appears in
/// ; names become the OPC UA browse name + full reference.
///
public sealed class ModbusDriverOptions
{
public string Host { get; init; } = "127.0.0.1";
public int Port { get; init; } = 502;
public byte UnitId { get; init; } = 1;
public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(2);
/// Pre-declared tag map. Modbus has no discovery protocol — the driver returns exactly these.
public IReadOnlyList Tags { get; init; } = [];
///
/// Background connectivity-probe settings. When
/// is true the driver runs a tick loop that issues a cheap FC03 at register 0 every
/// and raises OnHostStatusChanged on
/// Running ↔ Stopped transitions. The Admin UI / OPC UA clients see the state through
/// .
///
public ModbusProbeOptions Probe { get; init; } = new();
}
public sealed class ModbusProbeOptions
{
public bool Enabled { get; init; } = true;
public TimeSpan Interval { get; init; } = TimeSpan.FromSeconds(5);
public TimeSpan Timeout { get; init; } = TimeSpan.FromSeconds(2);
/// Register to read for the probe. Zero is usually safe; override for PLCs that lock register 0.
public ushort ProbeAddress { get; init; } = 0;
}
///
/// One Modbus-backed OPC UA variable. Address is zero-based (Modbus spec numbering, not
/// the documentation's 1-based coil/register conventions). Multi-register types
/// (Int32/UInt32/Float32 = 2 regs; Int64/UInt64/Float64 = 4 regs) respect the
/// field — real-world PLCs disagree on word ordering.
///
///
/// Tag name, used for both the OPC UA browse name and the driver's full reference. Must be
/// unique within the driver.
///
/// Coils / DiscreteInputs / InputRegisters / HoldingRegisters.
/// Zero-based address within the region.
///
/// Logical data type. See for the register count each encodes.
///
/// When true and Region supports writes (Coils / HoldingRegisters), IWritable routes writes here.
/// Word ordering for multi-register types. Ignored for Bool / Int16 / UInt16 / BitInRegister / String.
/// For DataType = BitInRegister: which bit of the holding register (0-15, LSB-first).
/// For DataType = String: number of ASCII characters (2 per register, rounded up).
public sealed record ModbusTagDefinition(
string Name,
ModbusRegion Region,
ushort Address,
ModbusDataType DataType,
bool Writable = true,
ModbusByteOrder ByteOrder = ModbusByteOrder.BigEndian,
byte BitIndex = 0,
ushort StringLength = 0);
public enum ModbusRegion { Coils, DiscreteInputs, InputRegisters, HoldingRegisters }
public enum ModbusDataType
{
Bool,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
Float32,
Float64,
/// Single bit within a holding register. selects 0-15 LSB-first.
BitInRegister,
/// ASCII string packed 2 chars per register, characters long.
String,
}
///
/// Word ordering for multi-register types. Modbus TCP standard is
/// (ABCD for 32-bit: high word at the lower address). Many PLCs — Siemens S7, several
/// Allen-Bradley series, some Modicon families — use (CDAB), which
/// keeps bytes big-endian within each register but reverses the word pair(s).
///
public enum ModbusByteOrder
{
BigEndian,
WordSwap,
}