Email us at: sales@trutegra.com |  Call Us at: (704) 529-0123

Automation Partners

Call us today! 24 Hour Service: (704) 529-0123

Blog

By An Nguyen

A PLC programmer may want to create an Add-on Instruction as part of a project.  This can make code re-usable, more compact or just more readable.  Passing an array into an AOI is possible, but when the array to be passed is of unknown length, some tricks or additional understanding is required.  This article explains how this can be easily done.

The programmer wants to create a simple AOI that finds the average of all elements in an array.  Finding an average is a very simple case, but it illustrates the point, which is that arrays of different lengths can be passed into an AOI. This technique promotes standardization and more re-usable code.

Figure 1: Rockwell gives us the ability to do this with AOI

Reading between the lines, we discover the key insight, and the trick which allows our AOI to accept any size array we want to it.  This allows us to avoid writing many versions of an AOI for various sized arrays.

First, setup AOI parameter based on Figure 1.

Notice that:  The data type can be bool, real or even your own UDT as long as it as an array length of 1 and the usage parameter is set to “InOut”.  

Expert tip: When usage is set to “InOut”, the array is passed by reference, not by value.  Parameters defined as “Input” are passed by value.

Then within the AOI code, we must determine the size of the array before we do any processing on it.

The example is given in structured text:

//Determine the size of the passed in array

size(Array_In,0,array_size);

//Initialize a sum variable…

sum := 0;

//Remember index of array range from 0 to size of array – 1 because 0 is inclusive

for index := 0 to array_size – 1 do

sum := sum + Array_In[index];

end_for;

Average := sum/array_size;

Now you can use the same AOI to take the average of an array of any length

An example using the AOI in a ladder diagram is as follows:

Notes:

Edits inside an AOI require going offline, editing, then downloading.  By building the AOI flexibly using this technique (passing variable length arrays) it may be that offline editing in the future can be reduced.

Also, the example above uses the same tag name (Test_Avg) for both instances of the AVG AOI.  This is not advised.

Key Points, Restated:
To pass an array of undetermined size (number of elements) into an AOI, set the parameter to “InOut” and in the AOI definition, declare it as having array size of 1.  There are side benefits to this, including flexibility, and standardization of code.

Happy Programming!

Contact Us

901 Blairhill Road Suite 500, Charlotte, NC
Phone: 704.529.0123 | Fax: 704.529.0139 | sales@trutegra.com