Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

STRUCTs in a GVL

raxyael
2021-03-26
2021-03-27
  • raxyael - 2021-03-26

    Hi everyone and thanks in advance for the replies. I need to rewrite an entire program in Codesys that was written in another program (let's just say that I have to migrate some Data Blocks).

    So I began to copy the program by the variables so I would have everything in place when I would later copy the code itself.
    I wrote down the variables that were in Data Blocks as variables in GVLs, and there was one thing I had to change. In the other program you can nest structures in data block/other structures, and in Codesys you can't do it. So i created an auxiliary folder and declared the STRUCTs I was going to use in the GVLs.

    Everything worked fine, the problem rose when I had to initialize some variables in the GVLs. In the GVL declaration I just wrote down the initial values of the STRUCT following the online helper format, compiled it and nothing went wrong.
    Now if I call a variable in a FB/PRG POU that has been initializated in the GVL it gives me this error: "No Matching "FB_Init" method found for instantiation of STRUCT..."

    I didn't found a useful solution for this problem and I don't really know what to do with the METHOD call. I hope that you can comprehend my explanation and if that's not the case let me know, I will be more than happy to try to explain it better.

     
  • i-campbell

    i-campbell - 2021-03-26

    Line 4 of your GVL, where you have
    ~~~ CL501 : STRUCT_CL501_HMI ( ~~~
    I think should read
    ~~~ CL501 : STRUCT_CL501_HMI := ( ~~~

     
    πŸ‘
    1
    • raxyael - 2021-03-26

      Thanks! Yep, that solved the first problem!
      Now I got another one, if you could solve it it would be great.

      In the DUT declaration I got some Arrays, and in the GVL I don't know how to initiate it, it gives me an error.

       
  • raxyael - 2021-03-26

    Found the solution! I'll post it.

    Thanks again.

    Ryan

     
  • rickj - 2021-03-27

    "In the other program you can nest structures in data block/other structures, and in Codesys you can't do it."

    Codesys does support nested STRUCTs as shown in the example below. Not sure where you went wrong but there is one quirk to be aware of. Codesys (sometimes or always?) doesn't compile and include entities in the build. Consequently, at least one variable defined in your GVL needs to be accessed in an executing program in order to view the GVL while Online.

    TYPE sSub :
    STRUCT
        Value   : INT;
    END_STRUCT
    END_TYPE
    

    Create sub STRUCT

    TYPE sMain :
    STRUCT
        Value   : INT;
        SubVal  : sSub;
    END_STRUCT
    END_TYPE
    

    Create main STRUCT

        NestedStruct    : sMain := (value:=1, SubVal:=(Value:=2));
    END_VAR
    

    Declare and Initialize in GVLVAR_GLOBAL

    Main_PROG
        VAR
            Val : INT;
        END_VAR
    
        VAL := NestedStruct.SubVal.Value;
    END
    

    Access any variable declared in GVL from a program attached to a task, otherwise compiler doesn't bother to compile it.

    Compile and go Online to see GVL data

     

    Last edit: rickj 2021-03-27
    • aliazzz

      aliazzz - 2021-03-27

      "In the other program you can nest structures in data block/other structures, and in Codesys you can't do it."

      To nuance, the IEC61131-3 norm (which CODESYS follows really well) does not allow type definition and instancing of DUT data types in a single go. If you wish to read up on this I can recommend this book: https://link.springer.com/chapter/10.1007%2F978-3-642-12015-2_8

       

Log in to post a comment.