The previous post we looked at some SCL code that produced a PLC stopping error.

Previous Problem Post:

Below is an image that shows what has changed:

Next_Stop_Turbine has replaced i in this answer, solving the issue of the PLC failing and stopping.

The reason the PLC was stopping is to do with the value of i once lines 80 to 82 have been processed.

Here’s what’s actually happening in the PLC:

  1. The loop on lines 73 to 78 are iterating through i, from the Max_Array_Limit (which is 11), to 0.
  2. Once the loop reaches i=0, the loop stops
  3. Next_Turbine_Stop_Found has been set to True in the loop, and Next_Stop_Turbine has been set to the value of i. This only happens if a Turbine is found to be running
  4. Because Next_Turbine_Stop_Found is True, the Stop_Load_Sequence for the corresponding Array element at the Next_Stop_Turbine index is set to True.
  5. From here on in there are no errors

In the previous, faulty, code:

The Next_Stop_Turbine is not used, and i is used instead. At the end of line 78, where the loop finishes, at this point the value of i is actually -1 and not 0.

This is because loops are Inclusive of the limits, so 0 is a valid value to run the loop on. In order to check for the run criteria, the value of i must be outside of 0..11. Once the value of i reaches -1, the code then drops to line 80.

On line 81, the array is attempted to be indexed with the value of -1 in i, which causes a PLC exception as the array starts at 0.

The reason Stop_Reset_DN is highlighted is because it’s the next point of executable code since the exception, since END_IF is not an executable part of the code (you can’t set a breakpoint there)

Summary

In order to ensure that the index is always valid for the array, the Next_Stop_Turbine value must be used. This is because the Next_Stop_Turbine value is only set when valid criteria are satisfied, meaning the value of the index used to index the array cannot be outside limits.

It’s important to remember that loops will leave the iteration variable outside of it’s defined limits at the end. If the limits match that of the Array you are looping over, and then you try to use the iteration variable to index, you’ll be outside the bounds of the array and an exception will be called, faulting the PLC.

Leave a Reply