3

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

manuel2705
  • 45
  • 4
  • What is it that's not working? Is it not compiling? Do you get a runtime error? Without more details you're forcing anyone that might eventually help you to write an example him/herself. – Jakob Dec 28 '19 at 14:44

1 Answers1

4

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

Jakob
  • 1,288
  • 7
  • 13
  • Thank you, thats the Solution! I already knew about the syntax but somehow didn't really know how to use it. Now its working perfectly. I have a additional question: if i use the var_in_out variables of functionblocks does it allocate memory ? Or is it internally using pointers/references ? – manuel2705 Dec 28 '19 at 15:28
  • It does allocate memory, but only for a reference to the variable type you are passing in. So this is a reference that you are passing, without having to go through additional reference or pointer syntax. – Mark Lazarides Jan 07 '20 at 21:28