Table of Contents

Class Can

Namespace
Pamoja.Can
Assembly
Pamoja.Can.dll

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

priority J1939Priority

The message priority, 0 (highest) to 7.

pgn uint

The parameter group number.

source byte

The 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

priority byte

The message priority, 0 (highest) to 7.

pgn uint

The parameter group number.

source byte

The address of the sending node.

destination byte

The 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

id uint

The identifier as it arrived.

extended bool

Whether it is a 29-bit extended identifier.

Returns

J1939Message

The decoded message, or null for 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

dlc byte

The 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

id uint

The arbitration identifier.

data ReadOnlySpan<byte>

The payload, at one of the discrete CAN-FD lengths: 0 to 8, then 12, 16, 20, 24, 32, 48, or 64 bytes.

extended bool

Whether 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

id uint

The arbitration identifier, masked to the width selected.

data ReadOnlySpan<byte>

The payload, at most eight bytes.

extended bool

Whether 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

length int

The 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

id uint

The arbitration identifier.

length int

The data length being requested, clamped to eight bytes.

extended bool

Whether 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.