Skip to main content

Variable Configuration – VAR_CONFIG

You use the "variable configuration" to map variables of functions blocks to the process image. For declarations in the function block, you assign the variables to the device inputs/outputs without providing the full address. Then, the exact address is provided centrally for all function block instances of the application in a global variable list including VAR_CONFIG declarations. This global variable list with the VAR_CONFIG declarations is called the variable configuration.

Important

For changes to variables that are assigned to I/O addresses, CODESYS displays them immediately in the process image. For changes to variables that are mapped by a variables configuration, CODESYS displays them only at the end of the responsible task.

Declaration of variables in functions blocks

When declaring variables in a function block, you declare the variables between the keywords VAR and END_VAR and assign incomplete addresses to the variables. In doing so, you assign incomplete addresses to the variables. You mark these incomplete addresses with an asterisk (*).

Syntax:

<identifier> AT %<I\|Q>*:<data type>;

Example 19. Example

You define two local I/O variables: the input variable xLocIn and the output variable xLocOut.

FUNCTION_BLOCK locio
VAR
  xLocIn AT %I*: BOOL := TRUE;
  xLocOut AT %Q*: BOOL;
END_VAR


The final definition of the addresses is performed in the variable configuration in a global variable list.

In the global variable list that you use as the variable configuration, you define the variable declarations with the absolute addresses between the VAR_CONFIG and END_VAR keywords.

You must declare the VAR_CONFIG variables with the complete instance path, separating the individual POU and instance name by a dot. The declaration has to include an address whose class (input/output) matches the class of the incomplete address (%I*, %Q*) in the function block. The data type also has to match.

Syntax:

<instance variable path> AT %<I\|Q><location>: <data type>;

If the path instance does not exist, then an error is reported. CODESYS issues an error also if there is not an address configuration available for a variable that you declared with an incomplete address.

Example 20. Example

The locio function block in the example above is used in a program as follows:

PROGRAM PLC_PRG
VAR
  locioVar1: locio;
  locioVar2: locio;
END_VAR

A correct variable configuration in a global variable list could then look like this:

VAR_CONFIG
  PLC_PRG.locioVar1.xLocIn AT %IX1.0 : BOOL;
  PLC_PRG.locioVar1.xLocOut AT %QX0.0 : BOOL;
  PLC_PRG.locioVar2.xLocIn AT %IX1.0 : BOOL;
  PLC_PRG.locioVar2.xLocOut AT %QX0.3 : BOOL;
END_VAR


Creating a variable configuration

Requirements: You have a project open that includes a PLC configuration with a field device. The project contains a program (for example, PLC_PRG) and a function block (for example, func1). The field device has inputs and outputs. The textual view is selected in the options for the declaration editor.

In the function block, assign variables to device I/Os with incomplete addresses and then create a variable configuration.

  1. Double-click a function block in the device tree (for example, func1).

    The POU editor opens.

  2. In the declaration part, specify xLocIn AT %I*: BOOL := TRUE; between the VAR and END_VAR keywords and XLocOut AT %Q*:BOOL; in the next line.

    You have declared an input variable xLocIn and assigned it to the incomplete input address %I* of a field device. You have assigned the declared output variables have to the incomplete output address %Q*.

  3. Click the PLC_PRG object in the device tree and add the following to the declaration part of the program between VAR and END_VAR:

    locioVar1: func;

    locioVar2: func;

  4. In the device tree, right-click the Application object and click Add Object → Global Variable List, and then in the Add Global Variable List dialog click Add.

    The global variable list is added to the device tree and opens in the editor.

  5. Change the keyword VAR_GLOBAL to VAR_CONFIG.

  6. Click Declarations → Add All Instance Paths.

    The following instance paths are added:

    PLC_PRG.logioVar1.xLocIn AT %I*;
    PLC_PRG.logioVar2.xLocIN AT %I*;
    PLC_PRG.logioVar1.xLocOut AT %Q*;
    PLC_PRG.logioVar2.xLocOut AT %Q*;
  7. Now, replace the incomplete addresses %I* and %Q* with the absolute, complete addresses.