Cause 1: Division by zero.
Even if a project does not have an explicit division by zero, it is possible that a denominator variable is becoming zero at some point during run time. This will cause the PAC to lock up, disconnect and signal an error (note that embedded Xpress and the PAC Configuration Tool should still work). Cycling power is required. In cases where the division happens early in program execution, there may not be enough time to connect to the PAC and halt program execution before the error occurs again. In this case, the Flashback Utility can return the PAC's SD card to factory defaults and ready it for a new download.
To avoid division by zero entirely, the following code can be written into a function to account for cases where the denominator is zero. The declaration and implementation are both given below.
FUNCTION SDIV : LREAL //Divides NUM by DEN and returns the result.
//If DEN is equal to 0, the function returns ERR.
//Return value is of type LREAL.
NUM : LREAL;
DEN : LREAL;
ERR : LREAL := 0;
//If the denominator is zero, return zero. Otherwise, perform division.
IF DEN = 0 THEN
SDIV := 0;
SDIV := NUM / DEN;
Cause 2: File Handling Issues
Keeping files open for too long can lead to memory corruption in the RAM and possible file corruption on the writable media. Because of this, it is advisable to keep files open for the minimum amount of time necessary to perform operations on them.
In addition, it is important to use the error-checking features of the file access functions in order to determine whether files opened and closed properly. In circumstances where a file does not exist or cannot be opened, there should be escape routines built into the logic so that file access can be consistently aborted.
File access functions should take place in their own dedicated low-priority task. Otherwise, they can inhibit the functioning of a higher-priority task that may be time critical. Accessing files generally takes more processor time than the PAC's operations, which are mostly math-related.
It is important to remember that functions accessing recipes should be used the same way as any other file access function. The function should be in a low-priority task.