Skip to main content

CanFrame

Struct CanFrame 

Source
pub struct CanFrame<'a> { /* private fields */ }
Expand description

A CAN 2.0 or CAN FD frame — a borrowed, zero-copy view over a payload.

Construct with CanFrame::new (classic data), CanFrame::fd (CAN FD data), CanFrame::remote (classic RTR), or the fully explicit CanFrame::from_parts; parse from SocketCAN bytes with CanFrame::parse; serialize with CanFrame::to_bytes.

use can_core::{CanFrame, CanId};

let id = CanId::standard(0x123).unwrap();
let payload = [0xDE, 0xAD, 0xBE, 0xEF];
let frame = CanFrame::new(id, &payload).unwrap();

let bytes = frame.to_bytes();
assert_eq!(bytes.len(), 16); // struct can_frame
assert_eq!(&bytes[8..12], &payload);

let reparsed = CanFrame::parse(&bytes).unwrap();
assert_eq!(frame, reparsed);

Implementations§

Source§

impl<'a> CanFrame<'a>

Source

pub fn new(id: CanId, data: &'a [u8]) -> Result<Self, CanError>

Builds a classic CAN 2.0 data frame; the DLC is the payload length.

§Errors

CanError::DataTooLong for payloads over 8 bytes, and CanError::InvalidIdLength for an out-of-range id.

Source

pub fn fd( id: CanId, data: &'a [u8], brs: bool, esi: bool, ) -> Result<Self, CanError>

Builds a CAN FD data frame.

§Errors

CanError::DataTooLong for payloads that are not DLC-representable (or exceed 64 bytes), and CanError::InvalidIdLength for an out-of-range id.

Source

pub fn remote(id: CanId, dlc: u8) -> Result<Self, CanError>

Builds a classic remote-transmission-request frame: no payload, a DLC of dlc (the requested length). DLC 9–15 models the legacy SocketCAN refinement (len8_dlc) for remote frames requesting more than 8 bytes.

§Errors

CanError::InvalidDlc when dlc > 15, CanError::InvalidIdLength for an out-of-range id.

Source

pub fn from_parts( id: CanId, data: &'a [u8], dlc: u8, is_remote: bool, is_fd: bool, brs: bool, error_state_indicator: bool, ) -> Result<Self, CanError>

Fully explicit constructor — every field is validated.

§Errors

All CanError variants are reachable, depending on the field combination: id range, payload/DLC consistency, FD-vs-classic flag constraints, and the remote-frame rules (remote frames carry no data; CAN FD frames cannot be remote).

Source

pub fn parse(buf: &'a [u8]) -> Result<Self, CanError>

Parses one frame in SocketCAN wire format: exactly 16 bytes (struct can_frame, classic) or exactly 72 bytes (struct canfd_frame, CAN FD). The payload borrows from buf.

§Errors

CanError::BufferTooShort under 16 bytes, CanError::DataTooLong above 16 bytes when not exactly 72, CanError::InvalidIdLength for error-flagged or over-wide id words, CanError::InvalidDlc for out-of-range length bytes, and CanError::RemoteFrameWithData for an RTR flag inside an FD frame.

Source

pub fn to_bytes(&self) -> Vec<u8> ⓘ

Serializes to the SocketCAN wire format: 16 bytes for classic frames (with the RTR flag for remote frames), 72 bytes for CAN FD frames (BRS/ESI in the flags byte).

Source

pub const fn id(&self) -> CanId

The frame’s identifier.

Source

pub const fn data(&self) -> &'a [u8] ⓘ

The payload — empty for remote frames.

Source

pub const fn dlc(&self) -> u8

The Data Length Code (0–8 classic, 0–15 CAN FD).

Source

pub const fn is_remote(&self) -> bool

true for remote-transmission-request frames.

Source

pub const fn is_fd(&self) -> bool

true for CAN FD frames.

Source

pub const fn brs(&self) -> bool

Bit-rate switch: the FD arbitration phase runs at the nominal rate, the data phase at a faster rate. Meaningful only when is_fd.

Source

pub const fn error_state_indicator(&self) -> bool

Error state indicator (FD only): the transmitter’s error-counter state, carried transparently for network error bookkeeping.

Trait Implementations§

Source§

impl<'a> Clone for CanFrame<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for CanFrame<'a>

Source§

impl<'a> Debug for CanFrame<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Eq for CanFrame<'a>

Source§

impl<'a> PartialEq for CanFrame<'a>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<'a> StructuralPartialEq for CanFrame<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for CanFrame<'a>

§

impl<'a> RefUnwindSafe for CanFrame<'a>

§

impl<'a> Send for CanFrame<'a>

§

impl<'a> Sync for CanFrame<'a>

§

impl<'a> Unpin for CanFrame<'a>

§

impl<'a> UnsafeUnpin for CanFrame<'a>

§

impl<'a> UnwindSafe for CanFrame<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.