I have problems to pass a Pointer during init to some FB's. Unfortunately, I cant post my whole application here, but my app structure is following:
My Main app has an array of independent units. All of these units are called once per cycle:
Program : PRGVAR
  //Arrayofunitfunctionblocks
  aUnits: ARRAY [1..8] OFFB_Unit;
  i:  USINT;END_VARFORi:=1to8DO
  aUnits[i]();END_FOR
Each of these units has some FB's which implement the main unit functionalities. These are also called on every unit call. Note, that a pointer to the unit is passed to the FB_Init of the FB's:
FUNCTION_BLOCK:FB_UnitVARÂ Â fbSomeFunction:Â Â FB_SomeFunction(Unit:=THIS);Â Â fbAnotherFunction:Â Â FB_AnotherFunction(Unit:=THIS);END_VARfbSomeFunction();fbAnotherFunction();
In these FB's init the pointer to the calling/defining unit is set. Although an array of subfunction is defined. Besides beeing called every cycle, they should also 'know' the pointer of 'their' corresponding unit:
FUNCTION_BLOCK : FB_SomeFunctionVAR
  pUnit:    POINTERTOFB_Unit;
  aSubFunctions:  ARRAY [0..10] OFFB_SubFunction(Unit:=pUnit);
  i:      USINT;END_VAR/////METHODFB_Init : BOOLVAR_INPUT
  bInitRetains:  BOOL;
  bInCopyCode:   BOOL;
   Unit:      POINTERTOFB_Unit;END_VARpUnit :=Unit;/////FORi:=0to10DO
  aSubFunctions[i]();END_FOR
The subfunction looks like this:
FUNCTION_BLOCK:FB_SubFunctionVARÂ Â pUnit:Â Â Â Â POINTERTOFB_Unit;END_VAR/////METHODFB_Init:BOOLVAR_INPUTÂ Â bInitRetains:Â Â BOOL;Â Â bInCopyCode:Â Â BOOL;Â Â Â Unit:Â Â Â Â Â Â POINTERTOFB_Unit;END_VARpUnit:=Unit;/////
My problem is, that SomeFunction initalizes correctly containing the correct pointer to the calling/defining unit, but the Subfunctions (array) does not. The unit pointer is null after init. How do I accomplish that the pointer is set correctly in the subfunctions?
I know, it is no problem to set this pointer in the first cycle programmatically via an input var, but my visualisation relies on calls based on the unit pointer and crashes when the unit pointer is null. Also some other init code relies on the correct reference of this unit pointer. Maybe my design isnt the best here.
Hope someone can help.
Thanks forward!
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Thanks for your reply! I solved my Problem by adding a (pointer to unit) property to the subfunction and set this in the FB_Init of "SomeFunction". This seems to work.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello @ all!
I have problems to pass a Pointer during init to some FB's. Unfortunately, I cant post my whole application here, but my app structure is following:
My Main app has an array of independent units. All of these units are called once per cycle:
Each of these units has some FB's which implement the main unit functionalities. These are also called on every unit call. Note, that a pointer to the unit is passed to the FB_Init of the FB's:
In these FB's init the pointer to the calling/defining unit is set. Although an array of subfunction is defined. Besides beeing called every cycle, they should also 'know' the pointer of 'their' corresponding unit:
The subfunction looks like this:
My problem is, that SomeFunction initalizes correctly containing the correct pointer to the calling/defining unit, but the Subfunctions (array) does not. The unit pointer is null after init. How do I accomplish that the pointer is set correctly in the subfunctions?
I know, it is no problem to set this pointer in the first cycle programmatically via an input var, but my visualisation relies on calls based on the unit pointer and crashes when the unit pointer is null. Also some other init code relies on the correct reference of this unit pointer. Maybe my design isnt the best here.
Hope someone can help.
Thanks forward!
Fb_init is called afer your variables inicialization. When you create your subfunction your fb_init in not called yet and the unit is not initialized
Hi Josep,
Thanks for your reply! I solved my Problem by adding a (pointer to unit) property to the subfunction and set this in the FB_Init of "SomeFunction". This seems to work.