Search code examples
data-structuresplcst

PLC-ST: How to initialize a structure in declaration?


According to the PLCOpen, IEC-61131 standard, is it possible to initialize a structure in the declaration?

I'm thinking of something along the lines of this C++ question.


Solution

  • The c++ example you gave is where they are defining a struct in a function. The equivalent component in IEC61131 would be a function block. You can initialize a struct in a function block

    FUNCTION_BLOCK SampleFunctionBlock
    VAR_INPUT
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
        internalBlockStruct:SampleStruct:=(One:=1,Two:=2,Three:=3);
    END_VAR
    

    and then use this struct in the function block code. You can also initialize a struct in a pou if you wanted to by following the same syntax.

    for reference this is my struct

    TYPE SampleStruct : STRUCT One:INT; Two:INT; Three:INT; END_STRUCT END_TYPE

    note: the example I gave is using Codesys syntax. Most IEC61131 languages have very similiar syntax.