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>
impl<'a> CanFrame<'a>
Sourcepub fn new(id: CanId, data: &'a [u8]) -> Result<Self, CanError>
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.
Sourcepub fn fd(
id: CanId,
data: &'a [u8],
brs: bool,
esi: bool,
) -> Result<Self, CanError>
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.
Sourcepub fn remote(id: CanId, dlc: u8) -> Result<Self, CanError>
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.
Sourcepub 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>
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>
Sourcepub fn parse(buf: &'a [u8]) -> Result<Self, CanError>
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.
Sourcepub fn to_bytes(&self) -> Vec<u8> ⓘ
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).
Sourcepub const fn brs(&self) -> bool
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.
Sourcepub const fn error_state_indicator(&self) -> bool
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.