Skip to content

J1939SpnValue Struct

The result of decoding one SPN field: either a physical value, or the reason there is no physical value to report (SAE J1939-71 §5.1.1, SRS FR-J1939-002).

public readonly record struct J1939SpnValue : System.IEquatable<CanKit.Pro.J1939.J1939SpnValue>

Implements IEquatable<J1939SpnValue>

Remarks

Every SPN reserves the top of its raw range for indicators. A 16-bit engine-speed field reading 0xFFFF means "not available", not 8191.875 rpm; 0xFE00..0xFEFF means the sending ECU knows its own reading is wrong. Returning a bare Double cannot distinguish those from a measurement, so extraction returns this type instead and Value throws unless IsValid is true.

Raw is always the field's bit pattern as read off the wire, indicator or not, so an application that wants to log or forward the exact code can still get at it.

Properties

J1939SpnValue.IsError Property

True when the sending ECU reported an error for this parameter.

public bool IsError { get; }

Property Value

Boolean

J1939SpnValue.IsNotAvailable Property

True when the sending ECU does not have this parameter — all bits set for an unsigned SPN, or the sign-bit-clear 0x7F… range for a signed one. See NotAvailable.

public bool IsNotAvailable { get; }

Property Value

Boolean

J1939SpnValue.IsValid Property

True when the field carries a real measurement.

public bool IsValid { get; }

Property Value

Boolean

J1939SpnValue.Kind Property

What the field carries: a measurement or one of the indicator ranges.

public CanKit.Pro.J1939.J1939SpnValueKind Kind { get; }

Property Value

J1939SpnValueKind

J1939SpnValue.Raw Property

The raw field bits as read from the payload, right-aligned and zero-extended — for a signed SPN this is the two's-complement pattern, not the sign-extended number.

public ulong Raw { get; }

Property Value

UInt64

J1939SpnValue.Value Property

The physical value, raw × resolution + offset.

public double Value { get; }

Property Value

Double

Exceptions

InvalidOperationException
The field carries an indicator rather than a measurement. Check IsValid, or use TryGetValue(double) / GetValueOrDefault(double).

Methods

J1939SpnValue.FromIndicator(J1939SpnValueKind, ulong) Method

Creates a value carrying one of the J1939-71 indicator ranges.

public static CanKit.Pro.J1939.J1939SpnValue FromIndicator(CanKit.Pro.J1939.J1939SpnValueKind kind, ulong raw);

Parameters

kind J1939SpnValueKind

The indicator; must not be Valid.

raw UInt64

The field's raw bit pattern.

Returns

J1939SpnValue

Exceptions

ArgumentOutOfRangeException
kind is Valid (use FromPhysical(ulong, double)) or is not a defined J1939SpnValueKind.

J1939SpnValue.FromPhysical(ulong, double) Method

Creates a value carrying a real measurement.

public static CanKit.Pro.J1939.J1939SpnValue FromPhysical(ulong raw, double value);

Parameters

raw UInt64

The field's raw bit pattern.

value Double

The scaled physical value.

Returns

J1939SpnValue

J1939SpnValue.GetValueOrDefault(double) Method

The physical value, or defaultValue when the field carries an indicator. The default default is Double.NaN so that an unguarded arithmetic use of an unavailable reading stays visibly wrong instead of looking plausible.

public double GetValueOrDefault(double defaultValue=double.NaN);

Parameters

defaultValue Double

Returns

Double

J1939SpnValue.TryGetValue(double) Method

Gets the physical value if the field carries one.

public bool TryGetValue(out double value);

Parameters

value Double

The physical value, or 0 when this is an indicator.

Returns

Boolean
true if a measurement was returned.