Support Note

 
       
   

Problem 

How do I convert from CAPL-scripts to USR-scripts?

 

 

Solution

How to convert a CAPL-script  to USR-script

This document is under constant development, but if will take care of most CAPL-scripts "out there"... 

If you have suggestions to convert something in a better way, please send an e-mail to support@kvaser.se.
The list you see below is tested and it works. If you have questions, don't hesitate to contact us! (like triggers and so on...)

Please observe the following before you start to convert your CAPL-code to USR-code:

  • USR is case-sensitive. CAPL is not.
  • USR doesn't have matrixes. (Don't mix this with arrays, which USR supports).
  • Arrays of strings do not exist in USR - would be matrixes and USR don't have that.
  • In CAPL, a locally defined variable is static. Not so in USR. Watch out for local variables that are initialized.
  • All CAPL-browser comments /*@@....*/ can be removed.
  • The *.dbc-files are 100% compatible.

 

EASY "SEARCH AND REPLACE" OPERATIONS.

CAN messages

CAPL


message [msg_name_in_db]  [message_name_in_CAPL];


In CAPL you see a CAN-message just as any other variable.
[msg_name_in_db]: Name of the "Message" in your *.dbc file.
[message_name_in_CAPL]: The "variable" name you which to use in the CAPL-script.
USR


CanMessage_[msg_name_in_db] [message_name_in_USR];


 

Works just as CAPL, only minor syntax difference.
[msg_name_in_db]: Name of the "Message" in your *.dbc file.
[message_name_in_USR]: The "variable" name you which to use in the USR-script.

"Just search and replace should be enough, remeber the underscore".

Example






CAPL-example

variables {
  message ALPHA_FRAME_Q AlphaFrame; /* database message */
  message BETA_FRAME_6 BetaFrame; /* database message */
  message  GammaFrame; /* Non database message */
  message  DeltaFrame; /* Non database message */
}  
USR-example

variables {
  CanMessage_ALPHA_FRAME_Q AlphaFrame; /* database message */
  CanMessage_BETA_FRAME_6 frame2; /* database message */
  CanMessage  GammaFrame; /* Non database message */
  CanMessage  DeltaFrame; /* Non database message */
}

 

 

 

 

 

 

 





Variable msTimer

CAPL

mstimer [timer]; Millisecond timer in CAPL.
USR


timer [variable];

 

In USR the corresponding data type is called just "timer". In USR a timer is *always* a millisecond resolution timer.

"Just search and replace should be enough".

Example



CAPL-example

variables {
  mstimer t5;
}  
USR-example

variables {
  timer t5;
}

 

 

 

 

 


Variable float

CAPL

float [variable]; Data type "float" is supported in CAPL.
USR

 

double [variable];

 

In USR the corresponding data type is called double.

"Just search and replace should be enough".

Example



CAPL-example

variables {
  float Control_Min=0.0;
}  
USR-example

variables {
   double Control_Min=0.0;
}

 

 

 

 

 


Variable dword

CAPL

dword [variable]; Data type "float" is supported in CAPL.
USR

 

int [variable];

 

In USR the corresponding data type is called int.

"Just search and replace should be enough".

Example



CAPL-example

variables {
  dword Control_Min = 122;
}  
USR-example

variables {
   int Control_Min = 122;
}

 

 

 

 

 


Variable word

CAPL

word [variable]; Data type "word" is supported in CAPL.
USR

 

int [variable];

 

In USR the corresponding data type is called int.

"Just search and replace should be enough".

Example



CAPL-example

variables {
  word  Control_Temp=13;
}  
USR-example

variables {
   int Control_Temp=13;
}

 

 

 

 

 


on message

CAPL

on message [message_name_in_CAPL]
{
  /* bla blu blah */
}
To trigger some action when a message has been received is done with the function "on message".

USR


on CanMessage [message_name_in_USR]
{
  /* bla blu blah */
}
Corresponding function is called "on CanMessage".


Example

 

 

 




CAPL-example

on message *
{
  /* bla bla bla */
}

on message AlphaFrame
{
  /* bla bla bla */
}

USR-example

on CanMessage *
{
  /* bla bla bla */
}

on CanMessage AlphaFrame 
{
  /* bla bla bla */

 

 

 

 

 

 

 

 

 

 


output

CAPL

output([message_name_in_CAPL]); To send a CAN-message from CAPL is done with the function "output".
USR canSendMessage([message_name_in_USR]) Corresponding function is called "on CanMessage".
Example



CAPL-example

output(this);

output( DeltaFrame );

USR-example

canSendMessage(this);

canSendMessage( DeltaFrame );

 

 

 

 

 

write

CAPL

write("[the text %x, %d, %f]", [comma sep. variables] ); This is how you write something on the screen in CAPL.
USR printf("[the text %x, %d, %f]", [comma sep. variables] ); This is how you write something on the screen in USR. (Just like "C").
Example



CAPL-example

write("Let's swap to Navigator");

write("the price is only %d !!!", (price-discount));

USR-example

printf("Let's swap to Navigator");

printf("\n...the price is only %d !!!", (price-discount));

 

 

 

 


cancelTimer

CAPL

cancelTimer( [timer] );

[timer]: A timer declared in the variables section. Most likely millesecond
   timers.
USR
timerCancel( [timer] );
[timer]: A timer declared in the variables section. Always a millisecond timer.
Example

 

CAPL-example

cancelTimer( aTimer )
USR-example

timerCancel( aTimer );

 

 

 

 

 



CHANGES THAT REQUIRES SOME HANDS ON WORK.

Differances how to access contents of CAN-messages

CAPL

msg.byte(x) This is how a specific byte of a message is accessed in CAPL-script..
USR msg.data[x] This is how a specific byte of a message is accessed in USR-script.
Example



CAPL-example

if (this.byte(2) == temperature) {
  /* bla bla bla */
USR-example

if (this.data[2] == temperature) {
  /* bla bla bla */

 

 

 

 


setTimer

CAPL




SetTimer( [timer], [timeout value] );




[timer]: A timer declared in the variables section. Most likely millesecond
   timers.
[timeout value]: Depending of the type of timer the timeout for this timer will
   be  a multipple of "timeout value".
USR








timerSetTimeout([timer], [timeout value]);
timerStart([timer]);







[timer]: A timer declared in the variables section. Always a millisecond timer.
[timeout value]: Depending of the type of timer that was orginally in the    
  CAPL-script you have to recalculate the timeout value. If the orignal
  CAPL-script timer was millisec-timer then nothing have to be recalculated.  
Note that you have to add a "timerStart([timer])" since the CAPL's "SetTimer" both initiates the timer and starts the timing.
Example


CAPL-example

SetTimer( aTimer_6, 250 );
USR-example

timerSetTimeout( aTimer_6, 250 );
timerStart( aTimer_6 );

 

 

 

 

 

 

 

 

 

 

 

signals

CAPL


[message_name_in_CAPL].[signal_in_db] = [value];


[message_name_in_CAPL]: The, in the variables list declared meassage.
[signal_in_db]: A signal in the specific CAN-message.
[value]: The value to assign to the variable.
USR




[message_name_in_USR].[signal_in_db].Raw = [value];




[message_name_in_USR]: The, in the variables list declared meassage.
[signal_in_db]: A signal in the specific CAN-message.
[value]: The value to assign to the variable.
The difference is that, in CAPL, the values are by default in Raw-mode.
Example

CAPL-example

BetaFrame.BetaFrameTemp = 0x10;
USR-example

BetaFrame.BetaFrameTemp.Raw = 0x10;

 

 

 

 

 

 

 

 


on key

CAPL


on key [key-combination]{
  /* bla bla bla */
}
[key-combination]: The key/keys that are supposed to be pressed to trigger a event.
USR

on key [key-combination]{
  /* bla bla bla */
}
[key-combination]: The key/keys that are supposed to be pressed to trigger a 
  event.
Example









CAPL-example

The "Control"-key:

cntrlF1


PageUp


shiftF2

USR-example

The "Control"-key:

CTRL+F1 (Navigator understands both but it looks much better as Navigator
  prefers)

KEY_PGUP (Navigator understands both but it looks much better as   
  Navigator prefers) 

SHIFT+F2 (Navigator understands both but it looks much better as   
  Navigator prefers) 

 

 

 

 

 

 

 

 

 



function declaration

CAPL


[datatype] [function name]([comma sep. variables])
{
    /* bla bla bla */
}
Note that a function that doesn't return a value doesn't have to be declared as void in CAPL (no datatype is specified).

USR

[datatype] [function name]([comma sep. variables])
{
    /* bla bla bla */
}
If [datatype] is not given in the CAPL script you may set the data type to void in the USR-script.

All other datatypes should work just fine!
Example




CAPL-example

A_nice_function()
{
    /* bla bla bla */
}
USR-example

void A_nice_function()
{
    /* bla bla bla */
}

 

 

 

 

 

 

 


CHANGES THAT REQUIRES SOME CAREFUL THINKING.

Declaration of local variables.

CAPL







myFunctionA{
  int variable_1;
  /* bla bla bla */
}

myFunctionB{
  int variable_1;
  /* bla bla bla */
}

In CAPL, a locally defined variable is static. Not so in USR. Watch out for local variables that are initialized.






USR







myFunctionA{
  int variable_1;
  /* bla bla bla */
}

myFunctionB{
  int variable_1;  /* OOOOOPS!!! */
  /* bla bla bla */
}

There will be a conflict between the two variables variable_1 and variable_1 since they have the same name!






 

 

 

 

 

 

 

 

 


Struct assignment.

CAPL










variables {
  message kvaser_rulesA;
  message kvaser_rulesB;
  message kvaser_rulesC;
  message kvaser_rulesD;
}

on message kvaser_rules
{
  kvaser_rulesB = this;
  kvaser_rulesD = kvaser_rulesC;
}

In CAPL you can to like this...










USR










variables {
  CanMessage kvaser_rulesA;
  CanMessage kvaser_rulesB;
  CanMessage kvaser_rulesC;
  CanMessage kvaser_rulesD;
}

on message kvaser_rules
{
  memcopy (kvaser_rulesB, this);
  memcopy (kvaser_rulesD, kvaser_rulesC);
}

... and in USR like this!









 

 

 

 

 

 

 

 

 

 

 

 



Logical operations priority.

CAPL

if ([logical experssion]){
  /* bla bla bla */
}
Sometimes you have a logical expression to test...

USR

if ([logical experssion]){
  /* bla bla bla */
}
... and in USR like this!

Example CAPL
if ( A & B == C & D || E & F == G & H )

USR
if ( ( ( A & B )  == ( C & D ) )  ||  ( ( E & F ) == ( G & H) ) )
Don't leave it like this! In USR you have the same system as the "C" language! CAPL uses something strange...

For readability, use a lot of ( )! That's our advice! 

 

 

 

 

 

 

 

This is the old version of this document...

How to convert a CAPL program to USR

Timers

Change "msTimer" to "timer" in the declaration of a timer. All timers have millisecond resolution in USR.

If you have CAPL timers that are not millisecond-resolution timers, be sure to check all calls to setTimer(). to ensure the timeout is appropriately changed.

  • setTimer() : use timerSetTimeout() and timerStart()
  • startTimer() : use timerStart()
  • cancelTimer() : use timerCancel()

 

Note: don't call timerStart() on a timer that is already running in Navigator 1.7 or earlier.

CAN messages

Message declarations

Message declarations are different in USR. Change "message" to CanMessage in the declaration of the message. So this CAPL declaration:

message msg-definition msg;

is in USR

CanMessage_msg-definition msg;

Note the underscore between CanMessage and the name of the message definition!

Accessing the raw data in a CAN message

CAPL: msg.byte(x) -> USR: msg.data[x];

CAPL: msg.word(x) doesn't exist in USR

 

 

CanMessage <file>::<message> msg qqq

msg1.SIMULATED and a few other special fields are not supported in USR.

 

Signals are raw by default in CAPL

In CAPL, a signal value is "raw" (i.e. not scaled) by default. This CAPL code

msg.signal = 34;

must be written like this in USR:

msg.signal.Raw = 34;

If you don't append the string ".Raw" you will see error E258 or E271 in Navigator.

CAN channel qualifier on messages

"on CanMessage <iface>.<message>" - not in USR

Declaration "CanMessage CAN1.* msg" not in USR
(If this occurs because you want to transmit on a specific CAN interface,
it's not needed in USR. Use canSendMessage() or canChannelSendMessage())

 

User-defined functions

In USR, when a struct or array is passed to a function it is always passed by reference. So the following CAPL code

CheckSnapshot( message * msg )
{
// bla bla
}

is expressed like this in USR:

void CheckSnapshot( CanMessage msg )
{
// bla bla
}

 

 

Back to Technical Notes index

 
 
 


© Kvaser AB - All rights reserved | Contact us | Webmaster | Sitemap
Direct link to this page: