How to initialize a structured type variable
Home › Forum › Programmazione IEC 61131 (LogicLab) › How to initialize a structured type variable
- Questo topic ha 7 risposte, 2 partecipanti ed è stato aggiornato l'ultima volta 1 mese, 1 settimana fa da
Sergio Bertana.
-
AutorePost
-
Aprile 29, 2025 alle 11:36 am #81752
George
PartecipanteMy question is about the way in LogicLab (v10.0.0) when I declare a variable of structure type to set initial values to the elements of a structure that are structure type.
For example:
sX: STRUCT a : INT; b : REAL; END_STRUCT;sY: STRUCT c : BOOL; d : DWORD; END_STRUCT;str: STRUCT x : sX; y : sY; END_STRUCT;sVar:str
Aprile 29, 2025 alle 11:38 am #81754Sergio Bertana
Amministratore del forumFollowing your example in the Init value field of the sVar declation you must define the init values as reported
VAR sVar : str := ( x:=(a:=100, b:=1.7), y:=(c:=TRUE, d:=16#12345678) ); END_VARIn the screenshot you can see the program with the declaration and during the program execution the values on watch window.
As you see I used the variable sVar in the program to enable LogicLab to init it. If not used in the program it has not initialized. The variable sVar.x.a is increased at first program loop execution so it init value of 100 has becomed 101.
Aprile 29, 2025 alle 12:52 pm #81762George
PartecipanteThank you very much. this was the information I was missed. It was not so clear in the LogicLab manual (6.6.7 SETTING VARIABLES INITIAL VALUE).
Agosto 31, 2025 alle 4:17 pm #83893George
PartecipanteIs it possible to declare a CONSTANT variable of structured data type?
When I try to declare I get compilation error without error code.
Settembre 8, 2025 alle 10:15 am #83971Sergio Bertana
Amministratore del forumYes, it is possible. The error you mentioned is probably this:
MyProgram.MS – warning G0290: MS => Constant without initial value
It has no reference number, but the description indicates that the constant doesn’t have any initial value. To declare a structured data type, you must also assign it a value.
- You can initialize values in the data structure definition as follows:
MYSTRUCT: STRUCT V1 : BOOL := TRUE; V2 : UINT := 20; END_STRUCT; - Or you can initialize the value directly when you instantiate the data structure variable in the program, like this:
VAR CONSTANT MS : MYSTRUCT := (V1:=FALSE, V2:=30); END_VAR
Settembre 15, 2025 alle 10:29 am #83988George
PartecipanteIn my project have a structure like:
MYSTRUCT: STRUCT V1 : BOOL; V2 : ARRAY[0..3] OF UINT; END_STRUCT;If I intitialize in a program it:
VAR CONSTANT MS : MYSTRUCT := (V1:=FALSE, V2:=[10,20,30,40]); END_VAR VAR myS : MYSTRUCT; END_VAR myS:= MS; myS.V1:= MS.V1; // [End of file]I get errors
MyProgram(2) - error G0291: MS => Invalid constant value MyProgram(3) - error G0291: MS.V1 => Invalid constant value
I think this error has to do with constant variable instantiation of structure data type containing array element.
Settembre 19, 2025 alle 9:00 am #84031Sergio Bertana
Amministratore del forumErrors are generated only if you declare the MS data structure as CONSTANT.
VAR CONSTANT MS : MYSTRUCT := (V1:=FALSE, V2:=[10,20,30,40]); END_VARWithout the CONSTANT attribute, the program works correctly.
I have informed Axel about this problem and I am still waiting for their response.Settembre 22, 2025 alle 5:21 pm #84089Sergio Bertana
Amministratore del forumAxel has just informed me that this behaviour is a bug. They have opened ticket:
LL-3790: Invalid constant value for structured types.
A fix will be available in future versions of LogicLab.
- You can initialize values in the data structure definition as follows:
-
AutorePost
- Devi essere connesso per rispondere a questo topic.