Developer Blog

14/04/2015 by Magnus Carlsson

A more object oriented approach on accessing your Kvaser device from Python (3 of 3)

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

This is the last 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

Earlier we have seen how to use the canlib.py and kvrlib.py wrappers in Python (currently for v2.7). However, those are primarily just “wrappers” and do not take into account the flexibility we get with an object oriented language as Python (this is equally true for other wrappers like C#).

During testing, the focus is often on a specific device instead of the channel centered CANlib. This is especially true when you are dealing with remote devices such as Kvaser BlackBird v2 and Ethercan that tend to turn up on new CANlib channels when connected remotely. This is now taken care of via the new Python module kvDevice.

To open a specific device, supply the EAN and serial number as shown below.

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

import kvDevice

device = kvDevice.kvDevice(ean="73-30130-00778-9", serial=1023)
print "\n", device

If we have a device with EAN and serial number that matches we get the following output:

Device: Kvaser Memorator Pro 5xHS (channel 0)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1a
Canlib channel: 3

If we do not specify the serial number, we get the first device that matches the EAN. It is also possible to specify a CANlib channel directly.

device = kvDevice.kvDevice(ean="73-30130-00778-9")
print "\n", device

device = kvDevice.kvDevice(ch=6)
print "\n", device
Device: Kvaser Memorator Pro 5xHS (channel 0)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1a
Canlib channel: 2

Device: Kvaser Memorator Pro 5xHS (channel 4)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1e
Canlib channel: 6

As a final example, if the specified device is not found, we still get a device returned but we do not get any information about it.

devices = kvDevice.kvDevice.allDevices()
print "List all %d devices" % (len(devices))
for dev in devices:
print "\n", dev

print "\n---\nTry and find a non-existing device."
device = kvDevice.kvDevice(ean="73-30130-00778-9", serial=100)
print "\n", device
List all 2 devices

Device: Kvaser Eagle (channel 0)
EAN           : 73-30130-00567-9
S/N           : 71
FW            : v2.5.261
Card          : 0
Drv           : kcany0a
Canlib channel: 0

Device: Kvaser Memorator Pro 5xHS (channel 0)
EAN           : 73-30130-00778-9
S/N           : 1023
FW            : v2.5.287
Card          : 1
Drv           : kcany1a
Canlib channel: 2
---
Try and find a non-existing device.

Device: None
EAN           : 73-30130-00778-9
S/N           : 100
FW            : None
Card          : None
Drv           : None
Canlib channel: None

If you take a look inside the module kvDevice.py, you’ll notice that most of the attributes are stored in names starting with an underscore (_driver, _channel and so on). This tries to denote that these values are the last known values. (In Python, a leading underscore is normally a weak “internal use” indicator.) That is, the device could now have been removed and perhaps also reinserted and thus got renumbered, but the last time the device was opened via kvDevice.py, it was found using those values. (The device was found on CANlib channel _channel using the driver name _driver.)

Hopefully, this overview of how Python can be used to help manage Kvaser devices will help you to be even more productive in your role as a developer.

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.