pub struct CanFrame { /* private fields */ }
Expand description
A CAN frame as transmitted over a CAN socket.
Implementations§
Source§impl CanFrame
impl CanFrame
Sourcepub fn new(id: impl Into<CanId>, data: impl Into<CanData>) -> Self
pub fn new(id: impl Into<CanId>, data: impl Into<CanData>) -> Self
Create a new data from with the given CAN ID and data payload.
To create a new data frame with a potentially invalid ID or data payload,
use Self::try_new()
.
Sourcepub fn try_new<Id, Data>(
id: Id,
data: Data,
) -> Result<Self, TryNewCanFrameError>
pub fn try_new<Id, Data>( id: Id, data: Data, ) -> Result<Self, TryNewCanFrameError>
Create a new data from with the given CAN ID and data payload.
Will report an error if the ID or data is invalid.
You should normally prefer Self::new()
if you can guarantee that the ID and data are valid.
Sourcepub fn new_rtr(id: impl Into<CanId>) -> Self
pub fn new_rtr(id: impl Into<CanId>) -> Self
Create a new remote transmission request (RTR) frame with a data length code of 0.
To set a different data length code, you can call Self::set_data_length_code()
or Self::with_data_length_code()
after constructing the RTR frame.
Sourcepub fn is_rtr(&self) -> bool
pub fn is_rtr(&self) -> bool
Check if this frame is a remote transmission request (an RTR frame).
RTR frames represent a request to transmit a value over the CAN bus. However, an application could decide to use RTR frames differently.
RTR frames have no associated data.
Sourcepub fn data(&self) -> Option<CanData>
pub fn data(&self) -> Option<CanData>
Get the data of the frame.
Returns None
for RTR frames and Some(data)
for data frames.
Sourcepub fn set_data_length_code(
&mut self,
dlc: u8,
) -> Result<(), InvalidDataLengthCode>
pub fn set_data_length_code( &mut self, dlc: u8, ) -> Result<(), InvalidDataLengthCode>
Set the data length code of the frame.
If the data length code is higher than the current data length,
the extra data bytes that become available will have a value of 0
.
If the data length code is in the range 9 to 15 (inclusive), the actual data length of the frame will be set to 8. However, if the CAN controller supports it, it may preserve the given data length code in the frame header.
Sourcepub fn with_data_length_code(
self,
dlc: u8,
) -> Result<Self, InvalidDataLengthCode>
pub fn with_data_length_code( self, dlc: u8, ) -> Result<Self, InvalidDataLengthCode>
Create a copy the frame with a modified data length code.
If the data length code is higher than the current data length,
the extra data bytes that become available will have a value of 0
.
If the data length code is in the range 9 to 15 (inclusive), the actual data length of the frame will be set to 8. However, if the CAN controller supports it, it may preserve the given data length code in the frame header.
Sourcepub fn data_length_code(&self) -> u8
pub fn data_length_code(&self) -> u8
Get the data length code of the frame (it may be higher than the number of data bytes in the frame).
If this is an RTR frame, it is often used to indicate how much bytes are expected in the response data frame. However, the application is free to use the data length code for a different purpose.
The CAN controller may preserve data length codes with a value above 8 (but at most 15).
The data length should normally be assumed to be 8 bytes,
and application is free to interpret the additional values according to it’s own logic.
Note that your CAN controller or driver may not preserve data length codes above 8
.