Running Python wrapper on Linux

  • September 20, 2018
  • Magnus Carlsson

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 "https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130980754&utm_status=latest"
$ 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 "https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130981966&utm_status=latest"
$ 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 "https://www.kvaser.com/downloads-kvaser/?utm_source=software&utm_ean=7330130981911&utm_status=latest"
$ unzip -q pycanlib.zip
$ 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, sendReceiveSingleCanMsg.py, that sends a CAN message on channel 1 and receives the same CAN message on channel 0:

# 01_sendReceiveSingleCanMsg.py
from canlib import canlib, Frame


def setUpChannel(channel=0,
                 openFlags=canlib.Open.ACCEPT_VIRTUAL,
                 bitrate=canlib.canBITRATE_500K,
                 outputControl=canlib.Driver.NORMAL):
    ch = canlib.openChannel(channel, openFlags)
    print("Using channel: %s, EAN: %s" % (
        canlib.ChannelData(channel).device_name,
        canlib.ChannelData(channel).card_upc_no))
    ch.setBusOutputControl(outputControl)
    ch.setBusParams(bitrate)
    ch.busOn()
    return ch


def tearDownChannel(ch):
    ch.busOff()
    ch.close()


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)
ch1.write(frame)

while True:
    try:
        frame = ch0.read()
        print(frame)
        break
    except (canlib.canNoMsg) as ex:
        pass
    except (canlib.canError) as ex:
        print(ex)

tearDownChannel(ch0)
tearDownChannel(ch1)

Running the above Python program results in the following:

$ python sendReceiveSingleCanMsg.py
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 [email protected]

Footnotes:

1

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 Kv...