2

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
clabacchio
  • 1,047
  • 1
  • 17
  • 28

2 Answers2

4

You can add default values to structure variables at struct declaration. You can also initialize variables with different values at variable declaration.

TYPE ST_Test :
STRUCT
    One     : INT   := 123;
    Two     : REAL  := 4567.5;
    Three   : STRING := 'Hello';
END_STRUCT
END_TYPE

In some block:

VAR
    //Original default values
    TestOriginalValues  : ST_Test;
    //Own initialization values
    TestOtherValues     : ST_Test := (One:=555, Two:=678.5, Three:='Whats up');
END_VAR
Quirzo
  • 1,183
  • 8
  • 10
1

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.

mrsargent
  • 2,267
  • 3
  • 19
  • 36