This chapter provides further information on the following variable types:
VAR
local variables
VAR_INPUT
input variables
VAR_OUTPUT
output variables
VAR_IN_OUT
input and output variables
VAR_IN_OUT CONSTANT
transfer variable
VAR_GLOBAL
global variables
VAR_TEMP
temporary variables
VAR_STAT
static variables
VAR_EXTERNAL
external variables
VAR_INST
instance variables
VAR
Between the keywords VAR
and END_VAR
, all local variables of a POU are declared. These have no external connection; in other words, they cannot be written from the outside.
Consider the possibility of adding an attribute to VAR
.
Example
VAR
iLoc1:INT; (* 1. Local Variable*)
END_VAR
VAR_INPUT
Between the keywords VAR_INPUT
and END_VAR
, all variables are declared that serve as input variables for a POU. Therefore, at the call position, the value of the variables can be provided along with a call.
Consider the possibility of adding an attribute.
Example
VAR_INPUT
iIn1:INT (* 1. Inputvariable*)
END_VAR
VAR_OUTPUT
Between the keywords VAR_OUTPUT
and END_VAR
, all variables are declared that serve as output variables of a POU. Therefore, these values are carried back to the POU that makes the call.
Consider the possibility of adding an attribute to VAR_OUTPUT
.
Example
VAR_OUTPUT
iOut1:INT; (* 1. Outputvariable*)
END_VAR
Output variables in functions and methods:
According to IEC 61131-3 draft 2, functions (and methods) can have additional outputs.You can assign them in the call of the function as shown in the following example.
Example
fun(iIn1 := 1, iIn2 := 2, iOut1 => iLoc1, iOut2 => iLoc2);
VAR_IN_OUT
Between the keywords VAR_IN_OUT
and END_VAR
, all variables are declared that serve as input and output variables for a POU.
IN_OUT
type, the value of the transferred variable is changed (transferred as a pointer, Call-by-Reference). Therefore, the input value for such variables cannot be a constant. For this reason, even the VAR_IN_OUT
variables of a function block cannot be read or written directly from outside via <FBinstance>.<InOutVariable>
.
VAR_IN_OUT
parameters of function blocks. If any such assignments are detected, they are reported as a detected error in the view.
Example
VAR_IN_OUT
iInOut1:INT; (* 1. Inputoutputvariable *)
END_VAR
VAR_IN_OUT CONSTANT
A VAR_IN_OUT CONSTANT
variable serves as a constant pass-by-reference parameter. A variable or a constant (literal) of type STRING or WSTRING can be passed to this parameter. The parameter is read-only. Passing of properties is not permitted.
Syntax
VAR_IN_OUT CONSTANT
<variable name> : <data type>; // formal parameter
END_VAR
VAR_IN_OUT CONSTANT
variables are declared without assigning an initialization value.
Usage
When calling the POU, a constant variable or literal of type STRING or WSTRING can be passed. Write access is not permitted.
When passing parameters of a STRING constant, the length of string is not restricted. The string length does not depend on the string length of the VAR_IN_OUT CONSTANT
variables.
The variable is supported by EcoStruxure Machine Expert V2.0 and later versions.
Example: Passing parameters of STRING constants and STRING variables
FUNCTION funManipulate : BOOL
VAR_IN_OUT
sReadWrite : STRING(16); (* Can be read or written here in POU *)
dwVarReadWrite : DWORD; (* Can be read or written here in POU *)
END_VAR
VAR_IN_OUT CONSTANT
c_sReadOnly : STRING(16); (* Constant string variable can only be read here in POU *)
END_VAR
sReadWrite := 'String_from_POU';
dwVarReadWrite := STRING_TO_DWORD(c_sReadOnly);
PROGRAM PRG_A
VAR
sVarFits : STRING(16);
sValFits : STRING(16) :='1234567890123456';
dwVar: DWORD;
END_VAR
// The following line of code causes the compiler error C0417:
// C0417: VAR_IN_OUT parameter 'sReadWrite' needs a variable with write access as input.
funManipulate(sReadWrite:='1234567890123456', c_sReadOnly:='1234567890123456', dwVarReadWrite := dwVar);
// Correct code
funManipulate(sReadWrite := sValFits, c_sReadOnly := '23', dwVarReadWrite := dwVar);
funManipulate(sReadWrite := sVarFits, c_sReadOnly := sValFits, dwVarReadWrite :=dwVar);
In the code example, strings are passed to the funManipulate function via different VAR_IN_OUT
variables:
When passing a string literal to a VAR_IN_OUT
variable, a compiler error is issued.
When passing a constant variable to a VAR_IN_OUT CONSTANT
variable, correct code is generated. This even applies for passing string variables.
VAR_GLOBAL
You can declare normal variables, constants, external, or remanent variables that are known throughout the project as global variables. To declare global variables, use the global variable lists (GVL). You can add a GVL by executing the
command (by default in the menu).
Declare the variables locally between the keywords VAR_GLOBAL
and END_VAR
.
Consider the possibility of adding an attribute to VAR_GLOBAL
.
A variable is recognized as a global variable by a preceding dot, for example, .iGlobVar1
.
For detailed information on multiple use of variable names, the global scope operator dot (.)
and name spaces refer to the chapter Global Scope Operator.
Global variables can only be declared in global variable lists (GVLs). They serve to manage global variables within a project. You can add a GVL by executing the
command (by default in the menu).VAR_TEMP
This feature is an extension to the IEC 61131-3 standard.
Temporary variables get (re)initialized at each call of the POU. VAR_TEMP
declarations are only possible within programs and function blocks. These variables are also only accessible within the body of the program POU or function block.
Declare the variables locally between the keywords VAR_TEMP
and END_VAR
.
VAR_TEMP
instead of VAR
to reduce the memory space needed by a POU (for example inside a function block if the variable is only used temporarily).
VAR_STAT
This feature is an extension to the IEC 61131-3 standard.
Static variables can be used in function blocks, methods, and functions. Declare them locally between the keywords VAR_STAT
and END_VAR
. They are initialized at the first call of the respective POU.
Such as global variables, static variables do not lose their value after the POU in which they are declared is left. They are shared between the POUs they are declared in (for example, several function block instances, functions or methods share the same static variable). They can be used, for example, in a function as a counter for the number of function calls.
Consider the possibility of adding an attribute to VAR_STAT
.
VAR_EXTERNAL
These are global variables which are imported into the POU.
Declare them locally between the keywords VAR_EXTERNAL
and END_VAR
and in the global variable list (GVL). The declaration and the global declaration have to be identical. If the global variable does not exist, a message will display.
Example
FUNCTION_BLOCK FB_DoSomething
VAR_EXTERNAL
iVarExt1:INT; (* 1st external variable *)
END_VAR
VAR_INST
If you declare a variable of a method as an instance variable by using the VAR_INST
attribute, then this variable is not stored on the method stack but on the stack of the function block instance. It therefore behaves like other variables of the function block instance and is not reinitialized when the method is called.
VAR_INST
variables are only allowed in methods. You can access such variables within the method only. Attributes such as CONST
, RETAIN
are not allowed in the declaration. The values of the variables can be monitored in the declaration part of the method.
Example
METHOD meth_last : INT
VAR_INPUT
iVar : INT;
END_VAR
VAR_INST
iLast : INT := 0;
END_VAR
meth_last := iLast;
iLast := iVar;