Difference between Properties and own getter/setter

2019-07-08
2019-07-08
  • fabian.poettker - 2019-07-08

    Hey,

    I'm struggling with the properties of a FunctionBlock in CODESYS V3.5.

    I have a FB A, which instances two different FBs B and C.
    Now I want to change a property from FB B in FB C.

    The error message says, that the FB B is not an entry of FB A.

    If I write my own setter-method in FB B for the property and call it in the FB C it works and I don't get an error message.

    Can anybody explain why I'm able to call a function from the other FB but not a property?

    Thank you for your help!

    Fabian

     
  • Anonymous - 2019-07-08

    Originally created by: scott_cunningham

    Are you giving FB_C a reference to FB_A's instance of FB_B?

    FB_C knows nothing about FB_B, unless you define an FB_B instance in FB_C. But, since you want to have FB_C specifically affect FB_A's instance of FB_B, then you need to setup FB_C to have a REFERENCE TO FB_B, instead of it's own instance of FB_B.

    Silly example:

    Create FB_B with a simple Property that maps to a local variable...

    FUNCTION_BLOCK FB_B
    VAR_INPUT
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
       _Local : BOOL;
    END_VAR
    PROPERTY Prop1 : BOOL
    Get routine:
    Prop1 := _Local;
    Set routine:
    _Local := Prop1;
    

    Now setup FB_C so it can take somebody else's instance (aka REFERENCE TO FB_B). You could also do POINTER TO FB_B or use an interface... I will use REFERENCE TO...

    Here, FB_C will set FB_B's property "Prop1" to match "DoIt".

    FUNCTION_BLOCK FB_C
    VAR_INPUT
       DoIt : BOOL;
       refB : REFERENCE TO FB_B;
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
    END_VAR
    refB.Prop1 := DoIt;
    

    Now create FB_A that creates instances of both FB_B and FB_C and does something really silly...

    FUNCTION_BLOCK FB_A
    VAR_INPUT
    END_VAR
    VAR_OUTPUT
    END_VAR
    VAR
       MyB : FB_B;
       MyC : FB_C;
       Count : INT;
    END_VAR
    Count := Count + 1;
    IF Count > 20 THEN
       Count := 0;
    END_IF
    IF Count >= 10 THEN
       MyC(DoIt:=TRUE, refB:=MyB);
    ELSE
       MyC(DoIt:=FALSE, refB:=MyB);
    END_IF
    

    And call FB_A from PLC_PRG...

    PROGRAM PLC_PRG
    VAR
       MyA : FB_A;
    END_VAR
    MyA();
    
     

Log in to post a comment.