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.


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.


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.

  • 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

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


Discard every message that may be queued in the output buffer(s).


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, timeout=None)[source]

Transmit a message to CAN bus. Override this method to enable the transmit path.

  • msg – A can.Message object.
  • timeout (float) – If > 0, wait up to this many seconds for message to be ACK:ed. If timeout is exceeded, an exception will be raised. Might not be supported by all interfaces.

can.CanError if the message could not be written.


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


Called to carry out any interface specific cleanup required in shutting down a bus.

class can.interface.Bus[source]

Bases: object

Instantiates a CAN Bus of the given bustype, falls back to reading a configuration file from default locations.


Writing to the bus is done by calling the send() method and passing a Message object.


Reading from the bus is achieved by either calling the recv() method or by directly iterating over the bus:

for msg in bus:

Alternatively the Listener api can be used, which is a list of Listener subclasses that receive notifications when new messages arrive.