80ef8806e0
- Driver.Modbus.Cli-003: ModbusCommandBase.ValidateEndpoint rejects --port outside 1..65535, non-positive --timeout-ms, and --unit-id outside 1..247. - Driver.Modbus.Cli-004: wrapped SubscribeCommand's OnDataChange handler body in a try/catch (warn-and-swallow) and serialised the console write through a lock. - Driver.Modbus.Cli-005: Probe / Read / Write now catch the cancellation-during-init OperationCanceledException and print 'Cancelled.' instead of dumping a stack trace. - Driver.Modbus.Cli-006: ProbeCommand.ComputeVerdict derives the headline from BOTH the driver state and the probe snapshot's OPC UA quality class so the headline can't disagree with the wire result. - Driver.Modbus.Cli-007: docs/Driver.Modbus.Cli.md carries an explicit 'CLI scope' callout — the address-string grammar is a DriverConfig JSON feature; the CLI takes the structured triple only. - Driver.Modbus.Cli-008: pinned BuildOptions, ValidateEndpoint, the region-validation guards, ComputeVerdict, and the cancellation-during- initialize paths. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
136 lines
6.5 KiB
C#
136 lines
6.5 KiB
C#
using System.Globalization;
|
|
using CliFx.Attributes;
|
|
using CliFx.Infrastructure;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus.Cli.Commands;
|
|
|
|
/// <summary>
|
|
/// Write one value to a Modbus coil or holding register. Mirrors <see cref="ReadCommand"/>'s
|
|
/// region / address / type flags + adds <c>--value</c>. Input parsing respects the
|
|
/// declared <c>--type</c> so you can write <c>--value=3.14 --type=Float32</c> without
|
|
/// hex-encoding floats. The write is non-idempotent by default (driver's
|
|
/// <c>WriteIdempotent=false</c>) — replay is the operator's choice, not the driver's.
|
|
/// </summary>
|
|
[Command("write", Description = "Write a single Modbus coil or holding register.")]
|
|
public sealed class WriteCommand : ModbusCommandBase
|
|
{
|
|
[CommandOption("region", 'r', Description =
|
|
"Coils or HoldingRegisters (the only writable regions per the protocol spec).",
|
|
IsRequired = true)]
|
|
public ModbusRegion Region { get; init; }
|
|
|
|
[CommandOption("address", 'a', Description =
|
|
"Zero-based address within the region.", IsRequired = true)]
|
|
public ushort Address { get; init; }
|
|
|
|
[CommandOption("type", 't', Description =
|
|
"Bool / Int16 / UInt16 / Int32 / UInt32 / Int64 / UInt64 / Float32 / Float64 / " +
|
|
"BitInRegister / String / Bcd16 / Bcd32", IsRequired = true)]
|
|
public ModbusDataType DataType { get; init; }
|
|
|
|
[CommandOption("value", 'v', Description =
|
|
"Value to write. Parsed per --type (booleans accept true/false/0/1).",
|
|
IsRequired = true)]
|
|
public string Value { get; init; } = default!;
|
|
|
|
[CommandOption("byte-order", Description =
|
|
"BigEndian (default, ABCD) or WordSwap (CDAB). Ignored for single-register types.")]
|
|
public ModbusByteOrder ByteOrder { get; init; } = ModbusByteOrder.BigEndian;
|
|
|
|
[CommandOption("bit-index", Description =
|
|
"For type=BitInRegister: which bit of the holding register (0-15, LSB-first).")]
|
|
public byte BitIndex { get; init; }
|
|
|
|
[CommandOption("string-length", Description =
|
|
"For type=String: character count (2 per register, rounded up).")]
|
|
public ushort StringLength { get; init; }
|
|
|
|
[CommandOption("string-byte-order", Description =
|
|
"For type=String: HighByteFirst (standard) or LowByteFirst (DirectLOGIC).")]
|
|
public ModbusStringByteOrder StringByteOrder { get; init; } = ModbusStringByteOrder.HighByteFirst;
|
|
|
|
public override async ValueTask ExecuteAsync(IConsole console)
|
|
{
|
|
ConfigureLogging();
|
|
ValidateEndpoint();
|
|
var ct = console.RegisterCancellationHandler();
|
|
|
|
if (Region is not (ModbusRegion.Coils or ModbusRegion.HoldingRegisters))
|
|
throw new CliFx.Exceptions.CommandException(
|
|
$"Region '{Region}' is read-only in the Modbus spec; writes require Coils or HoldingRegisters.");
|
|
|
|
// Driver.Modbus.Cli-002: coils are single-bit outputs — only Bool makes sense. A
|
|
// non-boolean type (e.g. --region Coils --type UInt16) would silently coerce the value
|
|
// to a boolean via Convert.ToBoolean, landing as ON for any non-zero value, with no
|
|
// diagnostic. Reject it early so the operator sees a clear error rather than a silent
|
|
// type-mismatch coerce.
|
|
if (Region == ModbusRegion.Coils && DataType != ModbusDataType.Bool)
|
|
throw new CliFx.Exceptions.CommandException(
|
|
$"Region 'Coils' only supports boolean values (--type Bool). " +
|
|
$"Type '{DataType}' cannot represent a single-bit coil write.");
|
|
|
|
var tagName = ReadCommand.SynthesiseTagName(Region, Address, DataType);
|
|
var tag = new ModbusTagDefinition(
|
|
Name: tagName,
|
|
Region: Region,
|
|
Address: Address,
|
|
DataType: DataType,
|
|
Writable: true,
|
|
ByteOrder: ByteOrder,
|
|
BitIndex: BitIndex,
|
|
StringLength: StringLength,
|
|
StringByteOrder: StringByteOrder);
|
|
var options = BuildOptions([tag]);
|
|
|
|
var parsed = ParseValue(Value, DataType);
|
|
|
|
await using var driver = new ModbusDriver(options, DriverInstanceId);
|
|
try
|
|
{
|
|
await driver.InitializeAsync("{}", ct);
|
|
var results = await driver.WriteAsync([new WriteRequest(tagName, parsed)], ct);
|
|
await console.Output.WriteLineAsync(SnapshotFormatter.FormatWrite(tagName, results[0]));
|
|
}
|
|
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
|
{
|
|
// Driver.Modbus.Cli-005: Ctrl+C during driver connect/write — exit quietly so
|
|
// CliFx does not render a full stack trace for a user-initiated cancellation.
|
|
await console.Output.WriteLineAsync("Cancelled.");
|
|
}
|
|
finally
|
|
{
|
|
await driver.ShutdownAsync(CancellationToken.None);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Parse the operator's <c>--value</c> string into the CLR type the driver expects
|
|
/// for the declared <see cref="ModbusDataType"/>. Uses invariant culture everywhere
|
|
/// so <c>3.14</c> and <c>3,14</c> don't swap meaning between runs.
|
|
/// </summary>
|
|
internal static object ParseValue(string raw, ModbusDataType type) => type switch
|
|
{
|
|
ModbusDataType.Bool or ModbusDataType.BitInRegister => ParseBool(raw),
|
|
ModbusDataType.Int16 => short.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.UInt16 or ModbusDataType.Bcd16 => ushort.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.Int32 => int.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.UInt32 or ModbusDataType.Bcd32 => uint.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.Int64 => long.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.UInt64 => ulong.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.Float32 => float.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.Float64 => double.Parse(raw, CultureInfo.InvariantCulture),
|
|
ModbusDataType.String => raw,
|
|
_ => throw new CliFx.Exceptions.CommandException($"Unsupported DataType '{type}' for write."),
|
|
};
|
|
|
|
private static bool ParseBool(string raw) => raw.Trim().ToLowerInvariant() switch
|
|
{
|
|
"1" or "true" or "on" or "yes" => true,
|
|
"0" or "false" or "off" or "no" => false,
|
|
_ => throw new CliFx.Exceptions.CommandException(
|
|
$"Boolean value '{raw}' is not recognised. Use true/false, 1/0, on/off, or yes/no."),
|
|
};
|
|
}
|