Search code examples
codesys

Can I use Structured Text in Codesys to initialize a Global Variable List


I'm trying to get my head into the Codesys world and since I'm not a Menu-click type I'm wondering if there is a way to stick to the keyboard and for instance add a PersistentVarsList and Global Vars List via Structured Text in the Editor.


Solution

  • If by add PersistentVarsList/Global Vars list you mean to crate a new list inside the project from Structured Text code, then no. If you just have some variable list (non constant!) inside your project that you want to initialize in runtime (perhaps their values need to be calculated first), then you can have some code that only executes at the begining of the runtime:

    PROGRAM POU_1
    VAR
        init: BOOL := FALSE;
    END_VAR
    
    IF (NOT init) THEN
        myGVL.someVar := CALLCULATE_VAR();
        IF (myPersist.positiveVar < 0) THEN
            myPersist.positiveVar := -myPersist.positiveVar;
        END_IF
        // other initializations
        init := TRUE;
    END_IF