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: //-----------------------------------------------------------------------
2: // Greenhouse_1.vpl, created 2000-12-31 14:45
3: //
4: //-----------------------------------------------------------------------
5: INCLUDE rtcu.inc
6:
7: //  Input variables that can be configured via the configuration dialog
8: VAR_INPUT
9:
10: END_VAR;
11:
12: //  Output variables that can be configured via the configuration dialog
13: VAR_OUTPUT
14:
15: END_VAR;
16:
17: // The global variables of the program
18: VAR
19:
20: END_VAR;
21:
22: PROGRAM Greenhouse_1;
23:
24:
25: // The next code will only be executed once after the program starts
26:
27: BEGIN
28: // Code from this point until END will be executed repeatedly
29:
30:
31: END;
32:
33: END_PROGRAM;

 

 

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: //-----------------------------------------------------------------------
2: // Greenhouse_1.vpl, created 2000-12-31 14:45
3: //
4: //-----------------------------------------------------------------------
5: INCLUDE rtcu.inc
6:
7: //  Input variables that can be configured via the configuration dialog
8:  VAR_INPUT
9:     Sensor_L   : BOOL; | Sensor input for low temperature
10:    Sensor_H   : BOOL; | Sensor input for high temperature
11:    Alarm_time : INT; | time, 1..546 minutes, time before SMS message is sent
12:   phone_number : STRING; | The number the SMS message should be sent to
13: END_VAR;
14:
15: //  Output variables that can be configured via the configuration dialog
16: VAR_OUTPUT
17:   Out_Heater     : BOOL; | Output to activate heater
18:   Out_Ventilation : BOOL; | Output to activate ventilation
19: END_VAR;
20:
21: // The global variables of the program
22: VAR
23:   timer : TON; // ON-delay timer is declared.
24:                 // A On-delay timer goes active when 'trig' has
25:                 // been active for 'pt' seconds.
26:   send_alarm : R_TRIG; // detects leading edge on alarm
28:
29: END_VAR;
30:
31: PROGRAM Greenhouse_1;
32: // The next code will only be executed once after the program starts
33:
34: BEGIN
35: // Code from this point until END will be executed repeatedly
36:
37:   // If the temperature is to high then activate ventilation:
38:   IF Sensor_H THEN
39:       Out_Ventilation:= ON;
40:   ELSE
41:       Out_Ventilation:= OFF;
42:   END_IF;
43:
44:   // If the temperature is to low then activate the heater:
45:   IF Sensor_L THEN
46:       Out_Heater:= ON;
47:   ELSE
48:       Out_Heater:= OFF;
49:   END_IF;
50:
51:   // Start timer on the leading edge of the sensor-inputs:
52:    timer(trig:= Sensor_L OR Sensor_H);
53:
54:   // Detect leading edge on alarm:
55:    send_alarm(trig:=timer.q);
56:
57:   IF send_alarm.q THEN
58:       IF Sensor_L THEN
59:         // send sms for temperature to low
60:         gsmSendSMS(number:=phone_number, message:="Temperature to low");
61:       ELSIF Sensor_H THEN
62:         // send sms for temperature to high
63:         gsmSendSMS(number:=phone_number, message:="Temperature to high");
64:       END_IF;
65:   END_IF;
66:
67: END;
68:
69: END_PROGRAM;

 

 

 

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:

 

 

Comments

Constants

INCLUDE

VAR

VAR_INPUT

VAR_OUTPUT

END_VAR

R_EDGE

F_EDGE

MODCALL