Anatomy of a VPL program |
Top Previous Next |
Below is seen a typical VPL program. We will describe each of the sections of the program. Please note that the red numbers (nn:) to the left are not part of the code and are only included to make it easier to reference the individual lines in the description below.
1: //-----------------------------------------------------------------------
Line 1..4 This is a comment. Comments can start with a "double slash" (//) and continue to the end of the line. It is also possible to use the /* to start and */ to finish the comment. This way, comments can extend over more than one line, and this form of comments are useful when excluding parts of the program code - for example during program testing. Line 5 This is a INCLUDE statement. This is used to include other files in a program. It includes the definitions of all the built-in functions, function blocks, and Platform support functions. Line 8 This starts the VAR_INPUT section. In this section, all variables that are inputs to the program, and that need to be able to be configured from the configuration dialog, are declared. Please note that all variables declared within this section are global and can be accessed from any section of the VPL code. Line 10 This ends the VAR_INPUT section. Line 13 This starts the VAR_OUTPUT section. In this section, all variables that are outputs to the program, and that need to be able to be configured from the configurationdialog are declared. Please note that all variables declared within this section are global and can be accessed from any section of the VPL code. Line 15 This ends the VAR_OUTPUT section. Line 18 This starts the VAR section. In this section, all variables that are global for the program are declared. Line 20 This ends the VAR section. Line 22 This defines the name of the program. Line 27 This starts the main program. The code from the BEGIN to the END statement are executed repeatedly. Line 31 This ends the section that started with the BEGIN statement. Line 33 This is the end of the program.
Next we will show a more complete program. This program is taken from the tutorial section of the online help manual. Following the code, an explanation of the various parts of the program are explained. Please note again that the red numbers (nn:) to the left are not part of the code and are only included to make it easier to reference the individual lines in the description below.
1: //-----------------------------------------------------------------------
Line 8..13 This starts the VAR_INPUT section. In this section, all variables that are inputs to the program, and that we need to be able to configure from the configurationdialog, are declared. We declare two variables of the type BOOL. They are variables that can only hold either "1" or "0" (you can use ON/OFF and TRUE/FALSE instead of 1/0). The variable "Alarm_time" is a variable of the type INT. The variable "phone_number" is a variable of the type STRING, and in this example it contains a telephone number. Line 16..19 This starts the VAR_OUTPUT section. In this section, all variables that are outputs from the program, and that we need to be able to configure from the configurationdialog, are declared. We declare two variables of the type BOOL. They are variables that can only hold a "1" or "0" (you can use ON/OFF and TRUE/FALSE instead of 1/0). Line 22..29 This starts the VAR section. In this section, all variables that are global in the program are declared. We declare "timer" as a variable of the type TON. TON is a function block. By writing the statement in line 23, we instantiate the function block TON and give this instantiation of the function block the name "timer". The variable "send_alarm" is an instantiation of the function block R_TRIG. Line 38..41 This is an example of an IF statement. Line 38 is executed if the variable "Sensor_H" is active (1/ON or TRUE) otherwise line 40 is executed. Line 52 This is a way of "connecting" the two inputs "Sensor_L" and "Sensor_H" to the trigger signal of the timer TON function block (instantiated by the "timer" variable). The "trig" input of the function block "timer" will be true in relation to either of the two variables (because of the OR operator). Line 55 This is an example of reading a VAR_OUTPUT variable from a function block and using it as a signal for a VAR_INPUT variable of another function block. Line 60 This is a call of a function (gsmSendSMS). When a function is called, the control is given to the function. The calling program will not gain control again before the function returns control to the caller. The arguments to a function (and a function block) can be listed in random order, and the order is not significant. When a parameter is not listed, the VAR_INPUT variable of the function / functionblock will keep its default value.
For further study, please have a look at:
•VAR |