Division checks (POUs CheckDivDInt, CheckDivLInt, CheckDivReal, CheckDivLReal)
Functions for avoiding the divisor value "0": CheckDivDInt, CheckDivLInt, CheckDivReal and CheckDivLReal
The functions CheckDivDInt, CheckDivLInt, CheckDivReal and CheckDivLReal can be used to avoid division by 0. If you integrate these functions in the PLC project, they are called before each division that occurs in the code.
![]() | Do not change the declaration part To maintain the functionality of the monitoring functions, the declaration part must not be modified. The only exception is to add local variables. |
Standard implementation of the function CheckDivReal
Declaration part:
// Implicitly generated code : DO NOT EDIT
FUNCTION CheckDivReal : REAL
VAR_INPUT
divisor : REAL;
END_VAR
Implementation part:
// Implicitly generated code : Only an Implementation suggestion
{noflow}
IF divisor = 0 THEN
CheckDivReal := 1;
ELSE
CheckDivReal := divisor;
END_IF
{flow}
The DIV operator uses the output from the function CheckDivReal as divisor. In the sample program below, division by 0 is avoided by changing the value of the divisor fDivisor implicitly initiated with "0" to "1" by the function CheckDivReal before executing the division. Thus the result of the division is 799.
PROGRAM MAIN
VAR
fResult : REAL;
fDivident : REAL := 799;
fDivisor : REAL := 0;
END_VAR
fResult := fDivident / fDivisor;
Sample: Generation of a core dump at runtime in CheckBounds()
The following is a sample in which the CreateCallstackCoreDump() function is called in the Bound Checks (POU CheckBounds) function for implicit checks.
This allows you to find out in which line of code a possible access outside the valid array bounds occurs without stopping the runtime using a breakpoint.
By calling CreateCallstackCoreDump() within the corresponding IF branches of CheckBounds(), the call stack with the position of the faulty array access is saved in a core dump during runtime and can be loaded at a suitable time for analysis purposes.
Function CheckBounds:
// Implicitly generated code : DO NOT EDIT
FUNCTION CheckBounds : DINT
VAR_INPUT
index, lower, upper: DINT;
END_VAR
// Implicitly generated code: Only an implementation suggestion
{noflow}
IF index < lower THEN
CreateCallstackCoreDump('CheckBounds_IdxTooLow');
CheckBounds := lower;
ELSIF index > upper THEN
CreateCallstackCoreDump('CheckBounds_ IdxTooHigh');
CheckBounds := upper;
ELSE
CheckBounds := index;
END_IF
For example, a core dump with the following name is created:
Port_851_CallStackDump_CheckBounds_IdxTooLow.2322085807.core
See also: