The Kvaser t programming language is event oriented and modeled after C. It can be used to customize the behavior of Kvaser t capable devices. devices.
A t program is invoked via hooks, which are entry points that are executed at the occurrence of certain events. These events can be, for example, the arrival of specific CAN messages, timer expiration, or external input.
Here we will describe how to interact with t programs on a Kvaser device (i.e. loading, starting, stopping) For a complete reference to the t language, see the Kvaser t Programming Language available from www.kvaser.com/downloads.
The first step is to compile your t program into a .txe file, see the Kvaser t Programming Language. A compiled .txe file may be examined using kvScriptTxeGetData().
Before starting a t program you need to load it into an available "slot". Some Kvaser devices have multiple slots, and are therefore capable of running multiple programs simultaneously.
To load a script located on the host PC, use kvScriptLoadFile(). The canHandle argument is used to determine what channel is set as the default channel for the loaded script. If your canHandle was opened via a device's second channel, the default channel number will be set to 1 (the numbering of channel on the card starts from 0).
To load a script located on the device, use kvScriptLoadFileOnDevice(). To copy arbitrary files to and from the the device, use kvFileCopyToDevice() and kvFileCopyFromDevice() respectively.
Example. The following code fragment shows how to load the compiled script "script0.txe" from PC and "script3.txe" from the SD card, using the channel number "hnd" was opened with as the default channel.
To unload a stopped script, use kvScriptUnload().
You may use kvFileGetCount(), and kvFileGetName() to examine files located on the Kvaser device, and kvFileDelete() to delete a specific file.
To start a previously loaded t program, use kvScriptStart(). You may stop a running script using kvScriptStop(). To examine the status of a slot (i.e. if the slot is free or have a program loaded or running), use kvScriptStatus().
Example. Starting and stopping scripts loaded in slot 0 and 3.
To communicate between the PC and your t program, you can use t Environment Variables (Envvar). The first step is to get a handle to your Envvar using kvScriptEnvvarOpen().
Depending of the type of the Envvar, you may now examine the value using kvScriptEnvvarGetInt(), kvScriptEnvvarGetFloat() and kvScriptEnvvarGetData(). The size of the "Data" Envvar is set by the user but can be max kvScriptGetMaxEnvvarSize() bytes.
To change the value of the Envvar, use kvScriptEnvvarSetInt(), kvScriptEnvvarSetFloat() and kvScriptEnvvarSetData() respectively.
Use kvScriptEnvvarClose() to free up memory when done handling the Envvar
Example. The following code fragment shows how to use environment variables, Envvars for communication between a PC and a script.
You may trigger the "on key" hook by sending the event kvEVENT_TYPE_KEY to a script using kvScriptSendEvent().
Example. The following code fragment shows how to send a key to a script.