This article discusses how to build a universal array resetter block using the CountOfElements instruction on an S7-1500 PLC in TIA Portal. Resetting an array containing production data, buffers, or recipes to zero is a very frequent task. The common approach is to write a FOR loop with array bounds typed manually for each array. This approach is error-prone because if the array size is changed, loop bounds that are not updated will leave some elements unreset or make the program access outside the bounds. With CountOfElements, the number of array elements is read while the program is running, so one block can reset an array of any size and lower bound.
This system uses a Function (FC), namely FC_ArrayReset, that receives an array through a Variant parameter. This block checks that the parameter really is an array, reads its number of elements, then overwrites every element with zero according to its type (Int or Real). The block is used in three cases: an Int array with lower bound 1, a Real array, and an ordinary variable that must be rejected. Testing is done with PLCSIM Advanced V5.0 on a virtual S7-1500 CPU.
Prerequisites
- The reader already understands how to create an array in a Data Block.
- The reader already understands how to create an FC and call it from OB1 using the SCL language.
- The reader already knows how to run a virtual CPU with PLCSIM Advanced and create a Watch table.
CountOfElements Concept
CountOfElements receives a Variant that points to an array, then returns the number of elements of that array as UDInt. This value is read while the program is running, so the block does not need to know the array size when it is created. This instruction is usually used together with two companion instructions: IS_ARRAY to check whether the Variant points to an array, and TypeOfElements to find out the element type of that array. To write to an element, MOVE_BLK_VARIANT is used, which copies elements from the source to a certain position in the destination.
There are two things to understand. First, the index in MOVE_BLK_VARIANT is counted from the first element of the array starting at 0, regardless of the declared lower bound of the array. An array [1..5] is still accessed with indexes 0 to 4. Second, MOVE_BLK_VARIANT requires the source and destination types to be exactly the same, so for every supported element type a zero-valued source of the same type is needed. In this article, the zero source is provided in a dedicated Data Block that is never written to.
S7-1500 PLC – Universal Array Resetter
Step 1: Creating the Data Blocks
- Create a Global Data Block DB_Zero (optimized) containing two one-element arrays: ZInt of type Array[0..0] of Int and ZReal of type Array[0..0] of Real. Leave their values at 0 and never write to them from the program.
- Create a Global Data Block DB_Test (optimized) containing the following variables.
- Array: Arr_Int of type Array[1..5] of Int and Arr_Real of type Array[0..9] of Real.
- Not an array: Single_Real of type Real.
- Control: Exec1 to Exec3 (Bool), Count1 to Count3 (UDInt), and Status1 to Status3 (Int).

Step 2: Creating FC_ArrayReset
- In the Project Tree, open PLC_1 > Program blocks > Add new block, select Function (FC), and name it FC_ArrayReset, language SCL.
- Fill in the block interface as follows.
- InOut: Target of type Variant.
- Output: Count (UDInt) and Status (Int).
- Temp: Last and i (DInt), ret (Int).
- Write the following SCL program:
#Status := 0;
#Count := 0;
// Reject anything that is not an array
IF NOT IS_ARRAY(#Target) THEN
#Status := 1; // not an array
RETURN;
END_IF;
// Number of elements, read at runtime (any array size or lower bound)
#Count := CountOfElements(#Target);
#Last := UDINT_TO_DINT(#Count) - 1;
// Overwrite every element with zero, according to the element type
IF TypeOfElements(#Target) = Int THEN
FOR #i := 0 TO #Last DO
#ret := MOVE_BLK_VARIANT(SRC := "DB_Zero".ZInt, COUNT := 1,
SRC_INDEX := 0, DEST_INDEX := #i,
DEST => #Target);
END_FOR;
ELSIF TypeOfElements(#Target) = Real THEN
FOR #i := 0 TO #Last DO
#ret := MOVE_BLK_VARIANT(SRC := "DB_Zero".ZReal, COUNT := 1,
SRC_INDEX := 0, DEST_INDEX := #i,
DEST => #Target);
END_FOR;
ELSE
#Status := 2; // element type not supported
END_IF;
How does FC_ArrayReset work?
The block starts by clearing Status and Count. IS_ARRAY(#Target) checks whether the paired variable is really an array. If not, Status is set to 1, and the block stops with RETURN, so an ordinary variable is left untouched. If it is an array, CountOfElements takes its number of elements, and Last is calculated as the number of elements minus one as the end bound of the loop.
Next, TypeOfElements determines the element type of the array. For the Int or Real type, the FOR loop runs from index 0 to Last. On each iteration, MOVE_BLK_VARIANT copies one zero element from DB_Zero to position #i in the destination array.
The return value ret is stored in a variable because in SCL the return value of this instruction must be stored. Element types other than Int and Real go to the ELSE branch and produce Status 2. Because the loop bound comes from CountOfElements, changing the array size does not require any change to the block.

Step 3: Calling FC_ArrayReset in OB1
- In OB1 (SCL), write the following program, then compile PLC_1 and make sure there are no errors.
// Each call resets its array while the matching Exec bit is TRUE
IF "DB_Test".Exec1 THEN
"FC_ArrayReset"(Target := "DB_Test".Arr_Int,
Count => "DB_Test".Count1, Status => "DB_Test".Status1);
END_IF;
IF "DB_Test".Exec2 THEN
"FC_ArrayReset"(Target := "DB_Test".Arr_Real,
Count => "DB_Test".Count2, Status => "DB_Test".Status2);
END_IF;
// Not an array: the block must reject it
IF "DB_Test".Exec3 THEN
"FC_ArrayReset"(Target := "DB_Test".Single_Real,
Count => "DB_Test".Count3, Status => "DB_Test".Status3);
END_IF;
How do the calls in OB1 work?
Each call is wrapped in an IF with its own Exec bit, so the FC only runs while that bit is TRUE. The Target parameter is filled with a different array in each call: a five-element Int array with lower bound 1, and a ten-element Real array. The same FC is used for both without any change.
The third call deliberately passes the variable Single_Real, which is not an array, to prove that the block rejects it and its value does not change. The element count result and status code are written to Count and Status in DB_Test so they can be monitored.

Step 4: Running the Virtual CPU
- Open PLCSIM Advanced, select PLCSIM Virtual Eth. Adapter, create an S7-1500 instance with an IP matching the CPU in the project, then Start. Make sure Support simulation during block compilation is checked in the project properties.
- Download to the virtual CPU, run the CPU, then Go online.
Final Test Result
Step 1: Preparing the Watch Table
- Create a Watch table containing all DB_Test variables, then click Monitor all.
Step 2: Testing an Int Array with Lower Bound 1
- Fill Arr_Int[1] to [5] with 1, 2, 3, 4, 5 via Modify now.
- Change Exec1 to TRUE. Expected result: all of Arr_Int becomes 0, Count1 = 5, and Status1 = 0. This proves that the lower bound of the array does not affect the result.
- Change Exec1 back to FALSE.

Step 3: Testing a Real Array of a Different Size
- Fill Arr_Real[0] = 1.5 and Arr_Real[9] = 9.5.
- Change Exec2 to TRUE. Expected result: all of Arr_Real becomes 0.0, Count2 = 10, and Status2 = 0.
- Change Exec2 back to FALSE.

Step 4: Testing the Rejection of an Ordinary Variable
- Fill Single_Real with 5.0, then change Exec3 to TRUE.
- Expected result: Status3 = 1, Count3 = 0, and Single_Real stays 5.0.
- Change Exec3 back to FALSE.

Usage Tips
For a very large array, a loop of one element per iteration takes a lot of scan time. In that case, run the reset only when needed or spread the process over several cycles, for example by copying larger chunks from a zero array. To add a supported type, add a one-element array in DB_Zero and one new ELSIF branch in the FC. If you want to reset to a value other than zero, replace the contents of the source in DB_Zero with the desired value or provide a separate source DB. Always check Status before using an array that has just been reset, and make sure DB_Zero is never written from any part of the program.