Class Can
CAN bus framing, and the J1939 identifier that rides on top of it.
public static class Can
- Inheritance
-
Can
- Inherited Members
Remarks
CAN is how the moving parts of a machine talk to each other: motor controllers, servos, battery management, and the engines and farm equipment that speak J1939. This is the identifier and payload layer; the controller hardware handles the wire itself.
Methods
BroadcastJ1939(J1939Priority, uint, byte)
Composes the identifier of a J1939 broadcast, which every node reads.
public static uint BroadcastJ1939(J1939Priority priority, uint pgn, byte source)
Parameters
priorityJ1939PriorityThe message priority, 0 (highest) to 7.
pgnuintThe parameter group number.
sourcebyteThe address of the sending node.
Returns
- uint
The 29-bit identifier.
Remarks
Most parameter groups are broadcast, so this is the common case; it saves a
caller knowing that a broadcast is addressed to 0xFF.
ComposeJ1939(byte, uint, byte, byte)
Composes the extended CAN identifier a set of J1939 fields describes.
public static uint ComposeJ1939(byte priority, uint pgn, byte source, byte destination = 0)
Parameters
prioritybyteThe message priority, 0 (highest) to 7.
pgnuintThe parameter group number.
sourcebyteThe address of the sending node.
destinationbyteThe destination address, used only for an addressed (PDU1) parameter group and ignored for a broadcast (PDU2) one.
Returns
- uint
The 29-bit identifier.
DecodeJ1939(uint, bool)
Decodes the J1939 fields out of an extended CAN identifier.
public static J1939Message? DecodeJ1939(uint id, bool extended = true)
Parameters
Returns
- J1939Message
The decoded message, or
nullfor a standard identifier, which J1939 does not use.
DlcToLen(byte)
Returns the payload length a data length code encodes.
public static int DlcToLen(byte dlc)
Parameters
dlcbyteThe data length code.
Returns
- int
The length in bytes.
FdFrame(uint, ReadOnlySpan<byte>, bool)
Builds a CAN-FD frame, which carries up to 64 bytes.
public static CanFrame FdFrame(uint id, ReadOnlySpan<byte> data, bool extended = false)
Parameters
iduintThe arbitration identifier.
dataReadOnlySpan<byte>The payload, at one of the discrete CAN-FD lengths: 0 to 8, then 12, 16, 20, 24, 32, 48, or 64 bytes.
extendedboolWhether the identifier is a 29-bit extended one.
Returns
- CanFrame
The frame.
Exceptions
- PamojaException
The payload length is not one CAN-FD can carry.
Frame(uint, ReadOnlySpan<byte>, bool)
Builds a classic CAN 2.0 frame.
public static CanFrame Frame(uint id, ReadOnlySpan<byte> data, bool extended = false)
Parameters
iduintThe arbitration identifier, masked to the width selected.
dataReadOnlySpan<byte>The payload, at most eight bytes.
extendedboolWhether the identifier is a 29-bit extended one.
Returns
- CanFrame
The frame.
Exceptions
- PamojaException
The payload is longer than a classic frame carries.
LenToDlc(int)
Returns the data length code that encodes a payload length.
public static byte LenToDlc(int length)
Parameters
lengthintThe payload length in bytes.
Returns
- byte
The code, rounding up to the next length CAN-FD can carry.
RemoteFrame(uint, int, bool)
Builds a remote transmission request, which asks another node to send.
public static CanFrame RemoteFrame(uint id, int length, bool extended = false)
Parameters
iduintThe arbitration identifier.
lengthintThe data length being requested, clamped to eight bytes.
extendedboolWhether the identifier is a 29-bit extended one.
Returns
- CanFrame
The frame, which carries no payload of its own.
Exceptions
- PamojaException
The native call failed.