In the runtime system different events occur. Each event can be assigned to an event class. Further on each event arises from a certain source. The runtime system has an internal list of registered callback definitions. At each occurring event the runtime system checks its list of callback definitions and if applicable calls a callback function.
A callback definition consists of:
Event e of class c from source s triggers the callback (e, c, s), if
(e = e OR e = CB.ALL_EVENTS) AND
((c AND c) > 0 OR c = CB.ALL_CLASSES) AND
(s = s OR s = CB.ALL_SOURCES)
A function which should be used as callback function, must have the following interface:
FUNCTION Callback<Name> : BOOL
VAR_INPUT
dwSpec : DWORD; (* CB.Event and CB.EventClass *)
dwParam : DWORD; (* Application specific Parameter *)
dwSource : DWORD; (* CB.EventSource *)
END_VAR
The data types are DWORDs in order to be compatible to CoDeSys V2.3. These however refer to the enumerations specified in brackets. Parameter dwSpec via function ⇘ CB.DecodeEvent and ⇘ CB.DecodeClass can be split in two variables of type ⇘ CB.Event and ⇘ CB.EventClass. The parameter dwSource can be converted to data type ⇘ CB.EventSource with the help of the DWORD_TO_INT conversion.
Remarks on the structure an the usage of callback functions:
The function name must have a prefix „Callback”.
The function must not contain any local variables.
The function must not be checked with breakpoints, ....
The function muss be parameterized with property Enable system call„“ !
Multiple callbacks may be assigned to one event. A callback (event, -class, -source, function index) however can get registered only once; if one tries to assign an identical callback, ⇘ CB.RegisterCallback will dump an error.
After a reset of the PLC all registered callbacks will be deregistered immediately. Callbacks, which are possible registered on event AFTER_RESET (see ⇘ CB.Event), will be called once before the automatic deregistration.
Regard that callback functions can be called immediately after the event has occurred. Thereby an currently running IEC task can be interrupted. The programmer must be aware that this might raise similar problems like there are in multitasking systems (data consistency etc.); besides that the run time of a callback function should be kept as short as possible, because it blocks the whole system.
The return value of the callback function is of no significance[1].
[1] Explanation: If the return value was of importance, then in case of multiple callback functions which are registered on the same event, it would not be clear which return value has priority.