The PP2CAN program for working with the CAN bus contains its own scripting language that allows you to easily create your own tools for generating data on the CAN bus, but also to process data from the CAN bus and implement various simple control algorithms or behavioral simulations. The syntax of this language is inspired by both C and Pascal. This custom syntax and implementation of the scripting language was developed even earlier than most current scripting languages and was chosen because of the ease of interfacing with the PP2CAN program without having to modify any existing scripting language at the time and port custom modifications to new versions of the interpreters of these languages.
The PP2CAN installation contains thirty sample scripts in the installation directory in the "Demo data" folder. A description of the scripting language function and CAN script window controls can be found in the corresponding manual.
The CAN scripts in PP2CAN have 2 basic variants, namely CAN_FUNCTION and TIMER_SCRIPT. The first type is triggered when a CAN message arrives, the triggering of the second type is determined by a periodic timer. There is also a third variant, namely CAN_FUNCTION_AND_TIMER. This is then triggered in both cases.
In this first part, we will create a very simple script that will generate a periodic message on the CAN bus. In this message, the value of the first data byte will be controlled by a scroll bar from the script window. The value of the slider will also control the setting of the upper bit in data byte 7. If we needed to control the value of the data byte in PP2CAN and send the data to CAN, we can normally use the Data sender tool. In this case, however, we need to additionally control the state of the bit in DB7. This is not possible with the Data sender tool and therefore it is advisable to use a script.
Each script starts with a header that defines what kind of script it is, so in this case it is a periodic script triggered by a timer:
script: type = TIMER_SCRIPT; end
This header is followed by a section that contains the definition of variables:
variables: char data; end
The next section is the object definition section. Here we define the CAN message object:
objects: obj_can_msg can_msg; end
The following section already contains the executed code. This particular section is code that is executed once after the script is executed, so it is used for example to initialize variables, data items of objects, user controls, etc:
init: //Write text to the console of the script window logs("Script: Tutorial 1"); //Next line log_endl(); //We send a message with a standard identifier with a value of 10. //The 11 bit part is set to id1 of the CAN message object can_msg.id1 = 10; can_msg.id2 = 0; //The message length is 8 bytes can_msg.size = 8; //11 b -> false can_msg.stext = false; //This is not an RTR can_msg.rtr = false; //CAN port can_msg.port=1; //Initialization values of data bytes can_msg.set_data(0,0x00); can_msg.set_data(1,0x00); can_msg.set_data(2,0x00); can_msg.set_data(3,0x00); can_msg.set_data(4,0x00); can_msg.set_data(5,0x00); can_msg.set_data(6,0x00); can_msg.set_data(7,0x00); //Presetting the script execution timer to 100ms ui_set_timer_script_period (100); end
Now we only need to create the body of the script with the desired function:
body: //Reading the scrollbar control value = ui_get_val0(); //Scaling, scrollbar returns value 0..10000 //CAN message sends data in the range 0..100 value = value/100; //Setting the value to the corresponding byte of the CAN message can_msg.set_data(0,u2c(value)); //Setting bits in DB7 according to the value read from the scrollbar //and saving the value to the CAN message if(value>50) { can_msg.set_data(7,0x80); } else { can_msg.set_data(7,0x00); } //Sending a message to CAN can_msg.send(); end
The full file with this script can be downloaded here.