Search code examples
plctwincatcodesys

TwinCat How To Initialize Functionblock with Reference to Global Variable


Hello StackOverflow Community,

i have another question regarding the TwinCat/Beckhoff/Codesys Programming Language, maybe someone is able to help me with this problem.

Here is the Problem: I want to initalize a functionblock with a reference to some variable. (In this example a simple bool). Hereby i want to make use of the FB_Init Method.

The Functionblock itself looks something like this:

FUNCTION_BLOCK PUBLIC FB_Ref
VAR
    reftoBool : REFERENCE TO BOOL;
END_VAR

The FB_Init Method looks something like this:

METHOD FB_init : BOOL
VAR_INPUT
    bInitRetains : BOOL := FALSE;
    bInCopyCode : BOOL := FALSE;
    reftoBoolIn : REFERENCE TO BOOL;
END_VAR



reftoBool := reftoBoolIn; 

The problem is that i can't get the code to work.. i don't know what im doing wrong.

Thanks in advance...


Solution

  • The problem is that you need to use REF= in the body of FB_init, like so:

    reftoBool REF= reftoBoolIn; 
    

    See documentation here: https://infosys.beckhoff.com/english.php?content=../content/1033/tc3_plc_intro/136301707.html