pub struct CanBus { /* private fields */ }Expand description
One node’s place on a CAN bus, shared by every clone of this handle.
§Examples
use std::time::Duration;
use pamoja_can::bus::{CanBus, Filter};
use pamoja_can::{Frame, J1939Id, Signals};
// EEC1 carries engine speed, and ET1 the engine's temperatures.
const EEC1: u32 = 61_444;
const ET1: u32 = 65_262;
let engine = CanBus::simulated();
let gateway = engine.join()?;
gateway.set_filters(&[Filter::pgn(EEC1)])?;
// The gateway keeps engine speed and lets the engine's other groups go by. Each frame
// here reports nothing yet, every signal marked not available.
let nothing = Signals::new();
let speed = J1939Id::broadcast(3, EEC1, 0).to_id();
let temperatures = J1939Id::broadcast(6, ET1, 0).to_id();
engine.send(&Frame::new(temperatures, nothing.as_bytes())?)?;
engine.send(&Frame::new(speed, nothing.as_bytes())?)?;
let heard = gateway.receive(Duration::from_millis(10))?.expect("engine speed");
assert_eq!(heard.id(), speed);
assert_eq!(gateway.receive(Duration::from_millis(10))?, None);Implementations§
Source§impl CanBus
impl CanBus
Sourcepub fn open(interface: &str) -> Result<CanBus, OpenError>
pub fn open(interface: &str) -> Result<CanBus, OpenError>
Opens a kernel CAN interface through SocketCAN, as one node on its bus.
The interface is brought up and given its bit rate outside the program, with
ip link set can0 up type can bitrate 250000. The node takes CAN FD frames too where the
interface runs CAN FD.
§Arguments
interface- the interface:can0for the first controller,vcan0for a virtual one.
§Returns
The node.
§Errors
OpenError::Unsupported anywhere but Linux, or in a build without the linux
feature, and OpenError::Device when the interface does not exist or cannot be bound.
§Examples
use pamoja_can::bus::CanBus;
let bus = CanBus::open("can0")?;Sourcepub fn join(&self) -> Result<CanBus, OpenError>
pub fn join(&self) -> Result<CanBus, OpenError>
Puts another node on the same bus: another socket on the same interface, or another node on the same simulated bus. It hears what this node sends, and this node hears it.
§Returns
The new node.
§Errors
OpenError::Device when the kernel refuses another socket on the interface.
Sourcepub fn kind(&self) -> CanBusKind
pub fn kind(&self) -> CanBusKind
Returns what the bus is.
Sourcepub fn interface(&self) -> Option<String>
pub fn interface(&self) -> Option<String>
Returns the kernel interface the node is on, or None on a simulated bus.
Sourcepub fn send(&self, frame: &Frame) -> Result<(), BusError>
pub fn send(&self, frame: &Frame) -> Result<(), BusError>
Sends a frame to every other node on the bus.
§Arguments
frame- the frame.
§Errors
BusError::Device when the kernel refuses the frame or the interface fails.
Sourcepub fn receive(&self, timeout: Duration) -> Result<Option<Frame>, BusError>
pub fn receive(&self, timeout: Duration) -> Result<Option<Frame>, BusError>
Takes the next frame this node keeps, waiting up to timeout for one when none has
arrived.
On a simulated bus nothing waits: with no frame there it returns at once, and the
timeout is added to CanBus::waited_micros.
§Arguments
timeout- how long to wait.
§Returns
The frame, or None when the timeout passed with nothing.
§Errors
BusError::Device when the interface fails.
Sourcepub fn set_filters(&self, filters: &[Filter]) -> Result<(), BusError>
pub fn set_filters(&self, filters: &[Filter]) -> Result<(), BusError>
Keeps only the frames that pass at least one of the filters, from now on.
An empty list keeps nothing, as SocketCAN’s does; clear_filters
keeps everything again.
§Arguments
filters- the filters, at most 512, the most SocketCAN takes.
§Errors
BusError::Device when the kernel refuses the filters.
Sourcepub fn clear_filters(&self) -> Result<(), BusError>
pub fn clear_filters(&self) -> Result<(), BusError>
Keeps every frame again, as a node does when it joins.
§Errors
BusError::Device when the kernel refuses the change.
Sourcepub fn received(&self) -> usize
pub fn received(&self) -> usize
Returns how many frames this node and its clones have received.
Sourcepub fn waited_micros(&self) -> u64
pub fn waited_micros(&self) -> u64
Returns how long receives on this node have waited without a frame, in microseconds, whether or not the process slept through it.