Skip to main content

Module bus

Module bus 

Source
Expand description

One CAN bus, joined by each node on it.

CAN has no master: every node sees every frame on the wire and keeps the ones it wants. CanBus is one node’s place on a bus. On a Linux board it is a SocketCAN socket on an interface such as can0, opened with CanBus::open; anywhere, CanBus::simulated makes a bus inside the program. CanBus::join puts another node on the same bus. A node hears every frame the others send and none of its own, as a SocketCAN socket does, and keeps only the frames its Filters pass.

A receive on a simulated bus never waits: when nothing is waiting it returns at once and adds its timeout to CanBus::waited_micros, so a test of a controller that has gone quiet runs at once and still says how long a real one would have taken.

§Examples

use std::time::Duration;

use pamoja_can::bus::CanBus;
use pamoja_can::{CanId, Frame};

// Two nodes on one bus with nothing plugged in.
let controller = CanBus::simulated();
let gateway = controller.join()?;

// The controller reports a motor speed of 500 rpm, big-endian.
let speed = 500u16.to_be_bytes();
controller.send(&Frame::new(CanId::standard(0x20A), &speed)?)?;
let heard = gateway.receive(Duration::from_millis(10))?.expect("a frame is waiting");
assert_eq!(heard.data(), speed);

// A node does not hear its own frames, and a receive with nothing waiting is counted.
assert_eq!(controller.receive(Duration::from_millis(250))?, None);
assert_eq!(controller.waited_micros(), 250_000);

Structs§

CanBus
One node’s place on a CAN bus, shared by every clone of this handle.
Filter
A frame a node keeps: one whose identifier, masked, equals the filter’s, and whose width, standard or extended, is the filter’s.

Enums§

BusError
Why a send or a receive on a CanBus failed.
CanBusKind
What a node’s bus is.
OpenError
Why a CAN interface could not be opened.