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

structured variable as property

tvm
2017-09-11
2017-09-13
  • tvm - 2017-09-11

    I've been trying to define a property as a structured variable. It seems that it's not possible to do it directly (see l viewtopic.php?t=5577 l ), so I followed the advice in that thread and used a reference. That appears to work as far as programming and compiling goes, however, I'm not seeing the variables when I go online with the controller. The little + sign is there, but when I click on it it disappears, and there's no variables. I've used both {attribute 'monitoring':='variable'} and {attribute 'monitoring':='call'}. "variable" will crash the controller with some of the larger structures.

    TYPE STRUCTUREDVARIABLE :
    STRUCT
       Status1: INT;
       Status2: BOOL;
    END_STRUCT
    END_TYPE
    {attribute 'monitoring':='variable'}
    PROPERTY Status : REFERENCE TO STRUCTUREDVARIABLE
    (Get)
    Status REF= _Status;
    FUNCTION_BLOCK TestPOU
    VAR
       _Status: STRUCTUREDVARIABLE;
    END_VAR
    

    IMG: Capture.PNG

     
  • Anonymous - 2017-09-11

    Originally created by: scott_cunningham

    Have you called the property? The code doesn't run (i.e. Status is referring to nothing until you call it!). Also, I get a program exception as soon as a assign the attribute monitor to the property (probably because it is not initialized to refer to anything).

    I duplicated your project and it is working (I forced some values into _Status):

    PROGRAM PLC_PRG
    VAR
       Test : MyFbWithRef;
       Res : T_STRUCT;
    END_VAR
    Res := Test.Status;
    
    FUNCTION_BLOCK MyFbWithRef
    VAR
       _Status : T_STRUCT;
    END_VAR
    PROPERTY Status : REFERENCE TO T_STRUCT
    (GET)
    Status REF= _Status;
    
    TYPE T_STRUCT :
    STRUCT
       Status1: INT;
       Status2: BOOL;
    END_STRUCT
    END_TYPE
    

    Here is my result:

    IMG: before

    IMG: after

     
  • tvm - 2017-09-12

    So if I understand correctly, a property isn't initialized until it is called from somewhere outside the function block in which it resides. If that's the case I might have to revise my design, as a lot of the status variable properties are strictly for display on an HMI.

     
  • Anonymous - 2017-09-13

    Originally created by: scott_cunningham

    Properties are like a special get method or set method. They actually run as code when you call them (you can see when you online debug and step through).

    I would probably avoid using properties for HMI use - I typically define a structure T_HMI and list out the variables I am transferring and the. Have one program that copies speaking variables from different parts of the program into the one structure. Use VAR ins/outs and inOuts for linking to HMIs.

    I typically define the HMI structure as global and then in my FBs that either give or receive data from the HMI, I define REFERENCE TOs on var ins, car outs or var inouts. Then I set the reference to the element in the global structure.

    But that is, of course, only my way of doing things.

     

Log in to post a comment.