Objectives
To provide examples and explanations for creating Function Blocks when In Out variables have non-fixed array sizes.
Introduction
In most cases, an array of data used in a Function Block is a fixed size such as 10 words or 10 bits. But special application circumstances may require that an In Out variable that is passed through a Function Block has an unknown or varying size. This can be difficult to accommodate but there are four possible solutions that will be detailed in this article:
1. Create unique Function Blocks to account for all required (known) array sizes.
2. Maximize the array size to account for the largest required array.
3. Create and use a temporary array as an In Out of the FB.
4. Create a structure to hold the array size and the data.
For information regarding passing data to a Function Block, see the article Passing an Array of Data to a Function Block.
Content
There are advantages and disadvantages to the four possible solutions described in the introduction.
1. Creating unique Function Blocks to account for all known array sizes can be efficient and easy if array sizes are known and limited. This may be difficult and FB memory intensive if there are many different sizes to accommodate.
2. Maximizing the array size can be a solution utilizing only one Function Block with minimal programming. This can use unnecessary memory when many smaller array sizes are present.
3. Creating a temporary array is flexible and efficient, but requires extra programming.
4. Creating a structure including the array size requires that the array size is known or attainable.
Carefully evaluate the application requirements before choosing a method.
Creating Unique Function Blocks
The attached example (Fixed 3.cxp) provides a solution for a scenario when 3 different array sizes are required of sizes 3, 5 and 10. The following details are used in the example:
- Calculating the average value for an array of data of known size
- Array sizes are fixed at 3,5 and 10
These individual Function Blocks are designed to access a fixed number of values from an array of data passed as an In Out variable.
Maximizing the Array Size
The attached example (Maximized.cxp) provides a solution for a scenario when the array size is unknown but will not exceed a fixed maximum size. The following details are used in the example:
- Calculating the average value for an array of data of unknown size, not to exceed a maximum size of 99.
This Function Block is designed to access a variable number of values from an array of data passed as an In Out variable, up to 99 in size.
Creating Temporary Arrays
The attached example (Temporary_Array.cxp) provides a solution for a scenario when the array size is unknown and memory usage needs to be efficient due to a large number of arrays and varied sizes. The following details are used in the example:
- Calculating the average value for an array of data of unknown size using one temporary array of large size 199.
This Function Block is designed to access a variable number of values from a large, temporary array of data. This temporary array of data can be reused for different sizes of data that are moved to this location prior.
Creating a Structure
The attached example (Size_In_Structure.cxp) provides a solution when the array size is known with several different array sizes. This method also keeps the size and the data together in the same structure for convenience. The following details are used in the example:
- Calculating the average value for an array of data of known size using a structure.
This function block is designed to access a variable number of values from an array of data passed as an In Out variable, up to 99 in size.
Summary
Evaluate the program and application requirements before choosing a method to account for arrays of varying size used with Function Blocks. The number of varying array sizes, number of arrays, maximum array size and number of Function Block instances should be known before one of the four detailed methods is employed.