f17ba8e258
Array tags were advertised writable but every write threw InvalidCastException in ConvertForWrite (reported BadTypeMismatch); STRING writes went through the Beckhoff marshaller with no length check against STRING(n) and the host's ANSI code page, which the read side deliberately avoids. - Array write: TwinCATArrayCodec.ToWriteElements converts exactly ArrayLength elements to the declared type (CLR lists convert - ScadaBridge writes int[] to Int16 nodes; a length mismatch or out-of-range element is BadOutOfRange, a scalar value BadTypeMismatch) before any wire traffic; the ADS client plans the layout from the resolved PLC array (TwinCATTypeGuard.CheckArray, as the read does), encodes with TwinCATArrayCodec.Encode - the exact inverse of Decode - and writes the raw bytes (new ITwinCATClient.WriteArrayAsync). - STRING / WSTRING write: encoded at the PLC's declared width by TwinCATStringCodec.EncodeString/EncodeWString (Windows-1252 or UTF-8 per target, NUL-padded, strict: an over-long value or unrepresentable character is OverflowException -> BadOutOfRange) and written as raw bytes. - TwinCAT CLI read/write gain --array-length (+ --separator) for the hardware gate. Refs #739 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
74 lines
3.2 KiB
C#
74 lines
3.2 KiB
C#
using CliFx.Attributes;
|
|
using CliFx.Infrastructure;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Cli.Common;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.TwinCAT.Cli.Commands;
|
|
|
|
/// <summary>
|
|
/// Read one TwinCAT symbol by path. Structure writes/reads are out of scope — fan the
|
|
/// member list into individual reads if you need them.
|
|
/// </summary>
|
|
[Command("read", Description = "Read a single TwinCAT symbol.")]
|
|
public sealed class ReadCommand : TwinCATTagCommandBase
|
|
{
|
|
/// <summary>Gets or sets the TwinCAT symbol path to read.</summary>
|
|
[CommandOption("symbol", 's', Description =
|
|
"Symbol path. Program scope: 'MAIN.bStart'. Global: 'GVL.Counter'. " +
|
|
"Nested UDT member: 'Motor1.Status.Running'. Array element: 'Recipe[3]'. " +
|
|
"TwinCAT 2 global (keep its leading dot): '.OPC_HEARTBEAT'.",
|
|
IsRequired = true)]
|
|
public string SymbolPath { get; init; } = default!;
|
|
|
|
/// <summary>Gets or sets the data type of the symbol being read.</summary>
|
|
[CommandOption("type", 't', Description =
|
|
"Bool / SInt / USInt / Int / UInt / DInt / UDInt / LInt / ULInt / Real / LReal / " +
|
|
"String / WString / Time / Date / DateTime / TimeOfDay (default DInt).")]
|
|
public TwinCATDataType DataType { get; init; } = TwinCATDataType.DInt;
|
|
|
|
/// <summary>Gets the element count for an array read; null reads a scalar.</summary>
|
|
[CommandOption("array-length", Description =
|
|
"Read the symbol as a 1-D array of this many --type elements (the leading sub-range when shorter).")]
|
|
public int? ArrayLength { get; init; }
|
|
|
|
/// <inheritdoc />
|
|
public override async ValueTask ExecuteAsync(IConsole console)
|
|
{
|
|
Validate();
|
|
ConfigureLogging();
|
|
var ct = console.RegisterCancellationHandler();
|
|
|
|
var tagName = SynthesiseTagName(SymbolPath, DataType);
|
|
var tag = new TwinCATTagDefinition(
|
|
Name: tagName,
|
|
DeviceHostAddress: Gateway,
|
|
SymbolPath: SymbolPath,
|
|
DataType: DataType,
|
|
Writable: false,
|
|
ArrayLength: ArrayLength);
|
|
var options = BuildOptions([tag]);
|
|
|
|
await using var driver = new TwinCATDriver(options, DriverInstanceId);
|
|
try
|
|
{
|
|
await driver.InitializeAsync("{}", ct);
|
|
var snapshot = await driver.ReadAsync([tagName], ct);
|
|
await console.Output.WriteLineAsync(SnapshotFormatter.Format(SymbolPath, snapshot[0]));
|
|
if (snapshot[0].Value is Array elements)
|
|
await console.Output.WriteLineAsync("Elements: [" + string.Join(", ",
|
|
elements.Cast<object?>().Select(e => Convert.ToString(e, System.Globalization.CultureInfo.InvariantCulture))) + "]");
|
|
}
|
|
finally
|
|
{
|
|
await driver.ShutdownAsync(CancellationToken.None);
|
|
FlushLogging();
|
|
}
|
|
}
|
|
|
|
/// <summary>Synthesizes an internal tag name from a symbol path and data type.</summary>
|
|
/// <param name="symbolPath">The TwinCAT symbol path.</param>
|
|
/// <param name="type">The data type of the symbol.</param>
|
|
/// <returns>A synthesized tag name combining the symbol path and type.</returns>
|
|
internal static string SynthesiseTagName(string symbolPath, TwinCATDataType type)
|
|
=> $"{symbolPath}:{type}";
|
|
}
|