Arrays In Siemens TIA Portal In A Datablock
Arrays In Siemens TIA Portal In A Datablock

Arrays are an important data type in PLC programming. It’s a method of grouping variables of the same data type in a long list, and accessing each internal element via an index.

For example, if I wanted to access Element 17 from the Array_1 in the above image, I would use Array_1[17].

Arrays are one of the first building blocks available in a structured approach, and offer means of indirect addressing (where another variable is used as the Index to make selection of elements dynamic)

Declaration Of Arrays

Just like any other variable, Arrays need to be declared before they can be used. Unlike any other variable though, Arrays are special in that they take parameters to change how the data type is formed:

  • Lower Boundary – The lowest value the index can go (for the love of all things good, make this 0)
  • Upper Boundary – The highest value the index can go
  • Data type – The data type of the elements in the Array

These parameters come together to form the following declaration in the Data type column:

Array [x..y] of z

Where:

  • x = Lower Boundary
  • y = Upper Boundary
  • z = Data Type

A declaration of an array of Byte values would look something like:

Array [0..3] of Byte

IMPORTANT
IMPORTANT
IMPORTANT

The Element Count (sometimes incorrectly referred to as length) of your array is determined by the Upper Boundary minus the Lower Boundary

For example, an Array [0..7] of Bool has an Element Count of 8, as there are 8 elements in the array. However, the memory length is 1, as 8 Bool takes up the space of 1 Byte

Special Declarations

You can do more with Arrays in the declaration area than is immediately obvious too.

Array Of Struct / UDT

For example, you can create an Array Of Struct or UDT:

Example of an Array of Struct

The above image shows Structure_Array declared as Array[0..3] of Struct and the internals of the Struct are defined in the first element of the array (Note that they are not greyed out)

The variables that are placed in element 0 are then replicated across the remainder of the Array bounds, including the comments, although they can be overridden and made specific to each element if you wish.

Arrays With Dynamic Length

It is not possible with Array’s in PLCs to have truly dynamic arrays, at some point the length of the Array must be a fixed length in order for memory to be allocated correctly.

But, Siemens do have a trick up their sleeve for this:

Dynamic Array Declaration

In Functions and Function Blocks you can declare an array of no fixed length, but, it can only be used at the InOut interface and the block and data must be Optimized

In order to use the interface, you need to actually pass an array value that matches the same Data type, for example Array[0..3] of Byte can be passed to Array[*] of Byte, but cannot be passed to Array[*] of Bool

Array Parser Function Block accepting two different sizes of Array

The above shows two different sizes of Array:

  • "Array_Block".Array_3Byte is declared as Array[0..3] of Byte
  • "Array_Block".Array_30Byte is declared as Array[0..30] of Byte

Both Arrays are being passed to Array[*] of Byte and the internal code is using the UPPER_BOUND instruction to output the Upper Bound of the relative array:

UPPER_BOUND instruction working with dynamic array

Arrays With Constant Boundaries

You can also create an Array using constants for the lower and upper boundaries

Example of Array[<Constant>..<Constant>] of <Data Type> using Global Constants

This allows for arrays to be resized by changing a global or local constant.

The above image shows Array_3Byte now being declared using Array_Min and Array_Max, which can be set to any value and will change the length of the array.

Paired with the previous function of [*], no code needs to change, but remember that Optimized data needs to be used

This approach is great if you have an array that changes length because of differences in your project, such as the number of instruments, the element count can be changed easily by changing the constant value.

Note
Note
Note

It’s important to remember that if you are using global constants in a Function or Function Block, this will break library conformance.

This means that if a global constant is used, TIA will warn you that its not good practice if its a library block (because you can’t ensure that the constant is available in every project)

In this case, it’s a bit of a pain because you have to either be non-conformant or use local constants, which means you won’t be able to change them without creating a new version… A draw back of library usage!

Something to watch out for when architecting new designs.

Indexing Elements

Indexing An Array

Indexing elements is achieved by using [x], where x is an int. The int can either be constant or can be a variable.

Note
Note
Note

TIA portal will warn of an index that is out of array bounds, but only if constants are bing used as the index.

101 is larger than the Upper Boundary

This is because the upper and lower boundaries are known, and so is the index.

If a Variable index is used, the compiler has no way of knowing what the index value will be.

It is up to you to ensure that you do not allow an index to go out of array bounds. If it does, your PLC will stop (or at least the block executing will stop if the programming error OB is in use)

SCL And Arrays – A Perfect Match

SCL looping through an Array

SCL (Structured Control Language, or Structured Text) is the perfect companion language to Arrays.

Using the FOR instruction, the iteration variable (i in the above example), can be used to provide the index for the array.

This means that on every pass of the FOR loop, a new element in the array is indexed.

This is a great method for checking multiple elements in passes of a loop, or executing logic against each element on each pass.


Check Out Another Post

Leave a Reply