Variable Declaration
Variable declaration: Where and how?
You can declare variables at the following locations:
Declaration part of a POU
The Auto Declare dialog helps you with this.
Variable lists (GVL editor, NVL editor)
I/O mapping configuration of an I/O device object
For more information, see: Configuring Devices and I/O Mapping
For more information, see: Shortcuts for Variable Declaration
Syntax
<pragma>
<scope> <type qualifier>
<identifier> AT <address> : <data type> := <initial value> ;
END_VAR
| Repeated optional (never, once, or multiple times) Pragma A pragma is a compiler statement (for example to achieve optimizations such as memory requirements or runtime improvements). | |
| Required Scope
| |
| Optional Type qualifier
| |
| Required Identifier, variable name Note: The rules listed in the chapter "Identifier Designation" have to be followed without exception when assigning an identifier. In addition, you will find recommendations for uniform naming. | |
| Optional The address is made up as follows: <memory area> <optional size prefix> <memory position> The memory area is subdivided into input memory area, output memory area, or flag memory area ( Example
| |
| Required Data Type
| |
| Optional Initial value; initialization as a literal, variable, or expression |
Global variable list GVL
{attribute 'qualified_only'}
{attribute 'linkalways'}
VAR_GLOBAL CONSTANT
g_ciMAX_A : INT := 100;
g_ciSPECIAL : INT := g_ciMAX_A - 10;
END_VARConfiguration variables GVL_CONFIG
{attribute 'qualified_only'}
VAR_CONFIG
// Generated instance path of variable at incomplete address
PLC_PRG.fbDoItNow.XLOCINPUT AT %I*: BOOL := TRUE;
END_VARFunction block FB_DoIt
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;IEC program PLC_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