Developer Blog

01/04/2015 by Magnus Carlsson

[Archive] Accessing Kvaser CANlib from Python (1 of 3)

This blog uses now deprecated functions, see blog “Improved API in Python canlib v1.5” for more information.

This is the first post in a 3-part series about using Python with Kvaser products:

  1. Accessing Kvaser CANlib from Python
  2. Using Python to connect to your remote device
  3. A more object oriented approach on accessing your Kvaser device from Python

Did you know that you can access Kvaser CANlib from Python? There have been Python wrappers for CANlib included in the Kvaser CANlib SDK for a while now, but recently they have gotten an overhaul and more libraries and functions are wrapped as we speak. Currently the wrappers is only tested in Python v2.7 and is not compatible with Python v3.

The latest released version of SDK can be found on www.kvaser.com/download/ (Current version is v 5.9 (released 2015-03-17). Be sure to install the SDK to a place where you have write permissions if you plan to edit the samples directly. (The default C:\Program Files (x86)\Kvaser\Canlib\ is usually not the ideal place. As you can see below, today I chose a sub-directory of C:\temp\.)

The downloaded Python sample directory now includes wrappers for both CANlib and kvrlib. To get an idea of how to use them, see the bottom of each wrapper file. E.g. the canlib.py can be used as seen below.

import sys
sys.path.append("C:/temp/Canlib_SDK_v5.9/Samples/Python")

import canlib

cl = canlib.canlib()
print "canlib version: %s" % cl.getVersion()

channel = 0
handle1 = cl.openChannel(channel, canlib.canOPEN_ACCEPT_VIRTUAL)
print "Using channel: %s, EAN: %s" % (handle1.getChannelData_Name(),
handle1.getChannelData_EAN())

handle1.setBusOutputControl(canlib.canDRIVER_NORMAL)
handle1.setBusParams(canlib.canBITRATE_1M)
handle1.busOn()

Running the above code results in the following printout.

canlib version: 8.9
Using channel: Kvaser Ethercan Light HS (Remote) (channel 0), EAN: 73-30130-00713-0

As veteran users of CANlib knows, the version number of canlib.dll that is printed (8.9), is not identical to the version number of the CANlib package (v5.9).

Appending of the variable sys.path (as seen on line 2) is one way to make the module accessible from Python, a more conventional way is to set the Environment variable PYTHONPATH before calling your script.

The Python wrapper uses ctypes to interface with Kvaser CANlib. This is a foreign function library for Python that provides C compatible data types and allows calling functions in DLLs or shared libraries. This means that it would also be possible to use these samples on Linux (but since kvrlib not yet is ported to Linux it is currently limited to CANlib).

In the next post, we will see how to connect to remote devices with Python…

EDIT 2015-05-22: Stated that the current wrappers are written for Python v2.7.

Comments?  Contact us directly at support@kvaser.com.

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.