Indirect Variables
Indirect variables are a Geo SCADA Expert specific extension to the IEC 1131-3 language. They allow an ST Program to perform calculations on tags that are specified at runtime as opposed to hard coded tags that are used at compile time. At runtime, the program will either prompt the user to enter a string (the string is the tag of an item in the database) or will use a string from another program and use the string to calculate the tag for a direct variable. For example, the user could enter the name of a point and the point would be used in the ST program's calculation. Indirect variables are normally used with ST Programs that are triggered by schedules and method pick actions.
To enter an indirect variable, use this format:
VAR_INPUT
<Name of Variable> : STRING;
END_VAR
VAR
<Name of Variable> AT<AccessType>(<Parameter>{Input Variable}<Parameter>): <TYPE>;
END_VAR
where:
- The VAR_INPUT declaration defines an input variable that is a String type.
- The VAR declaration defines the indirect variable:
- <Name of Variable> is the name that you allocate to the indirect variable. Do not enter the angle brackets < >.
- AT <AccessType> defines whether the indirect variable is read only (%I), write only (%Q), or read and write (%M).
- <Parameter> defines part of a tag. This is coded into the program and is not affected by the user input/input from another program for the indirect variable. The <Parameter> parts of the indirect variable are optional.
- {Input Variable} represents the part of the tag that will be substituted by the string that is entered by the user or provided by another program. The input variable has to match the name of the input variable defined in the VAR_INPUT declaration and has to be enclosed in braces { }.
When you include SQL queries in ST programs, and use braces '{' and '}' within SQL statements, you should use the caret '^' escape character before each brace.
For example:
VAR NOCACHE
rsPoints AT %S(SELECT ID, CurrentValueFormatted, CurrentTime FROM CSIMPLEPOINT WHERE CURRENTTIME = ^{ OPC 'D' ^}): RESULTSET OF Point;
END_VAR
If you reference system status tags in your ST programs, you need to prefix the names of those system status tags with an additional caret '^' escape character. This escapes the reserved caret '^' character that is used at the start of the names of system status tags (also known as 'status attributes') (see Access System Status Information).
For example:
VAR
sServerStateTime AT %M(^^<server name>.ServerStateTime): DATE_AND_TIME;
END_VAR
- <TYPE> is the type of the internal variable, for example, STRING.
Example 1: Using Indirect Variables to Utilize Values provided by a User
This example demonstrates a very simple use of indirect variables. It uses an ST program that adds the values of 2 inputs and then writes the result to an output point. The inputs are provided by a user - when the ST program is executed, it prompts the user to define the source of the input values.
For this example, the following database items are created:
- An ST Program
- Three Internal Analog Points (named Analog Point 1, Analog Point 2, and Analog Point 3)
- A Mimic
The ST Program is configured to use indirect variables as follows:
PROGRAM Addition
VAR_INPUT
Input1 : STRING;
Input2 : STRING;
END_VAR
VAR
Point1 AT %I(.{Input1}.CurrentValue) : LREAL;
Point2 AT %I(.{Input2}.CurrentValue) : LREAL;
Output AT %M(.Analog Point 3.CurrentValue) : LREAL;
END_VAR
Output := Point1 + Point2;
END_PROGRAM
The indirect variables are '.{Input1}.CurrentValue' and '.{Input1}.CurrentValue'. The { } instruct Geo SCADA Expert to prompt the user for the source of the inputs. The period before the { } means that Geo SCADA Expert will search for the source within the same Group that contains the ST program.
When executed, the program will add the current values of Input 1 and Input 2 and then write that value to the 'Analog Point 3' point.
The Mimic is configured so that it contains a button that has shows ‘Total Values’ and has a pick action animation. When the button is selected, it will trigger the Execute action for the ST Program. A text box is added to the Mimic and is configured to have a Value animation that sets the text box to display the CurrentValue property of the 'Analog Point 3' internal point..
A user selects the 'Total Values' button, which triggers the Execution action for the ST Program. Geo SCADA Expert prompts the user to define Input 1 and Input 2 for the program.
In the Input 1 field, the user enters:
Analog Point 1
In the Input 2 field, the user enters:
Analog Point 2
The user then selects the OK button and the ST Program executes. It reads the current values of Analog Point 1 and Analog Point 2, adds the two values together, and then writes the result to Analog Point 3.
The text box on the Mimic is animated to show the current value of Analog Point 3, which means it displays the result of the ST Program (which was written to the Analog Point 3 point). So if Analog Point 1’s current value was 10 and Analog Point 2’s current value was 8, the ST program would write the value 18 to Analog Point 3 and this would be shown in the text box on the Mimic.
Example 2: Using Indirect Variables to Utilize Values provided by another Program
This simple example shows how indirect variables can be used to allow an ST program to use values that are provided by another ST program.
Program 1 has the following code:
PROGRAM Program1
METHOD
Exec AT %M(.Program2.Execute):STRING;
END_METHOD;
Exec ('Analog Input Point');
END_PROGRAM
The first program is named Program1 and is used to perform a method. The method is named Exec within the ST program. The Exec method declaration sets the method to Execute argument. This means that the program will write a value. The value is written to the Program2 program (which is the other ST program) which is also defined in the method declaration. The type of value that will be written is a STRING.
The method declaration is finished with the END_METHOD keyword.
The Exec ('Analog Input Point'); line instructs the Exec method to write Analog Input Point as the string value. This means that when Program 1 is executed, it will write an Analog Input Point string to the Program2 program.
The Program2 program contains the following code:
PROGRAM Program2
VAR_INPUT
PointName :STRING;
END_VAR
VAR
CurValue AT %M({PointName}.CurrentValue): LREAL;
END_VAR
CurValue:=24.4;
END_PROGRAM;
Program 2 has two variables. The first is PointName, which is a string value. The VAR INPUT keyword instructs the program that it is receiving an input value (the input value is received from Program 1). In Program 2, the input value is called PointName and it is a string. The data type for the VAR INPUT has to match the data type for the output of Program 1.
Program 2 automatically detects that it has a string input from Program 1 (there is only 1 input).
The second variable is CurValue which is the CurrentValue of the item that has the same name as the PointName variable. In this case, it means that the CurValue is the value that is written to the current value of the database item that is named 'Analog Input Point'. The CurValue is a long real value.
The variable declarations are ended by END_VAR.
The CurValue :=24.4; line sets the value that is written to the current value of the database item that is named 'Analog Input Point'.