Sum-read (#310): batches a poll's reads per device into sum commands capped at sumReadMaxItems, per-item ADS errors mapped per tag, falls back to individual reads when the target refuses the command. Diagnostics (#314): ADS state, app name, online-change count, task cycle/exec/jitter via IDriverDiagnostics. Re-implemented against the current raw-byte read path (stranded auto/driver-gaps commits predate it). Sum-write not ported. Unit tests only; live gate pending a route. Claude-Session: https://claude.ai/code/session_01Ef2ozxS7QPanGT8uoATbTe
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Runtime.CompilerServices;
|
||||
using System.Threading.Channels;
|
||||
using TwinCAT;
|
||||
using TwinCAT.Ads;
|
||||
using TwinCAT.Ads.SumCommand;
|
||||
using TwinCAT.Ads.TypeSystem;
|
||||
using TwinCAT.TypeSystem;
|
||||
|
||||
@@ -166,6 +167,151 @@ internal sealed class AdsTwinCATClient : ITwinCATClient
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<IReadOnlyList<(object? value, uint status)>> ReadValuesAsync(
|
||||
IReadOnlyList<TwinCATBulkReadItem> reads, CancellationToken cancellationToken)
|
||||
{
|
||||
var results = new (object? value, uint status)[reads.Count];
|
||||
var planned = new List<PlannedSumRead>(reads.Count);
|
||||
|
||||
// Plan every item exactly as the single read would (symbol resolved from the Beckhoff symbol cache,
|
||||
// declared type checked against it), but stop short of the wire read: those become ONE sum command.
|
||||
// Anything the sum command cannot carry is read on its own right here.
|
||||
for (var i = 0; i < reads.Count; i++)
|
||||
{
|
||||
var r = reads[i];
|
||||
try
|
||||
{
|
||||
if (r.BitIndex is not null && r.Type == TwinCATDataType.Bool)
|
||||
{
|
||||
results[i] = await ReadValueAsync(r.SymbolPath, r.Type, r.BitIndex, r.ArrayCount, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
var (shape, array, symbol, symbolStatus) = await DescribeSymbolAsync(r.SymbolPath, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (symbolStatus != TwinCATStatusMapper.Good) { results[i] = (null, symbolStatus); continue; }
|
||||
|
||||
TwinCATArrayLayout layout;
|
||||
if (r.ArrayCount is int count)
|
||||
{
|
||||
if (TwinCATTypeGuard.CheckArray(r.Type, count, shape, array, out layout) is not null)
|
||||
{
|
||||
results[i] = (null, TwinCATStatusMapper.BadTypeMismatch);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (TwinCATTypeGuard.CheckRead(r.Type, shape) is not null)
|
||||
{
|
||||
results[i] = (null, TwinCATStatusMapper.BadTypeMismatch);
|
||||
continue;
|
||||
}
|
||||
// A scalar is a one-element "array" at the type's own width (a STRING at the PLC's declared
|
||||
// width), so scalar and array share one raw decode.
|
||||
layout = new TwinCATArrayLayout(1, r.Type is TwinCATDataType.String or TwinCATDataType.WString
|
||||
? shape.ByteSize
|
||||
: TwinCATTypeGuard.DeclaredByteSize(r.Type));
|
||||
}
|
||||
|
||||
// A handle-addressed symbol (0xF005) needs a fresh variable handle per read — not expressible
|
||||
// as an index-group/offset sum entry. Read it individually.
|
||||
if (symbol!.IndexGroup == SymbolValueByHandle)
|
||||
{
|
||||
results[i] = await ReadValueAsync(r.SymbolPath, r.Type, r.BitIndex, r.ArrayCount, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
continue;
|
||||
}
|
||||
|
||||
planned.Add(new PlannedSumRead(i, symbol.IndexGroup, symbol.IndexOffset, layout, r.ArrayCount is not null));
|
||||
}
|
||||
catch (AdsErrorException ex)
|
||||
{
|
||||
results[i] = (null, MapAndSignal((uint)ex.ErrorCode));
|
||||
}
|
||||
}
|
||||
|
||||
if (planned.Count == 0) return results;
|
||||
|
||||
ResultSumReadRaw sum;
|
||||
try
|
||||
{
|
||||
var entities = new List<IgIoSumReadEntity>(planned.Count);
|
||||
foreach (var p in planned)
|
||||
entities.Add(new IgIoSumReadEntity(p.IndexGroup, p.IndexOffset, p.Layout.ByteLength));
|
||||
sum = await new SumRead(_client, entities, SumCommandErrorStrategy.Relaxed)
|
||||
.ReadRawAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
catch (AdsErrorException ex) when (IsSumRefusal((uint)ex.ErrorCode))
|
||||
{
|
||||
throw new TwinCATSumReadRefusedException((uint)ex.ErrorCode, ex);
|
||||
}
|
||||
catch (AdsSumCommandException)
|
||||
{
|
||||
// The library gave up on the batch without an overall ADS code (a sub-item failure under its
|
||||
// strict handling). Not a refusal of the service: read these items one by one this time.
|
||||
foreach (var p in planned)
|
||||
results[p.Index] = await ReadValueAsync(reads[p.Index].SymbolPath, reads[p.Index].Type,
|
||||
reads[p.Index].BitIndex, reads[p.Index].ArrayCount, cancellationToken).ConfigureAwait(false);
|
||||
return results;
|
||||
}
|
||||
|
||||
// The overall code is the sum command itself; per-item codes are in SubErrors.
|
||||
if (sum.ErrorCode != AdsErrorCode.NoError)
|
||||
{
|
||||
var overall = (uint)sum.ErrorCode;
|
||||
if (IsSumRefusal(overall))
|
||||
throw new TwinCATSumReadRefusedException(overall);
|
||||
var mapped = MapAndSignal(overall);
|
||||
foreach (var p in planned) results[p.Index] = (null, mapped);
|
||||
return results;
|
||||
}
|
||||
|
||||
for (var k = 0; k < planned.Count; k++)
|
||||
{
|
||||
var p = planned[k];
|
||||
var subError = sum.SubErrors is { } errs && k < errs.Length ? errs[k] : AdsErrorCode.NoError;
|
||||
if (subError != AdsErrorCode.NoError)
|
||||
{
|
||||
results[p.Index] = (null, MapAndSignal((uint)subError));
|
||||
continue;
|
||||
}
|
||||
if (sum.ReadBlocks is not { } blocks || k >= blocks.Count || blocks[k].Length < p.Layout.ByteLength)
|
||||
{
|
||||
// A short block would decode garbage; treat it as a size mismatch, not a value.
|
||||
results[p.Index] = (null, TwinCATStatusMapper.MapAdsError(1797));
|
||||
continue;
|
||||
}
|
||||
var decoded = TwinCATArrayCodec.Decode(reads[p.Index].Type, sum.ReadBlocks![k].Span, p.Layout, StringsAreUtf8());
|
||||
results[p.Index] = (p.IsArray ? decoded : decoded.GetValue(0), TwinCATStatusMapper.Good);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
private const uint SymbolValueByHandle = 0xF005;
|
||||
|
||||
private readonly record struct PlannedSumRead(int Index, uint IndexGroup, uint IndexOffset, TwinCATArrayLayout Layout, bool IsArray);
|
||||
|
||||
/// <summary>
|
||||
/// True for an overall ADS code that means "this target does not do sum commands" (service / index
|
||||
/// group / offset / size not supported) rather than a comms fault.
|
||||
/// </summary>
|
||||
internal static bool IsSumRefusal(uint adsError) => adsError is 1792 or 1793 or 1794 or 1795 or 1797 or 1798;
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<string?> ReadAdsStateAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var state = await _client.ReadStateAsync(cancellationToken).ConfigureAwait(false);
|
||||
return state.ErrorCode == AdsErrorCode.NoError ? state.State.AdsState.ToString() : null;
|
||||
}
|
||||
catch (OperationCanceledException) { throw; }
|
||||
catch { return null; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resolves the PLC symbol behind <paramref name="symbolPath"/> into the facts
|
||||
/// <see cref="TwinCATTypeGuard"/> compares a declared type against. Uses the Beckhoff symbol
|
||||
|
||||
Reference in New Issue
Block a user