Bus¶
The Bus
class, as the name suggests, provides an abstraction of a CAN bus.
The bus provides a wrapper around a physical or virtual CAN Bus.
Filtering¶
Message filtering can be set up for each bus. Where the interface supports it, this is carried out in the hardware or kernel layer - not in Python.
API¶
-
class
can.
BusABC
(channel=None, can_filters=None, **config)[source]¶ Bases:
object
CAN Bus Abstract Base Class
- Concrete implementations must implement the following methods:
- send
- recv
As well as setting the channel_info attribute to a string describing the interface.
Parameters: - channel – The can interface identifier. Expected type is backend dependent.
- can_filters (list) –
A list of dictionaries each containing a “can_id” and a “can_mask”.
>>> [{"can_id": 0x11, "can_mask": 0x21}]
A filter matches, when
<received_can_id> & can_mask == can_id & can_mask
- config (dict) – Any backend dependent configurations are passed in this dictionary
-
__iter__
()[source]¶ Allow iteration on messages as they are received.
>>> for msg in bus: ... print(msg)
Yields: can.Message
msg objects.
-
channel_info
= 'unknown'¶ a string describing the underlying bus channel
-
recv
(timeout=None)[source]¶ Block waiting for a message from the Bus.
Parameters: timeout (float) – Seconds to wait for a message. Returns: None on timeout or a can.Message
object.
-
send
(msg)[source]¶ Transmit a message to CAN bus. Override this method to enable the transmit path.
Parameters: msg – A can.Message
object.Raise: can.CanError
if the message could not be written.
-
set_filters
(can_filters=None)[source]¶ Apply filtering to all messages received by this Bus.
Calling without passing any filters will reset the applied filters.
Parameters: can_filters (list) – A list of dictionaries each containing a “can_id” and a “can_mask”.
>>> [{"can_id": 0x11, "can_mask": 0x21}]
A filter matches, when
<received_can_id> & can_mask == can_id & can_mask