Instance-related Located Variables: 'Instance' and 'Program'
Note
Instance-related located variables as described here are not supported in safety-related POUs, i.e., for the Safety PLC. Therefore, the 'I/Q' variable property is not available in local variables tables and no 'Instance' or 'Program' declaration can be added to the Safety PLC Data List. |
The assignment of a variable to a process data item can only be made in the Data List of the resource ('IEC 61131-3') or the controller (see topic "Role Mapping in Data Lists"). In the Data List, resource-global variables are declared using the 'Global' keyword. After mapping a resource-global variable to a process data item, this variable is referred to as resource-global I/O variable and can be used in all POUs of the project.
If a program or function block is instantiated several times, such a resource-global I/O variable cannot be treated instance-related. Each instance that reads/writes this resource-global I/O variable accesses the same physical memory. However, instance-relation may be necessary if each instance of the same program or FB has to process different incoming or outgoing signals.
To ensure that each program/FB instance reads/writes the correct instance-related variables, instance-related I/O variables have to be used instead of Globals. Instance-related I/O variables can be realized in two ways:- as variables that result from local variables which have been declared in a program or FB POU using the 'Instance' keyword and with specified data direction. In the Data List, such variables automatically get the declaration keyword 'Instance'.
- as variables that result from program-global variables which have been declared in a program POU using the 'Program' keyword and with specified data direction. In the Data List, such variables automatically get the declaration keyword 'Program'.
Note
The difference between a local and a program-global instance-related variable is its visibility: a local 'Instance' variable can only be used within the POU where it is declared. In contrast, a program-global 'Program' variable can also be used in FB instances that are called in this program POU. For that purpose, the program-global variable must be declared as external variable in the called FBs. |
Local variable becomes instance-related 'Instance' variable
Proceed as follows to declare an 'Instance' variable for an FB:- Open the variables table of the FB POU which is to be instantiated several times.
- Insert the new variable and set the 'Usage' to 'Instance'.
The field 'I/Q' becomes active and the data direction 'I' is preset. - In the 'I/Q' column, specify whether the variable is to be connected to an input signal (I) or output signal (Q).Setting the data direction in the 'I/Q' column of the FB variables table automatically adds the local variable to the Data List of the resource.
- Double-click the 'IEC 61131-3' node in the PLANT and open the Data List. The variable is inserted here for each instance of the FB in which the variable is declared. Its 'Usage' is automatically set to 'Instance'.
Note
An 'Instance' variable can be initialized in the 'IEC 61131-3' Data List. An initial value set for the 'Instance' declaration overrules the initial value set in the local variables table (if defined).Assign a process data item to each instance of the variable.
Example: local instance-related variable
Program-global variable becomes instance-related 'Program' variable
Instead of a local I/O variable as described above, you can alternatively use a program-global variable. A program-global variable is declared in a program POU using the keyword 'Program'. The difference between a local I/O variable and a program-global I/O variable is that the program-global variable is also accessible from FB instances that are called from the program POU. For that purpose, the variable must be declared with the 'External' keyword in the called FB.
Proceed as follows to declare a program-global variable as 'Program' variable:- Open the variables table of the program POU which is to be instantiated several times.
- Insert the new variable and set the 'Usage' to 'Program'.
- In the 'I/Q' column, specify whether the variable is to be connected to an input signal (I) or output signal (Q).Setting the data direction in the 'I/Q' column of the program variables table automatically adds the program-global variable to the Data List of the resource.
- Double-click the 'IEC 61131-3' node in the PLANT and open the Data List. The variable is inserted here for each instance of the program in which the variable is declared. Its 'Usage' is automatically set to 'Program'.
Note
A 'Program' variable can be initialized in the 'IEC 61131-3' Data List. An initial value set for the 'Program' declaration overrules the initial value set in the local variables table (if defined).Assign a process data item to each instance of the variable.
Note
'Program' variables can also be forced while debugging the application. |