Using Python to connect to your remote device (2 of 3)

  • April 8, 2015
  • Magnus Carlsson

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

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

Last time I mentioned that kvrlib also has a Python v2.7 wrapper included in the Kvaser CANlib SDK and I thought we could take a quick look at this as well. The kvrlib library is used to handle remote devices, such as the Blackbird and Ethercan devices. We will try some discovery and configuration in the following two examples.

First let us use the discovery functions to scan and connect to a remote device. Our remote device has serial number 16 and is already connected to the same network as our computer:

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

import kvrlib

kl = kvrlib.kvrlib()
print "kvrlib version: %s" % kl.getVersion()

serialNo = 16
print "Connecting to device with serial number %s" % serialNo
addressList = kvrlib.kvrDiscovery.getDefaultAddresses(kvrlib.kvrAddressTypeFlag_BROADCAST)
print "Found %d addresses." % addressList.count
#print addressList                                       
discovery = kl.discoveryOpen()
discovery.setAddresses(addressList)

delay_ms = 100
timeout_ms = 1000
discovery.setScanTime(delay_ms, timeout_ms)
print "Scanning devices...\n"
deviceInfos = discovery.getResults()
#print "Scanning result:\n%s" % deviceInfos                
for deviceInfo in deviceInfos:
    if deviceInfo.ser_no == serialNo:
        deviceInfo.connect()
        print 'Connecting to the following device:'
        print '---------------------------------------------'
        print deviceInfo
        discovery.storeDevices(deviceInfos)
        break;
discovery.close()
kl.unload()

You should really set the envar PYTHONPATH before calling your script instead of appending to the sys.path (as seen on line 2). Note also that the Python wrappers are usually quite helpful when printing resulting variables. This means that you could uncomment line 13 and line 22 and get a human readable output.

The above code gives the following output:

kvrlib version: 8.9
Connecting to device with serial number 16
Found 4 addresses.
Scanning devices...

Connecting to the following device:
---------------------------------------------
name/hostname  : "Kvaser Remote #16" / "kv-07130-000016"
  ean/serial   : 73301-30007130 / 16
  fw           : 2.4.126
  addr/cli/AP  : 10.0.3.32 (IPV4) / 10.0.3.27 (IPV4) / unknown (UNKNOWN)
  usage/access : FREE / PUBLIC
  pass/enc.key : no / no

Now let us do some minimal configuration on the device, why not change the device name? The remote device has now turned up on CANlib channel 0 (line 6), let us set the device name to “MrKvaser” (line 7).

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

import kvrlib

canChannel = 0            
deviceName = "MrKvaser"   

kl = kvrlib.kvrlib()
print "Configurating device on channel %d." % canChannel
try:
    config = kl.configOpen(channel=canChannel, mode=kvrlib.kvrConfig.MODE_RW) 
    config.getXml()       
    print "Current device configuration:\n%s" % config.xml.toxml() 
    print "Device name was '%s'" % config.xml.getElementsByTagName('NETWORK')[0].attributes['device_name'].value
    print "Setting new device name '%s'" % deviceName
    config.xml.getElementsByTagName('NETWORK')[0].attributes['device_name'].value = deviceName 
    config.setXml()       
    config.close()
except (kvrlib.kvrError) as ex:
    print ex

kl.unload()

We start by opening the configuration in read/write mode on line 12, then reads in the existing configuration on line 13. After printing the whole XML configuration on line 14 we manipulate the device name element before writing it back to the device on line 18.

This results in the following being printed on the console:

Configurating device on channel 0.
Current device configuration:
<?xml version="1.0" ?><KVASER>
<VERSION>4.3</VERSION>
<PRODUCTFAMILY>ethercan</PRODUCTFAMILY>
<DEVICE_ACCESS level="PUBLIC" password=""/>
<CONFIG_PROTECTION password="" protected="NO"/>
<NETWORK address_type="DHCP" device_name="Kvaser Remote #16" host_name="kv-07130-000016" mtu="1492" port="11416"/>
<PROFILE_INFO name="Unnamed Profile"/>
<TRAFFIC_ENCRYPTION key="" type="NONE"/>
<WEBSERVER change_log_settings="NO" debug_commands="NO" enabled="NO" user_data_download="NO"/>
</KVASER>
Device name was 'Kvaser Remote #16'
Setting new device name 'MrKvaser'

The XML configuration that we get from the device can be manipulated using built-in Python XML support as can be seen on line 14 and line 17.

You can find the full description of the XML used during configuration in the document Specification of Kvaser Remote Device Configuration XML Format on the download page.

Just one more thing, do we really need to “know” that the device will turn up on CANlib channel 0 (at line 6)? Let us fix that in the next post…

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

Comments?  Contact us directly at [email protected]

Author Image

Magnus Carlsson

Magnus Carlsson is a Software Developer for Kvaser AB and has developed firmware and software for Kv...