Creating a device function block

The following section illustrates how to create a device function block based on the Tc3_DALI library. The sample describes a simple function block that maps a DALI sensor (input device) with occupancy sensor (instance 0) and light sensor (instance 1).

Here you can find the TwinCAT project with the sample .

Creating a device function block 1:

The TwinCAT project is available for download as *.zip file. This must first be unpacked locally so that the archive (*.tnzip file) is available for import into the TwinCAT project.

Creating a device function block 2:

The current measured value is read directly from the respective instance via the inputs bQueryOccupancy and bQueryBrightness and output at the output bOccupied or nBrightnessLevel. During the reading process, the output bReadingOccupancy or bReadingBrightness = TRUE.

A positive edge at input bCancelHoldTimerOccupancy causes the DALI command CANCEL HOLD TIMER to be sent to instance 0 (occupancy sensor). This terminates the hold timer prematurely and, if there is no motion, sets the output bOccupied to FALSE.

Parameterization of the DALI control gears

The function block can initialize all relevant variables from the occupancy sensor (instance 0) and from the light sensor (instance 1). Corresponding properties are available for this purpose. Their values are written to the DALI control device when a positive edge is detected at bInitialize. During this time the output bInitializing is TRUE.

Creating a device function block 3:

Transfer of the reference to the communication block

When declaring the function block, the complete path of the communication block (FB_KL6811Communication, FB_KL6821Communication or FB_EL6821Communication,…) is specified in round brackets.

fbDALIApplicationExample   :  FB_DALIApplicationExample(Communication.fbKL6821Communication);

This is used to specify via which DALI terminal the function block is to communicate.

In FB_DALIApplicationExample the parameter is passed by the method FB_init. The method FB_init is called automatically by the TwinCAT 3 runtime environment, once before the PLC program is started.

The parameter is of type I_DALICommunication and is contained in the Tc3_DALI library. All communication blocks (FB_KL6811Communication, FB_KL6821Communication or FB_EL6821Communication, …) have implemented this interface. All DALI command function blocks (e.g. FB_DALI102Off) communicate with the DALI communication block via the interface pointer.

In the method FB_init the parameter is assigned to the variable _ipDALICommunication of the function block.

To ensure that the reference can be changed at runtime, the interface pointer is also passed to the device function block via the property ipDALICommunication. Internally, the property is stored in the variable _ipDALICommunication.

In the device function block it must be ensured that this interface pointer is passed on to all DALI command function blocks. The internal method SetCommunication is available for this purpose. In this method the interface pointer is passed to all DALI command function blocks located in the function block. The property ipDALICommunication is used in the command function blocks for this purpose. This property is used to reassign the reference to the DALI communication block at runtime.

Checking the parameters

All parameters are checked by the internal method CheckParameters. In the event of an error the method returns the error Id, or 0 if there is no error. The Ids of the runtime messages can be used here:

IF (THIS^.ipDALICommunication = 0) THEN
  CheckParameters := TC_EVENTS.TcDALIEventClass.TheInterfaceToTheCommunicationBufferIsNotInitialized.nEventId;
END_IF

If required, you can also use your own error Ids, starting at 1000:

IF (<check your parameter>) THEN
  CheckParameters := 1000;
END_IF

CheckParameters is called in the first PLC cycle and on a positive edge at input bInitialize.

In addition, the method CheckParameters is called as soon as a parameter has changed. The check for parameter change is done in the method ParameterChanged. If this returns TRUE, a parameter has been changed. Then the method CheckParameters is used to check whether all parameters contain correct values. If this is not the case, the function block is terminated.

IF (THIS^.ParameterChanged()) THEN
  nErrorId := THIS^.CheckParameters();
  bError := (nErrorId > 0);
  IF (bError) THEN
    bParameterInvalid := TRUE;
    RETURN;
  ELSE
    bParameterInvalid := FALSE;
  END_IF
END_IF
IF (bParameterInvalid) THEN
  RETURN;
END_IF

Structure of the function block

In the upper part of the function block, the parameters (CheckParameters) are checked when the function block is started for the first time and the interface pointer from the communication block (_ipDALICommunication) is transferred to the internal DALI function blocks (SetCommunication).

In the middle part there is a step sequence with which the DALI control device is initialized. The function blocks FB_DALI103ControlDevice, FB_DALI303OccupancySensor and FB_DALI304LightSensor are used for this purpose. At the end of the initialization, the current measured value is read from both instances. Initialization may only be started if no measured value is currently being read out (bReadingOccupancy and bReadingBrightness are FALSE).

In the lower part the notifications are evaluated and the reading of the measured values is executed by the inputs bQueryOccupancy and bQueryBrightness. The function blocks FB_DALI103ControlDevice, FB_DALI303OccupancySensor and FB_DALI304LightSensor are also used for this purpose. This area is only processed if initialization is not active.