framer

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package framer builds CAN frames from encoded PGN payloads. It is the inverse of the read-side frame parsing in internal/adapter: where adapter.NewPacketInfo extracts NMEA 2000 metadata from a CAN ID, this package constructs CAN IDs and frames for transmission.

Index

Constants

View Source
const (
	// PGNISOAddressClaim is the ISO 11783 Address Claim PGN.
	PGNISOAddressClaim uint32 = 60928
	// PGNISORequest is the ISO Request PGN.
	PGNISORequest uint32 = 59904
)

Protocol PGNs shared across the read/write pipeline.

View Source
const BroadcastAddr uint8 = 255

BroadcastAddr is the NMEA 2000 broadcast destination address.

Variables

This section is empty.

Functions

func BuildCANID

func BuildCANID(pgn uint32, priority uint8, source uint8, destination uint8) uint32

BuildCANID constructs a 29-bit extended CAN ID from NMEA 2000 parameters.

CAN ID bit layout (29 bits total, extended frame format):

Bits 28-26: Priority (3 bits, 0-7, lower = higher priority)
Bit  25:    Reserved
Bit  24:    Data Page (DP)
Bits 23-16: PDU Format (PF) - determines if message is broadcast or addressed
Bits 15-8:  PDU Specific (PS) - destination address (if PF < 240) or group extension (if PF >= 240)
Bits 7-0:   Source Address (SA) - the sender's address on the bus

For PDU2 (broadcast) PGNs where PF >= 240, the PGN encodes both PF and PS fields. For PDU1 (addressed) PGNs where PF < 240, the PGN's low byte is zero and the destination address is placed into the PS field (bits 15-8 of the CAN ID).

Parameters:

  • pgn: The Parameter Group Number (18-bit). For PDU1 PGNs, the low byte must be 0.
  • priority: Message priority (0-7). Only the lower 3 bits are used.
  • source: Source address of the transmitting device (0-251; 252-255 are reserved/special in NMEA 2000).
  • destination: Destination address for PDU1 (addressed) PGNs. Ignored for PDU2 (broadcast) PGNs.

Returns the constructed 29-bit CAN ID.

func FastPacketHeader

func FastPacketHeader(seqID uint8, frameNum uint8) byte

FastPacketHeader builds a fast-packet header byte from a sequence ID and frame number. Inverse of FastPacketSeqFrame.

func FastPacketSeqFrame

func FastPacketSeqFrame(headerByte byte) (seqID uint8, frameNum uint8)

FastPacketSeqFrame splits a fast-packet header byte into its 3-bit sequence ID (bits 7-5) and 5-bit frame number (bits 4-0).

func FrameFastPacket

func FrameFastPacket(canID uint32, payload []byte, sequenceId uint8) []can.Frame

FrameFastPacket creates multiple CAN frames for fast-packet PGNs whose payload exceeds 8 bytes. The fast-packet protocol splits the payload across multiple CAN frames:

Frame 0:

  • Data[0] = (sequenceId << 5) | 0 (frame counter = 0)
  • Data[1] = total payload length
  • Data[2:8] = first 6 bytes of payload

Continuation frames (1..N):

  • Data[0] = (sequenceId << 5) | frameNumber
  • Data[1:8] = next 7 bytes of payload

The sequence ID is a 3-bit counter (0-7) managed by the caller to distinguish concurrent transmissions of the same PGN. Unused bytes in the last frame are padded with 0xFF.

Parameters:

  • canID: The 29-bit CAN ID constructed by BuildCANID.
  • payload: The raw PGN payload bytes (up to 223 bytes).
  • sequenceId: A 3-bit sequence counter (0-7) provided by the caller.

Returns a slice of can.Frame values ready for sequential transmission.

func FrameSingle

func FrameSingle(canID uint32, payload []byte) can.Frame

FrameSingle creates a single CAN frame for payloads of 8 bytes or fewer. Payloads shorter than 8 bytes are padded with 0xFF, which is the NMEA 2000 convention for unused bytes.

Parameters:

  • canID: The 29-bit CAN ID constructed by BuildCANID.
  • payload: The raw PGN payload bytes (must be <= 8 bytes).

Returns a can.Frame ready for transmission.

Types

type CANID

type CANID struct {
	PGN         uint32
	Priority    uint8
	Source      uint8
	Destination uint8
	Addressed   bool
}

CANID is the decomposed form of a 29-bit extended CAN identifier. ParseCANID and BuildCANID are inverses for the fields they share.

func ParseCANID

func ParseCANID(id uint32) CANID

ParseCANID extracts the NMEA 2000 fields from a 29-bit CAN ID. For PDU1 (addressed) PGNs the low byte of the raw PGN field is the destination address; it is masked off the returned PGN. For PDU2 (broadcast) PGNs the destination is BroadcastAddr.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL