You can declare variables at the following locations:
-
Declaration part of a POU
The “Declare Variable” dialog helps you with this.
TIP:
If you define a variable in the tabular declaration editor, the correct syntax is automatically produced.
-
Declaration part of the GVL or NVL editor
-
I/O mapping configuration of an I/O device object
Syntax
( <pragma> )* <scope> ( <type qualifier> )? <identifier> (AT <address> )? : <data type> ( := <initial value> )? ; END_VAR
Declaration |
||
<pragma> |
Pragma (none, one, or multiple) Note: By adding a pragma, you can affect the behavior and the properties of one or more variables. |
|
|
Scope
|
|
|
Type qualifier
|
|
|
Identifier, variable name Note: The rules listed in the chapter "Identifiers" have to be followed without exception when assigning an identifier. In addition, you will find recommendations for uniform naming. |
|
|
Assignment of an address in the input, output, or flag memory range (I, Q, or M)
Example
|
|
|
Data Type
|
|
|
Initial Value
|
|
|
Optional |
|
|
Optional repetition |
Example
GVL
{attribute 'qualified_only'} {attribute 'linkalways'} VAR_GLOBAL CONSTANT g_ciMAX_A : INT := 100; g_ciSPECIAL : INT := g_ciMAX_A - 10; END_VAR
GVL_CONFIG
{attribute 'qualified_only'} VAR_CONFIG // Generated instance path of variable at incomplete address PLC_PRG.fbDoItNow.XLOCINPUT AT %I*: BOOL := TRUE; END_VAR
FB_DoIt (FB)
METHOD METH_Last : INT VAR_INPUT iVar : INT; END_VAR VAR_INST iLast : INT := 0; END_VAR METH_Last := iLast; iLast := iVar; FUNCTION_BLOCK FB_DoIt VAR_INPUT wInput AT %IW0 : WORD; (* Input variable *) END_VAR VAR_OUTPUT wOutput AT %QW0 : WORD; (* Output variable *) END_VAR VAR_IN_OUT aData_A : ARRAY[0..1] OF DATA_A; // Formal variable END_VAR VAR_EXTERNAL GVL.g_ciMAX_A : INT; // Declared in object GVL END_VAR VAR_STAT iNumberFBCalls : INT; END_VAR VAR iCounter: INT; xLocInput AT %I* : BOOL := TRUE; // VAR_CONFIG END_VAR iNumberFBCalls := iNumberFBCalls + 1;
PLC_PRG (PRG)
PROGRAM PLC_PRG VAR iLoop: INT; iTest: INT; fbDoItNow : FB_DoIt; iTest_200: INT; aData_Now : ARRAY[0..1] OF DATA_A := [(iA_1 := 1, iA_2 := 10, dwA_3 := 16#00FF),(iA_1 := 2, iA_2 := 20, dwA_3 := 16#FF00)]; END_VAR iTest := GVL.g_ciMAX_A; iTest_200 := 2 * GVL.g_ciMAX_A; fbDoItNow(aData_A := aData_Now); FOR iLoop := 0 TO GVL.g_ciSPECIAL DO ; END_FOR
-
Variable initialization