1 unstable release

0.1.5 Dec 2, 2022
0.1.4 Dec 2, 2022

#11 in #receiving

MIT license

12KB
239 lines

socketcan2-rs

For a documentation please see: https://docs.rs/socketcan2/


lib.rs:

SocketCAN support.

Features

  • Receive can frames
  • Accurate timestamps (timestamps also support multi threading in contrast to receiving the TIMESTAMP via an ioctl call, which does not support mt)
  • epoll-support (allows to wait on multiple CAN devices in the same thread)
  • Send CAN frames (not implemented yet)
  • Filter CAN frames (not implemented yet)

Usage example

#[cfg(test)]
fn on_recv(msg: &Box<Msg>, _user_data: &u64) {
  println!("timestamp: {:?}", msg.timestamp());
  print!("received CAN frame (id: {}): ", msg.can_id());
  for i in 0..msg.len() {
    print!("{} ", msg[i as usize]);
  }
  println!("");
}

#[cfg(test)]
mod tests {
  use super::*;
  #[test]
  fn it_works() {
    let can = Can::open("vcan0").unwrap();
    let mut cg = CanGroup::<u64>::new();
    cg.add(can, 0).unwrap();
    match cg.next(Duration::milliseconds(-1), on_recv) {
      Ok(no_timeout) => if !no_timeout { panic!("timeout"); },
      Err(_) => panic!("error"),
    }
  }
}

Dependencies

~1MB
~19K SLoC