AcquirePLC Programming Best Practices: FOR..NEXT Loops | Standard Electric Supply Co.

PLC Programming Best Practices: FOR..NEXT Loops

ByRF Jordan

10/9/2018
General Description: The FOR…NEXT instruction is one of three iterative loop instructions available to the Unity Pro programmer. Iterative instructions are powerful tools unique to the Structured Text Editor. The FOR…NEXT instruction differs from the other two instruction, in that the number of iterations is known prior to entering the loop. Control of the number of iterations is managed by the index variable specified in the opening statement. It is of an integer data-type. The programmer specifies a starting value that the indexing variable is set to when first entering to loop. The instruction will increment the index variable until it is equal to (or greater than) the programmer specified end value.

General Precautions: The biggest danger in using iterative loops is creating a scenario where the indexing variable never achieves the end value. If a loop never terminates, the watchdog timer of the PLC will timeout and stop the PLC from processing logic. The other two iterative loops can use an EXIT instruction inside the loop to handle special cases where it becomes necessary to prematurely terminate loop execution. This feature is not supported in the FOR…NEXT instruction. Special care must be taken, so that the programmer does not create a never-ending loop. This can occur if logic inside the loop modifies the index variable. This can also occur if the end value is likewise modified. Any manipulation of the index variable, the start value (which can also be a variable), or the end value (which can also be a variable), inside the loop would be considered a poor programming practice.

The second danger in using the iterative loops, is overloading the loop. This can take several forms. In one form, the program performs elaborate manipulations on multi-element arrays of structured data-types. While this is the real power the instruction offers, it can be easily overused. Multi-dimensional arrays (more than four dimensions), large data-structures (more than four layers of abstraction), and indices greater than 100 elements, require special attention as these typically indicate a potential overload. The processing power of the PLC will determine the risk of exposure to this type of issue.

The least obvious type of danger in using iterative loops, is the potential to saturate the processing power of the PLC or its communication networks by initiating (or re-initiating) an advanced function call inside the loop. Any type of communication function block or mechanism that initiates such a function, will queue as many communication requests as there are iterations of the loop. As this happens in one PLC program scan, it is easily possible to saturate and lock up the communication port on the PLC. Using advanced functions, or communication functions, inside of an iterative loop is considered a poor programming practice. It is one of those programming traps that leads to intermittent operation of the system. As a side note, this danger is not always obvious. If a FOR…NEXT loop is used in an HMI or SCADA system, and the loop is reading a different variable from the PLC for each change in the index variable, the HMI or SCADA will actually be queueing successive communication requests. In most cases, these requests will likely be for only one element. This will, again, saturate the communications and lock up the system. The programmer should only use variables with a local scope, inside of an iterative loop. A local variable is a variable that can be accessed without going outside the confines of the memory structure where the code resides.

For any questions on these best practices, contact RF Jordan at [email protected] or 414-231-2539.

Sharing this post