Skip to content

Commit f6a4c2d

Browse files
committed
can: RTR frames can have a DLC > 0
1 parent 13f9a80 commit f6a4c2d

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/can.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@ impl Frame {
170170
Self::new(Id::new_extended(id), data)
171171
}
172172

173-
/// Sets the remote transmission (RTR) flag. Marks the frame as a remote frame.
173+
/// Marks the frame as a remote frame with configurable data length code (DLC).
174174
///
175175
/// Remote frames do not contain any data, even if the frame was created with a
176176
/// non-empty data buffer.
177-
pub fn with_rtr(&mut self, rtr: bool) -> &mut Self {
178-
self.id = self.id.with_rtr(rtr);
179-
self.dlc = 0;
177+
pub fn with_rtr(&mut self, dlc: usize) -> &mut Self {
178+
self.id = self.id.with_rtr(true);
179+
self.dlc = dlc;
180180
self
181181
}
182182

@@ -205,9 +205,21 @@ impl Frame {
205205
self.id
206206
}
207207

208+
/// Returns the data length code (DLC) which is in the range 0..8.
209+
///
210+
/// For data frames the DLC value always matches the lenght of the data.
211+
/// Remote frames no not carry any data, yet the DLC can be greater than 0.
212+
pub fn dlc(&self) -> usize {
213+
self.dlc
214+
}
215+
208216
/// Returns the frame data (0..8 bytes in length).
209217
pub fn data(&self) -> &[u8] {
210-
&self.data[0..self.dlc]
218+
if self.is_data_frame() {
219+
&self.data[0..self.dlc]
220+
} else {
221+
&[]
222+
}
211223
}
212224
}
213225

0 commit comments

Comments
 (0)