Vai al contenuto

How to initialize a structured type variable

Home Forum Programmazione IEC 61131 (LogicLab) How to initialize a structured type variable

Stai visualizzando 7 post - dal 1 a 7 (di 7 totali)
  • Autore
    Post
  • #81752
    George
    Partecipante

    My 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
    #81754
    Sergio Bertana
    Amministratore del forum

    Following 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_VAR

    In 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.

    #81762
    George
    Partecipante

    Thank 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).

    #83893
    George
    Partecipante

    Is it possible to declare a CONSTANT variable of structured data type?

    When I try to declare I get compilation error without error code.

    #83971
    Sergio Bertana
    Amministratore del forum

    Yes, 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
    #83988
    George
    Partecipante

    In 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.

    #84031
    Sergio Bertana
    Amministratore del forum

    Errors are generated only if you declare the MS data structure as CONSTANT.

    VAR CONSTANT
        MS : MYSTRUCT := (V1:=FALSE, V2:=[10,20,30,40]);
    END_VAR

    Without the CONSTANT attribute, the program works correctly.
    I have informed Axel about this problem and I am still waiting for their response.

Stai visualizzando 7 post - dal 1 a 7 (di 7 totali)
  • Devi essere connesso per rispondere a questo topic.