0

Suppose I have a function block (A) that has defined the FB_init method, for example:

{attribute 'enable_dynamic_creation'}
FUNCTION_BLOCK A
  METHOD FB_init : BOOL
    VAR_INPUT
      bInitRetains : BOOL;
      bInCopyCode : BOOL;
      parameter: BOOL;
    END_VAR
  END_METHOD
END_FUNCTION_BLOCK

And I have another function block (B) from which I want to initialize this (A) FB dynamically:

FUNCTION_BLOCK B
  VAR
    a := POINTER TO A;
  END_VAR
  METHOD FB_init : BOOL
    VAR_INPUT
      bInitRetains : BOOL;
      bInCopyCode : BOOL;
      parameter: BOOL;
      somethingElse: INT;
    END_VAR
    a := __NEW(A); // No matching FB_init method found for instantiation of A
    a := __NEW(A(TRUE)); // Build returns errors
    a := __NEW(A(parameter := TRUE)); // Build returns errors
  END_METHOD
END_FUNCTION_BLOCK

I am unable to dynamically create an instance of the A function block. Is this even possible, or am I doing something wrong?

PS. I am using Schneider SoMachine V4.3

Guiorgy
  • 1,405
  • 9
  • 26

1 Answers1

2

You have error in function block B. I tried with TwinCAT 3 and it works.

Change

a := POINTER TO A;

to

a : POINTER TO A;

After that the following works:

A:

{attribute 'enable_dynamic_creation'}
FUNCTION_BLOCK A
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
END_VAR

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)
    parameter: BOOL;
END_VAR

B:

FUNCTION_BLOCK B
VAR_INPUT
END_VAR
VAR_OUTPUT
END_VAR
VAR
    a : POINTER TO A;
END_VAR

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)
    parameter: BOOL;
    somethingElse: INT;
END_VAR

a := __NEW(A(parameter := TRUE));
Quirzo
  • 1,183
  • 8
  • 10
  • Thanks for the response, but turns out Schneider doesn't support dynamic memory allocation. Still, thanks again! – Guiorgy May 30 '20 at 09:11
  • That's sad to hear. Hopefully in the future! – Quirzo Jun 01 '20 at 05:58
  • I moved to Schneider Machine Expert 1.2, which is their newer software, and looks like dynamic memory allocation works as intended. Thank you for your time! – Guiorgy Jun 07 '20 at 15:45