fix(twincat): refuse a scalar tag over a PLC array on the read path too

Live on Z33856 a UInt tag over .ARRFAULTCODE (ARRAY [0..12] OF UINT) made the
Beckhoff read throw - BadCommunicationError and a Degraded driver - while the
native path reported BadTypeMismatch. CheckRead now refuses a scalar declared
over an array (by category, so an alias of an array type is caught), pointing
at the array-tag / element-path alternatives.
This commit is contained in:
Joseph Doherty
2026-10-07 14:50:39 -04:00
parent 1e68121b17
commit 313946e6ea
2 changed files with 34 additions and 8 deletions
@@ -105,6 +105,26 @@ public sealed class TwinCATTypeGuardTests
TwinCATTypeGuard.CheckRead(TwinCATDataType.String, Plc("ST_LOGDATA", 81, DataTypeCategory.Struct)).ShouldNotBeNull();
}
/// <summary>
/// A SCALAR tag over a PLC array has no value the symbol-aware read can convert: live on Z33856
/// (2026-10-07) <c>.ARRFAULTCODE</c> (<c>ARRAY [0..12] OF UINT</c>) read as <c>UInt</c> made the Beckhoff
/// read throw, surfacing as <c>BadCommunicationError</c> and a Degraded driver, while the native path
/// said <c>BadTypeMismatch</c>. Both paths now refuse it with <c>BadTypeMismatch</c> — by category, so an
/// alias of an array type is caught too. Other non-scalars (a pointer read as its address) are left
/// to the read, as before.
/// </summary>
[Fact]
public void Read_refuses_a_scalar_declared_over_a_plc_array()
{
TwinCATTypeGuard.CheckRead(TwinCATDataType.UInt, Plc("ARRAY [0..12] OF UINT", 26, DataTypeCategory.Array))
.ShouldNotBeNull();
TwinCATTypeGuard.CheckRead(TwinCATDataType.DInt, Plc("T_FAULTS", 52, DataTypeCategory.Array)).ShouldNotBeNull();
TwinCATTypeGuard.CheckRead(TwinCATDataType.UDInt, Plc("POINTER TO BYTE", 4, DataTypeCategory.Pointer)).ShouldBeNull();
Plc("ARRAY [0..12] OF UINT", 26, DataTypeCategory.Array).IsArray.ShouldBeTrue();
Plc("ARRAY [0..12] OF UINT", 26).IsArray.ShouldBeTrue(); // by name when no category resolved
Plc("UINT", 2, DataTypeCategory.Primitive).IsArray.ShouldBeFalse();
}
/// <summary>
/// 0.19.2: a STRING / WSTRING tag matches a PLC <c>STRING(n)</c> / <c>WSTRING(n)</c> of ANY length.
/// A numeric-over-numeric pair is left to the symbol-aware read, which converts the value.