Join us today!
__NEW(FB_A) with FB_INIT Method?
Hello:
I would like to be able to create an FB_A with dynamic memory as an input value in the Methode of the FB_Init of another FB_B, I see that it cannot be done. Is there another way to achieve it?
I don't want to do it inside the FB_B the __NEW instruction...
{attribute 'enable_dynamic_creation'}
FUNCTION_BLOCK FB_A
===========================
FUNCTION_BLOCK FB_B
METHOD FB_init : BOOL
VAR_INPUT
bInitRetains : BOOL; // if TRUE, the retain variables are initialized (warm start / cold start)
bInCopyCode : BOOL; // if TRUE, the instance afterwards gets moved into the copy code (online change)
ppointer : POINTER TO FB_A;
END_VAR
============================
PROGRAM MAIN
VAR
fb_B : FB_B(ppointer:= __NEW(FB_A)); // not possible, other way to get it??
END_VAR
https://github.com/runtimevic
https://github.com/TcMotion
https://www.youtube.com/playlist?list=PLEfi_hUmmSjFpfdJ6yw3B9yj7dWHYkHmQ
https://github.com/VisualPLC
For some reason Tc says that result of __NEW() must be assigned. So it seems like it cannot be done that way. What you can do on the other hand is to assign initialization value of a variable instead of using FB_Init ...you know when you are doing:
x : int := 42;
So this works for me:
{attribute 'enable_dynamic_creation'} FUNCTION_BLOCK FB_A VAR value : BOOL; value2 : REAL := 42; END_VAR
FUNCTION_BLOCK FB_B VAR ppointer : POINTER TO FB_A; END_VAR // no FB_init() is used here!
PROGRAM MAIN VAR // fb_B : FB_B(ppointer:= __NEW(FB_A)); // your idea fb_B : FB_B := (ppointer := __NEW(FB_A)); // try this instead END_VAR
You can create a function to malloc the function block you want and assign it in the constructor.
// Allocates memory for FB_A FUNCTION F_MallocFB_A : POINTER TO FB_A VAR_INPUT nCount : __UXINT; END_VAR VAR nSize : __UXINT; END_VAR ---------------------------------------------------------------- nSize := SIZEOF(FB_A) * nCount; F_MallocFB_A := __NEW(BYTE, nSize);
In your MAIN program do:
PROGRAM MAIN VAR fb_B : FB_B(F_MallocFB_A(1)); END_VAR
- 17 Forums
- 265 Topics
- 932 Posts
- 5 Online
- 689 Members