Server - Data access events

This example shows possible applications for data access events.

Download TwinCAT XAE Project (*.zip): Sample27.zip

The example described here uses the state machine that is described in the "General Server project structure" chapter. The States 0, 1, 10 and 100 are identical to the state machine described there. Other states were modified for the example or new states were also added.

When accessing a value in the data model via the property or communication services, user-specific behavior can be implemented by using the event methods. For this purpose, certain methods can be added to each data object function block, which are called by the Server application before and after write and read accesses. These are the following four methods:

Method name

Description

OnGetVPreEvent

Is called on read accesses before the data is read from the data model. In this method, the data to be read can be adapted or the read request can be rejected with an error code.

OnGetVEvent

Is called on read accesses after the data has been read from the data model. This method can be used to reset values set for a read request, for example.

OnSetVPreEvent

Is called on write accesses before the data is written to the data model. In this method, the data to be written can be checked and the write request rejected if necessary.

OnSetVEvent

Is called on in connection with write accesses after the data has been written to the data model. This method can be used to update other values according to the written value, for example.

The exemplary use cases for these methods as implemented in this example are explained in the following.

Substitution

The use of substitute values (substitution) can be implemented with the methods "OnGetVPreEvent" and "OnGetVEvent". In the example, this is implemented in the function blocks "FB_DO_IED_LD1_MMXU1_PhV_phsA" and "FB_DO_IED_LD1_MMXU1_PhV_phsB". In the first function block, if substitution is enabled, then the process value is overwritten with the substitute value in cases of read requests. In the second function block, the current process value is also cached in order to set it again after the read request.

Checking setting values

In the "FB_DO_IED_LD1_CCGR1_OilTmpSet" function block, the checking of write requests for setting values is implemented with the "OnSetVPreEvent" method. In the function block, the minimum and maximum valid value for the data attribute "setMag" can be set via the data attributes "minVal" and "maxVal". With each write request to "setMag", a check is now made to determine whether the value to be written is within the set range. If this is not the case, an error code is returned to the Server application. Then the value is not written and the error is sent to the Client.

Rounding of measured values

In the "OnSetVEvent" method, rounded measured values can be calculated and value ranges can be determined. This is implemented in the function block "FB_DO_IED_LD1_MMDC1_Vol". To do this, the value of the "IEDL1/MMDC1.Vol.instMag.f" data attribute is overwritten with a random value in the "FB_IEDServerSession" function block. With write access via the property, the "OnSetVEvent" method is called after the value has been transferred to the data model. In this, first the value of the data attribute "instMag.f" is rounded off and applied in the "mag.f" data attribute. Afterwards, the configured value ranges are used to determine in which value range the current value of "instMag.f" is located. In addition, a check is made to determine whether this is outside the defined range.