Arrays and indexed data in Structured Text

The Core Idea

An array stores a group of values of the same type under one name. An index chooses one element, and the declared bounds define which indexes are valid.

What you will learn

  • Declare an array with bounds, and read and write one element with an index.

  • Choose bounds that match how the plant numbers things, so the index means something.

  • Check an index before you use it, and read the message when an index is out of range.

Why it matters

Measurements from several similar devices are easier to handle as a collection, provided a requested device number is mapped to a valid element.

The problem

A filler has six heads, numbered 1 to 6 on the machine. The program keeps the last fill weight of every head in one array, and the operator types a head number to see its weight.

SignalTypeMeaning
FILL_GARRAY of REALLast fill weight of each head, in g. Declared in the program.
HEAD_SELINTThe head number the operator types, 1–6.
SELECTED_GREALThe selected head's weight, in g.
SEL_VALIDBOOLTRUE while HEAD_SEL names a head.

An array is one name for a row of values of the same type. ARRAY[0..5] OF REAL declares six REALs with indexes 0 to 5, and FILL_G[2] is the one at index 2. The index can be any INT expression, such as a tag. The program declares the array, so it appears at the bottom of Tags with all six values. The simulator scans every 100 ms.

These are the six weights, by head number:

Head123456
Weight (g)498.0502.0505.0497.0510.0495.0

After you lock in your prediction, type 6 into Head.

See it go wrong

Press Reset to start and try head 1. It shows 502.0 g, which is head 2's weight.

Fix it

Change the program so every head from 1 to 6 shows its own weight, and a number that is not a head shows 0.0 g with VALID HEAD off while the program keeps running. Editing pauses the program, so press Run to try your version.

Passing the checks records Structured Text progress on arrays and indexed data.

What happened and why

Two different things went wrong, and both come from the bounds.

  • Head 1 showed head 2's weight. With ARRAY[0..5], the first weight sits at index 0. Head number 1 reads index 1, the second element. Every head was off by one.

  • Head 6 stopped the program. Index 6 is past the end of ARRAY[0..5]. Reading or writing outside an array has no meaning, so the simulator stops the scan and names the index and the bounds.

Declaring the array ARRAY[1..6] makes the index the head number, so FILL_G[HEAD_SEL] reads the right element with no arithmetic. Keeping ARRAY[0..5] and writing FILL_G[HEAD_SEL - 1] also works, but every reader has to remember the shift.

The check comes first because an index from outside the program, such as an operator's entry or another machine's message, can be anything. With IF HEAD_SEL >= 1 AND HEAD_SEL <= 6, a bad number gives a defined result and a flag, and the rest of the program keeps running.

Writing works the same way: FILL_G[3] := 501.5; changes head 3's element only.

Common mistakes

  • Numbering from 0 when the items are numbered from 1: every item reads its neighbour's value, and the last one is outside the array.

  • Fixing the bounds but not checking the index: head 0 or 7 still stops the program.

  • Checking with the wrong limit: HEAD_SEL < 6 rejects head 6, which exists. Use the array's own bounds, both of them.

  • Reading the element before the check: a check on a later line never runs, because the bad index has already stopped the program.

Platform notes

  • What happens on an out-of-range index depends on the platform: some controllers fault and stop, some set a status bit and skip the access, and some read or write memory outside the array. Check every index that comes from outside the program on every platform.

  • IEC 61131-3 (the international standard for programmable controller languages) also has multidimensional arrays and arrays of structures. This simulator supports one-dimensional arrays of BOOL, INT, REAL, and STRING.

Enable JavaScript to run the embedded simulator and complete the interactive exercises.