Kvaser’s CANLIB

Kvaser‘s CANLib SDK for Windows (also available on Linux).

Bus

class can.interfaces.kvaser.canlib.KvaserBus(channel, can_filters=None, **config)[source]

Bases: can.bus.BusABC

The CAN Bus implemented for the Kvaser interface.

Parameters:
  • channel (int) – The Channel id to create this bus with.
  • can_filters (list) –

    A list of dictionaries each containing a “can_id” and a “can_mask”.

    >>> [{"can_id": 0x11, "can_mask": 0x21}]
    

Backend Configuration

Parameters:
  • bitrate (int) – Bitrate of channel in bit/s
  • tseg1 (int) – Time segment 1, that is, the number of quanta from (but not including) the Sync Segment to the sampling point. If this parameter is not given, the Kvaser driver will try to choose all bit timing parameters from a set of defaults.
  • tseg2 (int) – Time segment 2, that is, the number of quanta from the sampling point to the end of the bit.
  • sjw (int) – The Synchronisation Jump Width. Decides the maximum number of time quanta that the controller can resynchronise every bit.
  • no_samp (int) – Either 1 or 3. Some CAN controllers can also sample each bit three times. In this case, the bit will be sampled three quanta in a row, with the last sample being taken in the edge between TSEG1 and TSEG2. Three samples should only be used for relatively slow baudrates.
  • driver_mode (bool) – Silent or normal.
  • single_handle (bool) – Use one Kvaser CANLIB bus handle for both reading and writing. This can be set if reading and/or writing is done from one thread.
  • receive_own_messages (bool) – If messages transmitted should also be received back. Only works if single_handle is also False. If you want to receive messages from other applications on the same computer, set this to True or set single_handle to True.
flash(flash=True)[source]

Turn on or off flashing of the device’s LED for physical identification purposes.

flush_tx_buffer()[source]

Wipeout the transmit buffer on the Kvaser.

recv(timeout=None)[source]

Read a message from kvaser device.

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.

Since Kvaser only supports setting one filter per handle, the filtering will be disabled if more than one filter is requested.

Parameters:can_filters (list) –

A list of dictionaries each containing a “can_id”, “can_mask” and “extended”.

>>> [{"can_id": 0x11, "can_mask": 0x21, "extended": False}]

A filter matches, when <received_can_id> & can_mask == can_id & can_mask

timer_offset = None

Approximate offset between time.time() and CAN timestamps (~2ms accuracy) There will always be some lag between when the message is on the bus to when it reaches Python. Allow messages to be on the bus for a while before reading this value so it has a chance to correct itself

Internals

The Kvaser Bus object with a physical CAN Bus can be operated in two modes; single_handle mode with one shared bus handle used for both reading and writing to the CAN bus, or with two separate bus handles. Two separate handles are needed if receiving and sending messages are done in different threads (see Kvaser documentation).

Warning

Any objects inheriting from Bus should not directly use the interface handle(/s).

Message filtering

The Kvaser driver and hardware only supports setting one filter per handle. If one filter is requested, this is will be handled by the Kvaser driver. If more than one filter is needed, these will be handled in Python code in the recv method. If a message does not match any of the filters, recv() will return None.