Skip to main content

Crate kinavis_nmea2000

Crate kinavis_nmea2000 

Source
Expand description

NMEA 2000 parameter groups from CAN frames to the KINAVIS types.

Every NMEA 2000 device (GNSS, compass, sounder, AIS) sends parameter groups: fixed layouts of little-endian fields, carried in one 8-byte frame or, as a fast packet, in up to 32. Starting from frames delivered by the bus interface, this crate:

  1. Parses the identifier — CanId — into the Pgn and source address.
  2. Reassembles fast packets in an Assembler with fixed slots and a timeout, so a lost frame costs one group, not a slot.
  3. Decodes the group into a Message with kernel field types (Position, Speed, TrueCourse, Distance, Instant). “Not available” is None, not the raw all-ones value.

No allocation, no panics on any input; builds for bare-metal targets and CI checks the strict-profile build for panic paths. The bus (CAN controller, driver, address claim) is the caller’s.

use kinavis_nmea2000::{Assembler, CanId, Frame, Message, Pgn};
use kinavis_kernel::{Instant, Utc};

let mut assembler = Assembler::new();
let now = Instant::<Utc>::from_unix_seconds(1_789_000_000);

// Position, rapid update, from address 35: 50.755°N 1.333°W in 1e-7°.
let id = CanId::new(0x09F8_0123)?;
assert_eq!(id.pgn(), Pgn::POSITION_RAPID_UPDATE);
let frame = Frame::new(id, &[0x30, 0x99, 0x40, 0x1E, 0xB0, 0x99, 0x34, 0xFF])?;

let Some(payload) = assembler.push(&frame, now)? else { panic!("in frames") };
let Message::PositionRapidUpdate(update) = Message::decode(&payload)? else {
    panic!("not a position");
};
assert_eq!(format!("{:.3}", update.position.unwrap()), "50°45.300'N 001°19.980'W");

§Supported PGNs

GroupDecoded as
129025 — position, rapid updatePositionRapidUpdate
129026 — COG and SOG, rapid updateCourseAndSpeed
129029 — GNSS position dataGnssPosition, and a kernel GnssFix from it
127250 — vessel headingVesselHeading
128267 — water depthWaterDepth
129038 — AIS class A position reportAisPositionReport
129039 — AIS class B position reportAisPositionReport

Other PGNs pass the assembler only with an explicit transport (Assembler::push_as) and decode as Message::Unsupported with the PGN and the raw Payload.

§Feature flags

  • std (default) — standard library maths in the kernel.
  • libm — for no_std targets: --no-default-features --features libm.
  • serde — serialisation of the messages.

Structs§

AisPositionReport
AIS position report, class A or B.
Assembler
Fast-packet assembler with N slots.
CanId
29-bit extended CAN identifier, J1939 layout.
CourseAndSpeed
Course and speed over ground, rapid update: PGN 129026.
Frame
CAN frame: identifier and up to 8 data bytes.
GnssPosition
GNSS position data, PGN 129029.
Payload
Complete PGN payload (single frame or reassembled fast packet), fixed capacity.
Pgn
Parameter group number.
PositionRapidUpdate
Position, rapid update: PGN 129025.
VesselHeading
Vessel heading: PGN 127250.
WaterDepth
Water depth: PGN 128267.

Enums§

GnssSystem
Satellite system(s) of a fix.
Integrity
Receiver integrity monitoring status.
Message
Decoded parameter group.
Nmea2000Error
Why a frame could not be accepted, reassembled or decoded.
Referenced
Direction with its reference: true or magnetic north.
StationClass
Station class.
Transport
PGN transport on the bus.

Constants§

DEFAULT_TIMEOUT
Default timeout for an incomplete packet. Frames of one packet follow within milliseconds; the standard specifies 750 ms.
MAX_ASSEMBLIES
Default number of concurrent fast-packet assemblies.
MAX_PAYLOAD_BYTES
Maximum fast-packet payload: 6 bytes in the first frame + 7 in each of up to 31 more.