Understanding the Basic Structure of an ST Program
Before you begin to create your own ST programs, you need to be aware of the basic structure of an ST program.
- The start of the program.
ST programs begin with a PROGRAM keyword and a program name. This is followed by a VAR keyword and a list of variables.
- The variable blocks.
There are 2 types of VAR block—VAR blocks for internal variables and VAR blocks for direct and indirect variables. Variables of different types have to be defined in separate blocks.
For internal variables, the VAR block will begin with a VAR keyword. The variables are defined after the VAR keyword and use the format:
- <name> : <type>;
For direct and indirect variables, the VAR block will also begin with a VAR keyword. The variables are defined after the VAR keyword and use this format:
- <name> <instruction> (<item>.<variable>) : <variable type>;
For example:
- VAR
- Desc AT %I(.Valve.Position.CurrentStateDesc) : STRING;
- END_VAR
Each VAR block is ended by an END_VAR keyword.
When you are using variables, you need to:
- Define the type for any arguments
- Separate each argument type with a comma, for example, STRING, BYTE,STRING; means there are 3 arguments, the first is a string, the second is a byte, and the final argument is a string.
- Use VAR INPUT definitions to allow the ST program to receive inputs from a user/another ST program. The VAR INPUT keyword needs to be included in the recipient program (so, if one ST program receives the inputs for its indirect variables from another program, the program that receives the inputs has to have VAR INPUTS).
- Define the VAR INPUTS in the same order in which they are to be executed.
To allow a program to use the values from another program, the arguments for the indirect variables’ inputs have to be in the same order in both programs. For example, Program 1 defines a method that has outputs in this order: STRING, BYTE. These values are output to Program 2. In Program 2, the VAR INPUT definition has to define that the first argument is a STRING and the second argument is a BYTE as this is the order defined in Program 1.
- Set the Interval to 0s on the ST Program Form for the recipient ST program. Programs that use manually entered input values or input values from other programs cannot be executed on an interval basis. However, they can be executed by another program (the other program executes the recipient ST program at a regular interval), according to a schedule, and manually. If required, configure a schedule to execute the program at specific times (see Using Schedules to Automate Regular Functions in the ClearSCADA Guide to Core Configuration).
If you execute a recipient ST program manually, you will be prompted to enter the input value. You need to enter the input value in the dialog box and select the OK button to proceed. You will only be prompted to enter a value if the recipient program has inputs defined, but the inputs are not associated with other programs.
- The method block.
If an ST program uses methods, the methods are defined in a METHOD block. The METHOD block has to follow the VAR block(s) and also come before the ST code for the program. If the ST program does not use methods, there is no METHOD block and the ST code for the program follows the last VAR block.
The METHOD block begins with a METHOD keyword. The methods that the program uses are listed and use this format:
- <name> <instruction> (<item>.<method>) : <argument types>;
For example:
- METHOD
- OVR AT %M(AIs.AnalogInp.Override) : LREAL;
- END_METHOD
The METHOD block is ended by an END_METHOD keyword.
- The ST code for the program. This can include statements, expressions, and operators.
- The end of the program.
Each ST program is ended with the END_PROGRAM keyword.
Example:
In the following basic example, the ST program contains 3 variables and 3 methods. ST programs can contain as many variables and methods as required.
For the example, entries in angle brackets < > indicate a name or type that you have to enter for the program. These will vary depending on the items you want to reference and the names you want to use. You should not enter the angle brackets in your ST programs—we use them here to indicate that you need to specify a name, location, or data type that will vary according to your needs.
- PROGRAM <program name>
- VAR
- <name> : <type>;
- <name> : <type>;
- <name> : <type>;
- END_VAR
- METHOD
- <name> AT %M(<location and name of item>.<method>) : <argument types>;
- <name> AT %M(<location and name of item>.<method>) : <argument types>;
- <name> AT %M(<location and name of item>.<method>) : <argument types>;
- END_METHOD
- <ST code>;
- <ST code>;
- <ST code>;
- VAR
- END_PROGRAM
The ST program begins with a PROGRAM definition and the program name. This is followed by a VAR definition, and the names and types of the variables that are used by the program. The list of variables is referred to as a VAR block, and it is ended by an END_VAR definition.
If the ST program uses methods, a METHOD block is defined after the VAR block. The METHOD block consists of the METHOD keyword followed by the name of a method, an instruction, the location and name of the item to which the method will be applied, the name of the method to be applied, and the argument types that will be used (argument types are types of data for specific settings, for example, the Override method needs an argument type that specifies the type of value for the override value). The METHOD block is ended with an END_METHOD keyword.
The ST code that defines the behavior that the program provides is entered after the METHOD block (or after the VAR block if there is no METHOD block).
The program is ended by the END_PROGRAM keyword.
NOTE: Constants also need separate VAR blocks—see Constants.