This is the first post in a 3-part series about using the Converter Library (kvlclib) in CANlib SDK:
- Writer formats and properties (1 of 3)
- Converting to plain ASCII (2 of 3)
- Special converter conditions (3 of 3)
This is the first post in a 3-part series about using the Converter Library (kvlclib) in CANlib SDK:
The converter included in the Kvaser Memorator Config Tool is also available as a separate converter library called kvlclib. If you have used the โExtract and convert filesโ wizard in Kvaser Memorator Config Tool, you have already been accessing the kvlclib and seen what it can do.
Figure 1: Starting โExtract and convert filesโ wizard from the menu.
In this article we take a look at how to use the Kvaser Converter Library, kvlclib, from Python using version 1.7 of the canlib package. The latest version of both the Python canlib package and the Kvaser CANlib SDK can be downloaded from www.kvaser.com/download.
The converter library has two functions, kvlclib.writer_formats
and kvlclib.reader_formats
, that can be used to list all supported formats.
Our first program uses these two functions to print all supported formats.
# 01_list_kvlclib_formats.py
from canlib import kvlclib
print("Supported Writer formats:")
print(" Id Name (Extension), Description\n" + '=' * 50)
for fmt in kvlclib.writer_formats():
print(fmt)
print("\nSupported Reader formats:")
print(" Id Name (Extension), Description\n" + '=' * 50)
for fmt in kvlclib.reader_formats():
print(fmt)
Running our code gives as the result a list1 of all the supported writer (i.e. output) and reader (i.e. input) formats.
Supported Writer formats:
Id Name (Extension), Description
==================================================
4: CSV Frame (.csv) Reader , CAN frames in CSV format
100: CSV Signal (.csv) Reader , Selected signals in CSV format
200: CSV CCP/XCP (.csv) Reader , CCP/XCP calibration in CSV format
102: Matlab (.mat) Reader , Selected signals in Matlab format for ATI Vision
1: KME 2.4 (.kme) Reader , Kvaser binary format (KME 2.4) - used for Vector CANalyzer
2: KME 2.5 (.kme25) Reader , Kvaser binary format (KME 2.5)
7: KME 4.0 (.kme40) Reader , Kvaser binary format (KME 4.0)
9: KME 5.0 (.kme50) Reader , Kvaser binary format (KME 5.0)
5: Plain text (.txt) Reader , CAN frames in plain text format
105: FAMOS (.dat) Reader , Selected signals in FAMOS format
201: FAMOS CCP/XCP (.dat) Reader , CCP/XCP calibration in FAMOS format
3: Vector ASCII (.asc) Reader , CAN frames in Vector ASCII format
8: Vector BLF (.blf) Reader , CAN frames in Vector BLF format
1000: Debug output (.dbg) Reader , RAW Debug output
101: MDF (.log) Reader , CAN frames in Vector Mdf
107: MDF v4.1 (.mf4) Reader , CAN frames in MDF v4.1 for Vector CANalyzer
106: MDF Signal (.mdf) Reader , Selected signals in MDF format for Vector CANalyzer
108: MDF v4.1 Signal (.mf4) Reader , Selected signals in MDF v4.1 for Vector CANalyzer
103: ASCII J1587 (.asc) Reader , Vector ASCII J1587
Supported Reader formats:
Id Name (Extension), Description
==================================================
1: KME 2.4 (.mke) Reader , Kvaser binary format (KME 2.4)
2: KME 2.5 (.kme25) Reader , Kvaser binary format (KME 2.5)
7: KME 4.0 (.kme40) Reader , Kvaser binary format (KME 4.0)
9: KME 5.0 (.kme50) Reader , Kvaser binary format (KME 5.0)
101: MDF (.log) Reader , CAN frames in Vector Mdf
107: MDF v4.1 (.mf4) Reader , CAN frames in MDF v4.1 for Vector CANalyzer
5: Plain text (.txt) Reader , CAN frames in plain text format
3: Vector ASCII (.asc) Reader , CAN frames in Vector ASCII format
4: CSV Frame (.csv) Reader , CAN frames in CSV format
6: Kvaser Memorator (.memo) Reader , Kvaser Memorator
Each writer format has a number of properties that controls how the resulting file should look, and by using the function isPropertySupported()
, we can find out if an individual property is supported by a specific format. E.g. the property ATTACHMENTS
indicates if we can attach files to the output file or not.
# 02_print_sample_properties.py
from canlib import kvlclib
# output format
fmt = kvlclib.WriterFormat(kvlclib.FileFormat.PLAIN_ASC)
# check if format supports the CHANNEL_MASK property
if fmt.isPropertySupported(kvlclib.Property.CHANNEL_MASK):
# print default value (in binary format)
print("Default value for CHANNEL_MASK is %s" %
bin(fmt.getPropertyDefault(kvlclib.Property.CHANNEL_MASK)))
Running the code shows us that the writer property CHANNEL_MASK
is supported by the writer format PLAIN_ASC
, and that the default value is 0x1F (0b11111
in binary format) which means that the five first channels (channel 0 to channel 4) will be included when writing with this formatter.2
Default value for CHANNEL_MASK is 0b11111
Once we have instantiated a writer format, the default value for an individual property can be examined using getPropertyDefault()
. To actually set and read a property, we need to create a converter using kvlclib.Converter()
, and then most (but not all) writer properties may be read and written using getProperty()
and setProperty()
.3
# 03_set_get_sample_properties.py
from canlib import kvlclib
# use plain ASCII output format
fmt = kvlclib.WriterFormat(kvlclib.FileFormat.PLAIN_ASC)
# set resulting output file name taking advantage of the extension
# defined in the format.
outfile = 'myresult.' + fmt.extension
# create converter
cnv = kvlclib.Converter(outfile, fmt)
# check if converter supports the CHANNEL_MASK property
if cnv.format.isPropertySupported(kvlclib.Property.CHANNEL_MASK):
# get the current (default) value (in binary format)
value = bin(cnv.getProperty(kvlclib.Property.CHANNEL_MASK))
print('Current value for CHANNEL_MASK is:', value )
# set a new value
cnv.setProperty(kvlclib.Property.CHANNEL_MASK, 1)
value = bin(cnv.getProperty(kvlclib.Property.CHANNEL_MASK))
print('New value for CHANNEL_MASK is:', value)
# it is also possible to specifically read the default value from the
# converter
value = bin(cnv.format.getPropertyDefault(kvlclib.Property.CHANNEL_MASK))
print('Default value for CHANNEL_MASK is:', value)
Running the code gives us the following output:
Current value for CHANNEL_MASK is: 0b11111
New value for CHANNEL_MASK is: 0b1
Default value for CHANNEL_MASK is: 0b11111
1 Due to implementation details, the result is returned in an arbitrary order.
2 The property CHANNEL_MASK
is set to a bitmask of the channels that should be used during conversion.
3 Beside ATTACHMENTS
, the properties SIGNAL_BASED
and SHOW_SIGNAL_SELECT
are only usable with the function isPropertySupported()
.