Creating and deleting own BACnet function blocks (FB)

If own BACnet FBs are to be instantiated in addition to the FBs available in the Tc3_BACnetRev14 library, these must be deleted in a method FB_exit when the PLC program is terminated so that the dynamically allocated memory is released again.

Dynamically created FB instances of function blocks from the Tc3_BACnetRev14 library are automatically deleted and removed from the memory via the method FB_exit of the Dynamic Object Manager.

The following example shows the dynamic creation of FB instances of own FBs (the implementations themselves are not shown in this example) as well as the deletion of these instances in the method FB_exit.

Creating and deleting own BACnet function blocks (FB) 1:

Care must be taken to ensure that the correct type of FB is used for the enable.

Variable MAIN

PROGRAM MAIN
VAR
    fbDynObj : FB_DYN_OBJECTS;
END_VAR

Code MAIN

fbDynObj();

Variables FB_DynObj

FUNCTION_BLOCK FB_DYN_OBJECTS
VAR
    DynMgmt : FB_BACnet_DynObjectManager := (bCycleObjects := TRUE, bAutoFinishInit := FALSE);

    bCreate : BOOL := TRUE;
    bDelete : BOOL;

    TestFbBVOwn : POINTER TO FB_BACnet_BV_Event;
    TestFbAVOwn : POINTER TO FB_BACnet_AV_EventSetp;
END_VAR

Code FB_DynObj

DynMgmt();
IF (DynMgmt.Ready) THEN
    IF (bCreate) THEN
        bCreate := FALSE;

        TestFbBVOwn := __NEW( FB_BACnet_BV_Event );
        IF (DynMgmt.CreateObjectEx(TestFbBVOwn, BACnet_Globals.nBACnetInstId_Auto, '\/TestBV own', '\/TestBV own', 0)) THEN
            // Initialize properties:
            TestFbBVOwn^.sInactiveText := 'AUS';
            TestFbBVOwn^.sActiveText := 'EIN';
        END_IF

        TestFbAVOwn := __NEW( FB_BACnet_AV_EventSetp );
    IF (DynMgmt.CreateObjectEx(TestFbAVOwn, BACnet_Globals.nBACnetInstId_Auto, '\/TestEvent own', '\/TestEvent own', 0)) THEN
            // Initialize properties:
            TestFbAVOwn^.fHighLimit := 470;
            TestFbAVOwn^.fLowLimit := -100;
    END_IF
        DynMgmt.FinishInit();
    END_IF

    IF (bDelete) THEN
        bDelete := FALSE;
    FB_exit(FALSE);
    END_IF
END_IF

Variables of the FB_DynObj.FB_exit method

METHOD FB_exit : BOOL
VAR_INPUT
    bInCopyCode : BOOL; // if TRUE, the exit method is called for exiting an instance that is copied afterwards (online change).
END_VAR

Code of the FB_DynObj.FB_exit method

DynMgmt.RemoveObjectEx(TestFbAVOwn);
__DELETE(TestFbAVOwn);
DynMgmt.RemoveObjectEx(TestFbBVOwn);
__DELETE(TestFbBVOwn);