Siemens PLC Three-Dimensional Array Programming Example

A simple example of three-dimensional array operations used to store and process temperature data from a factory.

Assuming we need to monitor three areas (Area) in a factory, each with 4 production lines (Line), and each production line records 5 temperature values (Measurement) daily. We will use a three-dimensional array to store this data and write a program to read and process it.

1. Define the Three-Dimensional Array

In TIA Portal, create a data block (Data Block) to define the three-dimensional array.

// Data block definition
DATA_BLOCK "TemperatureData"
    STRUCT
        TempArray : ARRAY[1..3, 1..4, 1..5] OF REAL; // Three-dimensional array: 3 areas × 4 production lines × 5 measurements
    END_STRUCT
BEGIN
END_DATA_BLOCK

2. Example Program

The following is an SCL program that demonstrates how to initialize, write to, and read data from the three-dimensional array, as well as calculate the average temperature of a specific area.

FUNCTION_BLOCK "ProcessTemperature"
    VAR_INPUT
        AreaIndex : INT; // Area index (1..3)
        LineIndex : INT; // Production line index (1..4)
        MeasurementIndex : INT; // Measurement point index (1..5)
        NewValue : REAL; // Temperature value to write
    END_VAR
    VAR_OUTPUT
        AreaAverage : REAL; // Average temperature of a specific area
    END_VAR
    VAR_TEMP
        Sum : REAL; // Temporary variable for sum calculation
        i, j : INT; // Loop variables
    END_VAR
    VAR
        Data : "TemperatureData"; // Reference to data block
    END_VAR

BEGIN
    // Write data to the specified location
    IF AreaIndex >= 1 AND AreaIndex <= 3 AND 
       LineIndex >= 1 AND LineIndex <= 4 AND 
       MeasurementIndex >= 1 AND MeasurementIndex <= 5 THEN
        Data.TempArray[AreaIndex, LineIndex, MeasurementIndex] := NewValue;
    END_IF;

    // Calculate the average temperature for the specified area
    Sum := 0.0;
    FOR i := 1 TO 4 DO // Iterate over production lines
        FOR j := 1 TO 5 DO // Iterate over measurement points
            Sum := Sum + Data.TempArray[AreaIndex, i, j];
        END_FOR;
    END_FOR;
    AreaAverage := Sum / 20.0; // 4 production lines × 5 measurements = 20 data points
END_FUNCTION_BLOCK

3. Program Explanation

  • Three-Dimensional Array Definition: TempArray is a 3×4×5 three-dimensional array that stores temperature values of type REAL.

  • Writing Data: Using input parameters AreaIndex, LineIndex, and MeasurementIndex, the NewValue is written to the specified location in the three-dimensional array. The program checks the validity of the indices to prevent out-of-bounds errors.

  • Calculating Average: The program iterates through all production lines and measurement points of the specified area (AreaIndex), calculates the sum, and computes the average, outputting it to AreaAverage.

  • Data Block Reference: Access the three-dimensional array through the "TemperatureData" data block.

4. Calling Example

Assuming this function block is called in the main program:

FUNCTION "Main" : Void
    VAR
        Process : "ProcessTemperature"; // Instantiate function block
        AvgTemp : REAL; // Store average temperature
    END_VAR
BEGIN
    // Write data: Area 1, Production Line 2, Measurement Point 3, Temperature 25.5
    Process(AreaIndex := 1, LineIndex := 2, MeasurementIndex := 3, NewValue := 25.5, AreaAverage => AvgTemp);

    // Output the average temperature of Area 1
    // AvgTemp can be viewed in HMI or debug window
END_FUNCTION

Leave a Comment