Listeners

Listener

The Listener class is an “abstract” base class for any objects which wish to register to receive notifications of new messages on the bus. A Listener can be used in two ways; the default is to call the Listener with a new message, or by calling the method on_message_received.

Listeners are registered with Notifier object(s) which ensure they are notified whenever a new message is received.

Subclasses of Listener that do not override on_message_received will cause NotImplementedError to be thrown when a message is received on the CAN bus.

class can.Listener
stop()

Override to cleanup any open resources.

BufferedReader

class can.BufferedReader

A BufferedReader is a subclass of Listener which implements a message buffer: that is, when the can.BufferedReader instance is notified of a new message it pushes it into a queue of messages waiting to be serviced.

get_message(timeout=0.5)

Attempts to retrieve the latest message received by the instance. If no message is available it blocks for given timeout or until a message is received (whichever is shorter),

Parameters:timeout (float) – The number of seconds to wait for a new message.
Returns:the Message if there is one, or None if there is not.

Logger

class can.Logger

Logs CAN messages to a file.

The format is determined from the file format which can be one of:

Printer

class can.Printer(output_file=None)

The Printer class is a subclass of Listener which simply prints any messages it receives to the terminal.

Parameters:output_file – An optional file to “print” to.

CSVWriter & SqliteWriter

These Listeners simply create csv and sql files with the messages received.

class can.CSVWriter(filename)

Writes a comma separated text file of timestamp, arbitrationid, flags, dlc, data for each messages received.

class can.SqliteWriter(filename)

Logs received CAN data to a simple SQL database.

The sqlite database may already exist, otherwise it will be created when the first message arrives.

ASCWriter

Logs CAN data to an ASCII log file compatible with other CAN tools such as Vector CANalyzer/CANoe and other. Since no official specification exists for the format, it has been reverse- engineered from existing log files. One description of the format can be found here.

class can.ASCWriter(filename)

Logs CAN data to an ASCII log file (.asc)

stop()

Stops logging and closes the file.