Developer Blog

20/09/2018 by Magnus Carlsson

Running Python wrapper on Linux

This is an update of an older blog post and is now taking advantage of the Python canlib package v1.6 and later.

Today we take a look at how to setup and send a CAN message using Kvasers Python package canlib. For this example we use the Kvaser USBcan Pro 2xHS v2, but any Kvaser interface can be used.

First step is to download and install Kvaser Linux Driver and SDK, linuxcan, note that the URL is within quotation marks (the current version of CANlib is v5.24):

$ cd ~
$ # We need tools for building (gcc compiler, make, etc)
$ sudo apt install build-essential
$ # Download latest version of linuxcan, Kvaser Linux Driver and SDK
$ wget --content-disposition ""
$ tar xf linuxcan.tar.gz
$ cd linuxcan
$ make
$ sudo make install

Now we insert the Kvaser interface into a USB port and run the listChannels example program to verify that the driver loaded correctly and our device is recognized:

$ cd canlib/examples
$ ./listChannels
Found 2 channel(s).
ch  0: Kvaser USBcan Pro 2xHS v2  73-30130-00752-9, s/n 1014, v3.10.0.496  (mhydra v8.24.533)
ch  1: Kvaser USBcan Pro 2xHS v2  73-30130-00752-9, s/n 1014, v3.10.0.496  (mhydra v8.24.533)

The Kvaser Linux Driver and SDK only contains drivers, canlib and LINlib, the rest of the libraries (kvaMemoLibXML, kvmlib, kvlclib and kvaDbLib) 1 are placed in the Kvaser Linux SDK Library, kvlibsdk. We build and install these in a similar manner to Kvaser Linux Driver and SDK, but we first need to install some dependency packages.

$ cd ~
$ # The package libxml2-dev is needed in order to build kvamemolibxml.
$ sudo apt install libxml2-dev
$ # The packages libssl-dev and zlib1g-dev are needed to build kvlclib.
$ sudo apt install libssl-dev zlib1g-dev
$ # Download latest version of kvlibsdk, Kvaser Linux SDK Library
$ wget --content-disposition ""
$ tar xf kvlibsdk.tar.gz
$ cd kvlibsdk
$ make
$ sudo make install

The next step is to download and install the Python canlib package (current version is v1.6):

$ cd ~
$ wget --content-disposition ""
$ unzip -q
$ cd pycanlib
$ pip install canlib-1.6.615*.whl

Depending on your Linux distribution, if you are using Python v3, you would perhaps be using pip3 instead of pip in order to install the Python package in the correct place.

Now we can write a small Python program,, that sends a CAN message on channel 1 and receives the same CAN message on channel 0:

from canlib import canlib, Frame

def setUpChannel(channel=0,
    ch = canlib.openChannel(channel, openFlags)
    print("Using channel: %s, EAN: %s" % (
    return ch

def tearDownChannel(ch):

print("canlib dll version:", canlib.dllversion())

ch0 = setUpChannel(channel=0)
ch1 = setUpChannel(channel=1)

frame = Frame(id_=100, data=[1, 2, 3, 4], flags=canlib.MessageFlag.EXT)

while True:
        frame =
    except (canlib.canNoMsg) as ex:
    except (canlib.canError) as ex:


Running the above Python program results in the following:

$ python
canlib dll version: 8.24
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 0), EAN: 73-30130-00752-9
Using channel: Kvaser USBcan Pro 2xHS v2 (channel 1), EAN: 73-30130-00752-9
Frame(id=100, data=bytearray(b'\x01\x02\x03\x04'), dlc=4,
flags=<MessageFlag.EXT: 4>, timestamp=7918895)

Depending on your Linux distribution, if you are using Python v3, you would perhaps be using the python3 command, instead of python.

And thus we have successfully sent and received a CAN message using the Python canlib package. The documentation is also available in the downloaded .zip file at pycanlib/docs/index.html.

Bug reports, contributions, suggestions for improvements, and similar things are much appreciated and can be sent by e-mail to



See the blog article Get more from your hardware with Kvaser SDK libraries for an overview of the different SDK libraries.

Author Image

Magnus Carlsson

Magnus Carlsson is a Software Developer for Kvaser AB and has developed firmware and software for Kvaser products since 2007. He has also written a number of articles for Kvaser’s Developer Blog dealing with the popular Python language.