fix(focas): decode cnc_rdmacro dec_val at offset 6; vacant/fractional macros go Bad
The ODBM wire payload is [i32 mantissa][i16 length, a constant 10][i16 dec_val]. ReadMacroAsync read the decimal at offset 4, so every macro decoded without failing but wrongly: the captured #500 = 1.0 (05 f5 e1 00 00 0a 00 08) published 0.01. FANUC normalises macros to a 9-significant-digit mantissa, so the error was never small. - dec_val is read at [6..7]; a payload < 8 bytes is refused, not guessed at. - A vacant variable (dec_val 0xFFFF) publishes BadNoData, never a Good 0. - Integer-typed tags get the exact whole value (integer arithmetic); a fractional macro on an integer tag is BadTypeMismatch, a whole value that does not fit the type BadOutOfRange. Float tags get mantissa / 10^dec. Ported from the sister ignitionoee FOCAS driver v0.15.0 (Focas.Wire-013, Focas-017/018); live vectors from Makino Z-34184 (31i-B, G431). Adds a WireFixtures helper that loads the raw 31i-B captures in Fixtures/v3.
This commit is contained in:
@@ -19,6 +19,14 @@ public static class FocasStatusMapper
|
||||
public const uint BadTimeout = 0x800A0000u;
|
||||
public const uint BadTypeMismatch = 0x80740000u;
|
||||
|
||||
/// <summary>
|
||||
/// The CNC answered, but its answer describes no usable value — a vacant macro variable, a
|
||||
/// position / meter record whose own scale envelope is undecodable, a field the control did
|
||||
/// not report. Distinct from a comms fault (the device is fine) and never a fabricated
|
||||
/// Good <c>0</c>.
|
||||
/// </summary>
|
||||
public const uint BadNoData = 0x809B0000u;
|
||||
|
||||
/// <summary>
|
||||
/// Map common FWLIB <c>EW_*</c> return codes. The values below match Fanuc's published
|
||||
/// numeric conventions (EW_OK=0, EW_FUNC=1, EW_NUMBER=3, EW_LENGTH=4, EW_ATTRIB=7,
|
||||
|
||||
@@ -577,9 +577,33 @@ public sealed class FocasWireClient : IAsyncDisposable, IDisposable
|
||||
ushort? pathId = null)
|
||||
=> ReadSingleWithTimeoutAsync(
|
||||
0x0015,
|
||||
payload => new WireMacro(number, payload.Length >= 4 ? ReadInt32(payload, 0) : 0, payload.Length >= 6 ? ReadInt16(payload, 4) : (short)0),
|
||||
payload => ParseMacro(number, payload),
|
||||
cancellationToken, timeout, EffectivePathId(pathId), number, number);
|
||||
|
||||
/// <summary>
|
||||
/// Decode a <c>cnc_rdmacro</c> (0x0015) payload. The on-wire <c>ODBM</c> record is 8
|
||||
/// big-endian bytes: <c>[0..3] i32 mantissa</c>, <c>[4..5] i16</c> length field (a
|
||||
/// <b>constant 10</b> on every observed response), <c>[6..7] i16 dec_val</c>
|
||||
/// (<c>0xFFFF</c> = vacant).
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The middle field is the trap: it is not the decimal count but looks like a plausible one,
|
||||
/// so reading the decimal at offset 4 decoded every macro on every control without ever
|
||||
/// failing — just wrongly (the captured <c>#500 = 1.0</c> published <c>0.01</c>). Pinned by
|
||||
/// a live Makino 31i-B capture; found and fixed by the sister ignitionoee driver
|
||||
/// (Focas.Wire-013, 2026-08-05). A payload shorter than 8 bytes carries no
|
||||
/// <c>dec_val</c> and is refused rather than guessed at.
|
||||
/// </remarks>
|
||||
/// <param name="number">The macro number the request asked for.</param>
|
||||
/// <param name="payload">The response-block payload.</param>
|
||||
/// <returns>The decoded macro.</returns>
|
||||
/// <exception cref="FocasWireException">The payload is shorter than 8 bytes.</exception>
|
||||
internal static WireMacro ParseMacro(short number, byte[] payload)
|
||||
{
|
||||
RequireLength(payload, 8, "cnc_rdmacro");
|
||||
return new WireMacro(number, ReadInt32(payload, 0), ReadInt16(payload, 6));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a PMC range via <c>pmc_rdpmcrng</c>. <paramref name="area"/> is the numeric
|
||||
/// address-letter code (see <see cref="FocasPmcArea"/>); <paramref name="dataType"/>
|
||||
|
||||
@@ -86,13 +86,31 @@ public sealed record WireParameter(
|
||||
int Value);
|
||||
|
||||
/// <summary>
|
||||
/// Macro variable from <c>cnc_rdmacro</c>. Scaled decimal: the callable value is
|
||||
/// <c>Value / 10^Decimal</c>.
|
||||
/// Macro variable from <c>cnc_rdmacro</c>. Scaled decimal: the real value is
|
||||
/// <c>Value / 10^Decimal</c> (<see cref="Scaled"/>). FANUC normalises every macro to a
|
||||
/// 9-significant-digit mantissa, so <see cref="Value"/> alone is never a value to publish
|
||||
/// (live: <c>#502 = 270</c> arrives as <c>270000000 / 10^6</c>).
|
||||
/// </summary>
|
||||
/// <param name="Number">Macro variable number (the request argument; not carried in the payload).</param>
|
||||
/// <param name="Value">The 9-significant-digit mantissa.</param>
|
||||
/// <param name="Decimal">Implied decimal places; negative (<c>0xFFFF</c> on the wire) = vacant.</param>
|
||||
public sealed record WireMacro(
|
||||
short Number,
|
||||
int Value,
|
||||
short Decimal);
|
||||
short Decimal)
|
||||
{
|
||||
/// <summary>
|
||||
/// True when the CNC reported the variable as vacant (undefined / never set):
|
||||
/// <c>dec_val == 0xFFFF</c>. A vacant macro's mantissa is 0, indistinguishable from a
|
||||
/// genuine 0, so this must be tested before <see cref="Value"/> means anything. Any
|
||||
/// negative decimal is treated as vacant — no other negative has ever been observed and
|
||||
/// failing closed is the safe reading.
|
||||
/// </summary>
|
||||
public bool IsVacant => Decimal < 0;
|
||||
|
||||
/// <summary>The real value, <c>Value / 10^Decimal</c>; null when <see cref="IsVacant"/>.</summary>
|
||||
public double? Scaled => IsVacant ? null : Decimal == 0 ? Value : Value / Math.Pow(10.0, Decimal);
|
||||
}
|
||||
|
||||
/// <summary>PMC range read-back from <c>pmc_rdpmcrng</c>: one or more values of the requested width.</summary>
|
||||
public sealed record WirePmcRange(
|
||||
|
||||
@@ -62,7 +62,7 @@ public sealed class WireFocasClient : IFocasClient
|
||||
{
|
||||
FocasAreaKind.Pmc => await ReadPmcAsync(address, type, cancellationToken).ConfigureAwait(false),
|
||||
FocasAreaKind.Parameter => await ReadParameterAsync(address, type, cancellationToken).ConfigureAwait(false),
|
||||
FocasAreaKind.Macro => await ReadMacroAsync(address, cancellationToken).ConfigureAwait(false),
|
||||
FocasAreaKind.Macro => await ReadMacroAsync(address, type, cancellationToken).ConfigureAwait(false),
|
||||
_ => (null, FocasStatusMapper.BadNotSupported),
|
||||
};
|
||||
}
|
||||
@@ -368,17 +368,14 @@ public sealed class WireFocasClient : IFocasClient
|
||||
}
|
||||
|
||||
private async Task<(object? value, uint status)> ReadMacroAsync(
|
||||
FocasAddress address, CancellationToken cancellationToken)
|
||||
FocasAddress address, FocasDataType type, CancellationToken cancellationToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var result = await _wire.ReadMacroAsync((short)address.Number, cancellationToken).ConfigureAwait(false);
|
||||
if (!result.IsOk || result.Value is null)
|
||||
return (null, FocasStatusMapper.MapFocasReturn(result.Rc));
|
||||
var m = result.Value;
|
||||
// Macro value is scaled-decimal: the real value is Value / 10^Decimal.
|
||||
var scaled = m.Value / Math.Pow(10.0, m.Decimal);
|
||||
return ((object)scaled, FocasStatusMapper.Good);
|
||||
return MapMacro(result.Value, type, address.BitIndex);
|
||||
}
|
||||
catch (FocasWireException ex)
|
||||
{
|
||||
@@ -386,6 +383,62 @@ public sealed class WireFocasClient : IFocasClient
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turn a decoded macro into the value + quality its tag publishes.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>A <b>vacant</b> variable has no value at all → <see cref="FocasStatusMapper.BadNoData"/>,
|
||||
/// never a Good <c>0</c> (its mantissa is 0, indistinguishable from a genuine zero).</para>
|
||||
/// <para>Float tags get <c>mantissa / 10^decimal</c>. <b>Integer</b> tags (Byte / Int16 /
|
||||
/// Int32 / Bit) get the exact whole value, tested with integer arithmetic
|
||||
/// (<c>mantissa % 10^decimal</c>) so no epsilon is involved; a fractional macro on an
|
||||
/// integer tag is <see cref="FocasStatusMapper.BadTypeMismatch"/> — truncating or rounding
|
||||
/// would publish a plausible wrong count at Good quality — and a whole value that does not
|
||||
/// fit the type is <see cref="FocasStatusMapper.BadOutOfRange"/>. Ported from the sister
|
||||
/// ignitionoee driver's <c>macroInteger</c>/<c>macroReal</c> (Focas-017/018).</para>
|
||||
/// </remarks>
|
||||
/// <param name="m">The decoded macro.</param>
|
||||
/// <param name="type">The tag's declared data type.</param>
|
||||
/// <param name="bitIndex">Optional bit index for a <see cref="FocasDataType.Bit"/> tag.</param>
|
||||
/// <returns>The boxed value (null when Bad) and its status.</returns>
|
||||
internal static (object? value, uint status) MapMacro(WireMacro m, FocasDataType type, int? bitIndex)
|
||||
{
|
||||
if (m.Scaled is not { } scaled) return (null, FocasStatusMapper.BadNoData);
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case FocasDataType.Float64:
|
||||
return (scaled, FocasStatusMapper.Good);
|
||||
case FocasDataType.Float32:
|
||||
return ((float)scaled, FocasStatusMapper.Good);
|
||||
case FocasDataType.Byte or FocasDataType.Int16 or FocasDataType.Int32 or FocasDataType.Bit:
|
||||
if (!TryWholeMacroValue(m, out var whole)) return (null, FocasStatusMapper.BadTypeMismatch);
|
||||
return type switch
|
||||
{
|
||||
FocasDataType.Bit => (bitIndex is int bit ? ((whole >> bit) & 1L) != 0 : whole != 0, FocasStatusMapper.Good),
|
||||
FocasDataType.Byte when whole is >= sbyte.MinValue and <= sbyte.MaxValue => ((sbyte)whole, FocasStatusMapper.Good),
|
||||
FocasDataType.Int16 when whole is >= short.MinValue and <= short.MaxValue => ((short)whole, FocasStatusMapper.Good),
|
||||
FocasDataType.Int32 when whole is >= int.MinValue and <= int.MaxValue => ((int)whole, FocasStatusMapper.Good),
|
||||
_ => (null, FocasStatusMapper.BadOutOfRange),
|
||||
};
|
||||
default:
|
||||
return (scaled, FocasStatusMapper.Good);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Exact <c>mantissa / 10^decimal</c> when it is a whole number; integer arithmetic only.</summary>
|
||||
private static bool TryWholeMacroValue(WireMacro m, out long whole)
|
||||
{
|
||||
whole = 0;
|
||||
if (m.Value == 0) return true; // 0 is whole at any decimal
|
||||
if (m.Decimal > 18) return false; // an i32 mantissa over 10^19+ is always fractional
|
||||
var divisor = 1L;
|
||||
for (var i = 0; i < m.Decimal; i++) divisor *= 10L;
|
||||
if (m.Value % divisor != 0) return false;
|
||||
whole = m.Value / divisor;
|
||||
return true;
|
||||
}
|
||||
|
||||
private void RequireConnected()
|
||||
{
|
||||
if (!_wire.IsConnected)
|
||||
|
||||
Reference in New Issue
Block a user