Internal API¶
Here we document the odds and ends that are more helpful for creating your own interfaces or listeners but generally shouldn’t be required to interact with python-can.
Extending the BusABC
class¶
- Concrete implementations must implement the following:
send()
to send individual messages_recv_internal()
to receive individual messages (see note below!)- set the
channel_info
attribute to a string describing the underlying bus and/or channel
- They might implement the following:
flush_tx_buffer()
to allow discarding any messages yet to be sentshutdown()
to override how the bus should shut down_send_periodic_internal()
to override the software based periodic sending and push it down to the kernel or hardware._apply_filters()
to apply efficient filters to lower level systems like the OS kernel or hardware._detect_available_configs()
to allow the interface to report which configurations are currently available for new connections.state()
property to allow reading and/or changing the bus state.
Note
TL;DR: Only override _recv_internal()
,
never recv()
directly.
Previously, concrete bus classes had to override recv()
directly instead of _recv_internal()
, but that has
changed to allow the abstract base class to handle in-software message
filtering as a fallback. All internal interfaces now implement that new
behaviour. Older (custom) interfaces might still be implemented like that
and thus might not provide message filtering:
Concrete instances are usually created by can.Bus
which takes the users
configuration into account.
Bus Internals¶
Several methods are not documented in the main can.BusABC
as they are primarily useful for library developers as opposed to
library users. This is the entire ABC bus class with all internal
methods:
-
class
can.
BusABC
(channel, can_filters=None, **kwargs)[source] Bases:
object
The CAN Bus Abstract Base Class that serves as the basis for all concrete interfaces.
This class may be used as an iterator over the received messages.
Construct and open a CAN bus instance of the specified type.
Subclasses should call though this method with all given parameters as it handles generic tasks like applying filters.
Parameters: -
RECV_LOGGING_LEVEL
= 9 Log level for received messages
-
__init__
(channel, can_filters=None, **kwargs)[source] Construct and open a CAN bus instance of the specified type.
Subclasses should call though this method with all given parameters as it handles generic tasks like applying filters.
Parameters:
-
__iter__
()[source] Allow iteration on messages as they are received.
>>> for msg in bus: ... print(msg)
Yields: Message
msg objects.Return type: Iterator
[Message
]
-
__weakref__
list of weak references to the object (if defined)
-
_apply_filters
(filters)[source] Hook for applying the filters to the underlying kernel or hardware if supported/implemented by the interface.
Parameters: filters ( Optional
[Iterable
[CanFilter
]]) – Seeset_filters()
for details.
-
static
_detect_available_configs
()[source] Detect all configurations/channels that this interface could currently connect with.
This might be quite time consuming.
May not to be implemented by every interface on every platform.
Return type: Iterator
[dict
]Returns: an iterable of dicts, each being a configuration suitable for usage in the interface’s bus constructor.
-
_matches_filters
(msg)[source] Checks whether the given message matches at least one of the current filters. See
set_filters()
for details on how the filters work.This method should not be overridden.
Parameters: msg ( Message
) – the message to check if matchingReturn type: bool
Returns: whether the given message matches at least one filter
-
_recv_internal
(timeout)[source] Read a message from the bus and tell whether it was filtered. This methods may be called by
recv()
to read a message multiple times if the filters set byset_filters()
do not match and the call has not yet timed out.New implementations should always override this method instead of
recv()
, to be able to take advantage of the software based filtering provided byrecv()
as a fallback. This method should never be called directly.Note
This method is not an @abstractmethod (for now) to allow older external implementations to continue using their existing
recv()
implementation.Note
The second return value (whether filtering was already done) may change over time for some interfaces, like for example in the Kvaser interface. Thus it cannot be simplified to a constant value.
Parameters: Return type: Returns: - a message that was read or None on timeout
- a bool that is True if message filtering has already been done and else False
Raises: - can.CanError – if an error occurred while reading
- NotImplementedError – if the bus provides it’s own
recv()
implementation (legacy implementation)
-
_send_periodic_internal
(msgs, period, duration=None)[source] Default implementation of periodic message sending using threading.
Override this method to enable a more efficient backend specific approach.
Parameters: Return type: Returns: A started task instance. Note the task can be stopped (and depending on the backend modified) by calling the
stop()
method.
-
channel_info
= 'unknown' a string describing the underlying bus and/or channel
-
filters
Modify the filters of this bus. See
set_filters()
for details.Return type: Optional
[Iterable
[CanFilter
]]
-
flush_tx_buffer
()[source] Discard every message that may be queued in the output buffer(s).
-
recv
(timeout=None)[source] Block waiting for a message from the Bus.
Parameters: timeout ( Optional
[float
]) – seconds to wait for a message or None to wait indefinitelyReturn type: Optional
[Message
]Returns: None on timeout or a Message
object.Raises: can.CanError – if an error occurred while reading
-
send
(msg, timeout=None)[source] Transmit a message to the CAN bus.
Override this method to enable the transmit path.
Parameters: - msg (Message) – A message object.
- timeout (
Optional
[float
]) – If > 0, wait up to this many seconds for message to be ACK’ed or for transmit queue to be ready depending on driver implementation. If timeout is exceeded, an exception will be raised. Might not be supported by all interfaces. None blocks indefinitely.
Raises: can.CanError – if the message could not be sent
-
send_periodic
(msgs, period, duration=None, store_task=True)[source] Start sending messages at a given period on this bus.
The task will be active until one of the following conditions are met:
- the (optional) duration expires
- the Bus instance goes out of scope
- the Bus instance is shutdown
BusABC.stop_all_periodic_tasks()
is called- the task’s
CyclicTask.stop()
method is called.
Parameters: - msgs (
Union
[Sequence
[Message
],Message
]) – Messages to transmit - period (
float
) – Period in seconds between each message - duration (
Optional
[float
]) – Approximate duration in seconds to continue sending messages. If no duration is provided, the task will continue indefinitely. - store_task (
bool
) – If True (the default) the task will be attached to this Bus instance. Disable to instead manage tasks manually.
Return type: Returns: A started task instance. Note the task can be stopped (and depending on the backend modified) by calling the
stop()
method.Note
Note the duration before the messages stop being sent may not be exactly the same as the duration specified by the user. In general the message will be sent at the given rate until at least duration seconds.
Note
For extremely long running Bus instances with many short lived tasks the default api with
store_task==True
may not be appropriate as the stopped tasks are still taking up memory as they are associated with the Bus instance.
-
set_filters
(filters=None)[source] Apply filtering to all messages received by this Bus.
All messages that match at least one filter are returned. If filters is None or a zero length sequence, all messages are matched.
Calling without passing any filters will reset the applied filters to None.
Parameters: filters ( Optional
[Iterable
[CanFilter
]]) –A iterable of dictionaries each containing a “can_id”, a “can_mask”, and an optional “extended” key.
>>> [{"can_id": 0x11, "can_mask": 0x21, "extended": False}]
A filter matches, when
<received_can_id> & can_mask == can_id & can_mask
. Ifextended
is set as well, it only matches messages where<received_is_extended> == extended
. Else it matches every messages based only on the arbitration ID and mask.
-
shutdown
()[source] Called to carry out any interface specific cleanup required in shutting down a bus.
-
state
Return the current state of the hardware
Return type: BusState
-
About the IO module¶
Handling of the different file formats is implemented in can.io
.
Each file/IO type is within a separate module and ideally implements both a Reader and a Writer.
The reader usually extends can.io.generic.BaseIOHandler
, while
the writer often additionally extends can.Listener
,
to be able to be passed directly to a can.Notifier
.
Adding support for new file formats¶
This assumes that you want to add a new file format, called canstore. Ideally add both reading and writing support for the new file format, although this is not strictly required.
- Create a new module: can/io/canstore.py (or simply copy some existing one like can/io/csv.py)
- Implement a reader
CanstoreReader
(which often extendscan.io.generic.BaseIOHandler
, but does not have to). Besides from a constructor, only__iter__(self)
needs to be implemented. - Implement a writer
CanstoreWriter
(which often extendscan.io.generic.BaseIOHandler
andcan.Listener
, but does not have to). Besides from a constructor, onlyon_message_received(self, msg)
needs to be implemented. - Add a case to
can.io.player.LogReader
’s__new__()
. - Document the two new classes (and possibly additional helpers) with docstrings and comments. Please mention features and limitations of the implementation.
- Add a short section to the bottom of doc/listeners.rst.
- Add tests where appropriate, for example by simply adding a test case called class TestCanstoreFileFormat(ReaderWriterTest) to test/logformats_test.py. That should already handle all of the general testing. Just follow the way the other tests in there do it.
- Add imports to can/__init__py and can/io/__init__py so that the new classes can be simply imported as from can import CanstoreReader, CanstoreWriter.
IO Utilities¶
Contains a generic class for file IO.
-
class
can.io.generic.
BaseIOHandler
(file, mode='rt')[source]¶ Bases:
object
A generic file handler that can be used for reading and writing.
Can be used as a context manager.
Attr Optional[FileLike] file: the file-like object that is kept internally, or None if none was opened
Parameters:
-
class
can.io.generic.
MessageReader
(file, mode='rt')[source]¶ Bases:
can.io.generic.BaseIOHandler
The base class for all readers.
Parameters:
-
class
can.io.generic.
MessageWriter
(file, mode='rt')[source]¶ Bases:
can.io.generic.BaseIOHandler
,can.listener.Listener
The base class for all writers.
Parameters:
Other Utilities¶
Utilities and configuration file parsing.
-
can.util.
channel2int
(channel)[source]¶ Try to convert the channel to an integer.
Parameters: channel – Channel string (e.g. can0, CAN1) or integer Returns: Channel integer or None if unsuccessful Return type: int
-
can.util.
dlc2len
(dlc)[source]¶ Calculate the data length from DLC.
Parameters: dlc (int) – DLC (0-15) Returns: Data length in number of bytes (0-64) Return type: int
-
can.util.
len2dlc
(length)[source]¶ Calculate the DLC from data length.
Parameters: length (int) – Length in number of bytes (0-64) Returns: DLC (0-15) Return type: int
-
can.util.
load_config
(path=None, config=None, context=None)[source]¶ Returns a dict with configuration details which is loaded from (in this order):
- config
- can.rc
- Environment variables CAN_INTERFACE, CAN_CHANNEL, CAN_BITRATE
- Config files
/etc/can.conf
or~/.can
or~/.canrc
where the latter may add or replace values of the former.
Interface can be any of the strings from
can.VALID_INTERFACES
for example: kvaser, socketcan, pcan, usb2can, ixxat, nican, virtual.Note
The key
bustype
is copied tointerface
if that one is missing and does never appear in the result.Parameters: - path – Optional path to config file.
- config – A dict which may set the ‘interface’, and/or the ‘channel’, or neither. It may set other values that are passed through.
- context – Extra ‘context’ pass to config sources. This can be use to section other than ‘default’ in the configuration file.
Returns: A config dictionary that should contain ‘interface’ & ‘channel’:
{ 'interface': 'python-can backend interface to use', 'channel': 'default channel to use', # possibly more }
Note
None
will be used if all the options are exhausted without finding a value.All unused values are passed from
config
over to this.Raises: NotImplementedError if the
interface
isn’t recognized
-
can.util.
load_environment_config
(context=None)[source]¶ Loads config dict from environmental variables (if set):
- CAN_INTERFACE
- CAN_CHANNEL
- CAN_BITRATE
- CAN_CONFIG
if context is supplied, “_{context}” is appended to the environment variable name we will look at. For example if context=”ABC”:
- CAN_INTERFACE_ABC
- CAN_CHANNEL_ABC
- CAN_BITRATE_ABC
- CAN_CONFIG_ABC
-
can.util.
load_file_config
(path=None, section='default')[source]¶ Loads configuration from file with following content:
[default] interface = socketcan channel = can0
Parameters: - path – path to config file. If not specified, several sensible default locations are tried depending on platform.
- section – name of the section to read configuration from.