Memorator programming tutorial
This section describes how to develop programs for Memorator devices. With the libraries included with Canlib, you can write programs for reading and converting log files, as well as for configuring a device.
Using the samples
Samples for this tutorial are available in the \Samples\Memorator folder in your Canlib SDK installation.
- Before you start
- 1: Showing device info (C#)
- 2: Reading logs (C#)
- 3: Logging with t script (t)
- 4: Fetching configurations (C)
- 5. Converting configurations to XML (C++)
Before you start
In order to work with the libraries, you need to import them.
Working in C#
When creating a C# project in Visual Studio, add a reference to the appropriate version of kvmlibCSLNET.dll. You will also need to have the required dlls in your path.
Working in C or C++
When making a C or C++ program, include the header file of the library/libraries you want to use (like kvmlib or kvaMemoLibXML) and make sure that the libraries are in your path.
The Memorator libraries
- kvmlib
- Contains functions for reading log files from a device. Can also be used to read and write .kme files, as well as reading and writing param.lif configuration files to a device. To use this library in C#, import kvmlibCLSNET.dll.
- kvaMemoLibXml
- Contains functions for converting param.lif files to XML and vice versa. A specification for writing Memorator configurations is available in the Downloads section.
Fetching device info with Kvmlib
The KvmTest program demonstraters some basic Kvmlib functions which read info about a device.
Initialization
Like Canlib, Kvmlib must be initialized before usage. This is done with a call to the Initialize
method. Next, we open a handle to the device that we want to use, with DeviceOpen
. Note that the first parameter here is the card number, which is not the same thing as the channel number used in Canlib. The card number can be found in Kvaser Hardware. One important thing to note is that Kvaser Hardware starts indexing the devices at 1, while Kvmlib starts at 0. This means that the number in Kvaser Hardware must be decreased by 1 in order to point to the correct device.
Functions demonstrated
DeviceGetSerialNumber
This function returns the serial number of the device.
DeviceGetRTC
Reads the time from the device’s hardware clock. The return value is an Int64
with the timestamp in Unix time format (number of seconds since 1970-01-01 00:00:00). This can then be converted into a DateTime object as shown in the code.
DeviceDiskStatus
This method checks if the device has an SD card mounted. The output parameter present
will be non-zero if that is the case.
LogGetFileCount
This method checks how many log files are on the SD card.
KmfGetUsage
Checks how many 512 byte sectors are available on the card, and how many of them that are used.
Reading log events
The ReadLogs program goes through all the log files in the device and saves their contents in plain text files.
Initialization
Like in the previous sample, we use Initialize
to initialize Kvmlib and DeviceOpen
to get a handle to the device.
Reading log files
The log files are mounted using LogFileMount
. Here we need to supply the device handle, the index of the log file (starting from zero) and an integer where the number of events is saved.
Next, we use LogFileReadEvent
to iterate through the events in the log file. This method gives us a Log
object which contains all the information about the logged event. The method will return kvmERR_NOLOGMSG
when the end of the log file is reached. This way, we can iterate through all the events by calling LogFileReadEvent
until it returns an error.
Getting info from log events
The Log
objects belong to one of three subclasses depending on the type of event:
LogMsg
for received CAN messages. These contain all the fields of a CAN message, as well the number of the receiving channelLogRtcClock
for clock messages. These contain the timestamp in seconds (calendarTime
) and nanosecond (timeStamp
).LogTrigger
for trigger messages. These contain the type of the trigger, the pre- and post times, a timestamp and the type of trigger event.
To write the messages to the file, we check which of these subclasses the Log
object belongs to, and then construct a string from the values of this subclass.
Working in other languages
The Log
class is specific for the C# version of the library. When working in C or C++, the LogFileReadEvent
method returns a kvmLogEventEx
struct, which contains a union of the different event types. For details, see the help file for kvmlib.
Logging with t script
The example program in tLogger shows how to use the t programming language to start and stop logging. The t script uses a database to interpret messages, and has hooks which control the logging.
The database contains a message called StatusMessage
which contains two signals: Temperature
and Pressure
. When a StatusMessage
with a Temperature
value over 100 is received, the logging starts. The logging can be turned off by sending a message with id 1 to the Memorator.
If you want to compile the t program yourself using the scc
compiler, you need to include the database file: scc.exe -dbase status.dbc logtemp.t
Usage
Open the Kvaser Memorator Config Tool and connect to the device. Under “Scripts”, add the logtemp.txe
file and check the “Active” box. Make sure that the “Log everything” box under “Log configuration” is not checked. Then, press “Download” to download the configuration to the device and then “Disconnect”.
Start logging
The on CanMessage<*> StatusMessage
hook is executed when a StatusMessage
is received on any channel. Here we check if the Temperature
signal’s physical value is over 100. If it is, and the logger is idle, we call loggerStart
, which will start logging of all incoming messages.
Stop logging
The second hook has the prefilter
keyword attached, which ensures that the message will not be picked up by any other hook or trigger. Here, we use filterDropMessage
to remove the message from the logging queue (which means that messages with identifier 1 will not be logged) and call loggerStop
to stop the logging.
getconf
This sample demonstrates how to use kvmlib and kvaMemolib to list log files and to read and write configurations to a device.
Usage
To use the program, run getconf memoNr
followed by optional flags, where memoNr
is the card number of the Memorator device you want to use. Use the -ext
flag if the device has extended data capabilities, the -format
flag if the SD card on the device is unformated and you wish to format it, and the -write
flag if you have a param.lif
file that you want to write to your device.
Program description
The library is initialized with kvmInitialize
and the device is opened with kvmDeviceOpen
. If the SD card is not formatted and the -format
flag is used, the program will call kvmDeviceFormat
to format the card.
kvmLogFileGetCount
is used to find the total number of log files on the card. Each log file is mounted with kvmLogFileMount
, which also gives the number of events in the file. kvmLogFileGetStartTime
is used to find the start time of the log file.
The param.lif
file containing all the log files is read using kvmKmfReadConfig
. If the -write
flag is used, the program will attempt to read a local file called param.lif
and write it to the device using kvmKmfWriteConfig
. Otherwise, the program will write the file fetched from the device to a local file.
Converting XML to param.lif
The xml2param sample shows how to convert an XML file to a param.lif file which can be uploaded to a Memorator device, or vice versa.
Note that this program uses functions from kvaMemoLibXml, and not kvmlib.
Usage
Run the program with xml2param [-d] xmlfile paramfile [-inv]
.
The -d
flag will turn on debug output through a call to kvaXmlDebugOutput
. If the -inv
flag is used, the program will convert the XML file to a param.lif and write it to the specified output file using kvaXMLToFile
. Otherwise, the program will convert a param.lif file to XML and write it using kvaFileToXml
.