Skip to main content

Declaring Global Variables

Declaring global variables which are available within the application

Requirement: A project is open.

  1. In the Device tree of your project, select the application in which the global variables are to be valid.

  2. In the context menu, click Add Object → Global Variable List.

    CODESYS inserts the GVL in the device tree under the application and opens it in the editor.

  3. Click Edit → Auto Declare.

    The Declare Variable dialog opens.

  4. In the list box Scope, select the entry VAR_GLOBAL.

  5. In the field Name, enter a name for the global variable.

  6. Select a data type from the Data type list box.

  7. If your variable is to have an initialization value other than the standard initialization value, click _cds_icon_input_assistent.png next to the Initialization value field.

    The Initialization Value dialog opens.

  8. Double-click the Initialization value field of your variable and enter the desired valid value.

  9. Click OK.

    The initialization value is displayed in the Declare Variable dialog.

  10. Activate one of the flags if necessary.

  11. Confirm your entries by clicking the button OK.

    CODESYS inserts the declared variable in the GVL.

    The global variable is available in the total application of your project.

Declaring global variables which are available throughout the entire project

  1. Click View → POUs.

    The POUs view opens.

  2. In the POUs view, select the top node with the project name and click Add Object → Global Variable List.

    CODESYS inserts the GVL in the POUs view and opens it in the editor.

  3. Click Edit → Auto Declare.

    The Declare Variable dialog opens.

  4. In the list box Scope, select the entry VAR_GLOBAL.

  5. In the field Name, enter a name for the global variable.

  6. Select a data type from the Data type list box.

  7. If your variable is to have an initialization value other than the standard initialization value, enter it in the column Initialization.

  8. Activate one of the flags if necessary.

  9. Confirm your entries by clicking the button OK.

    CODESYS inserts the declared variable in the GVL.

    The global variable is now available in the entire project.