Using IEC61131-3 syntax, variables are declared within structured blocks. Each blocks begins with "VAR", "VAR_INPUT", "VAR_OUTPUT" or "VAR_EXTERNAL" keyword and ending with "END_VAR" keyword (with no semicolon after). Below is the meaning of each keyword:
VAR | Memory variables. Can be global, local or RETAIN depending on the edited group |
VAR_INPUT | Input parameters of a block. Available only when the edited group is a UDFB. |
VAR_OUTPUT | Output parameters of a block. Available only when the edited group is a UDFB. |
VAR_EXTERNAL | External variables. Can be global or local depending on the edited group |
Basic syntax for declaring a variable:
To declare a variable, simply enter its symbol, followed by ":" and its data type. If the data type is STRING, it must be followed the maximum length between parenthesis. Example:
MyVar : BOOL;
MyString : STRING(255);
To indicate that a variable has the "read only" attribute, insert the "CONSTANT " keyword at the beginning of the variable declaration:
CONSTANT VarName : DataType;
To declare an array, the data type must be preceeded by "ARRAY [ dimensions ] OF". There are at most 3 dimensions, separated by comas. Each dimension is specified as "0 .. MaxBound". Below are examples:
Array1 : ARRAY [0 .. 99]
OF DINT;
Matrix : ARRAY [0 .. 9, 0 .. 9, 0 .. 9] OF
REAL;
Additionaly, you can specify an initial value for single variables. The initial value is entered after the data type, and is preceeded by ":=". The initial value must be a valid constant expression that fits the data type. Examples:
MyBool : BOOL := TRUE;
MyString : STRING(80) := 'Hello';
MyLongReal : LREAL := lreal#1.0E300;
Additional information and description texts:
As a variable may have additional properties and comment texts in the Workbench, we use special directives entered as IEC comments AFTER the declaration of the variable, to specify additional info. The following directives are available:
(*$tag=Text*) | Variable tag (short comment) |
(*$desc=Text*) | Variable description |
(*$profile=ProfileName*) | Variable embedded profile |
(*$embed=Text*) | Variable embedded properties (the syntax is the one shown in the variable grid, in the "Property" column) |
You can also use "//" single line comments to enter the directives:
//$tag=Text
//$desc=Text
//$profile=ProfileName
//$embed=Text