Comparing Large Arrays of Double Integer (DINT) Values in SIMATIC PCS 7

Comparing Large Arrays of Double Integer (DINT) Values in SIMATIC PCS 7

To compare a large number of Double Integer (DINT) values (e.g., more than 20 variables) against constants or setpoints in SIMATIC PCS 7, the most efficient approach is to implement a custom SCL function block utilizing an array loop ('FOR i := 1 TO 20 DO...'), or use standard library comparator blocks ('CMP_DI' or APL 'CompIn02') in CFC. While cascading individual comparator blocks in CFC is viable for smaller groups, a compact SCL block dramatically reduces CFC chart clutter, minimizes memory consumption, and executes in a fraction of the CPU scan time.

Table of Contents

Executive Summary

Industrial automation applications—such as recipe batch code validation, multi-lane barcode verification, part sorting, and state matrix matching—frequently require comparing dozens of 32-bit integer (DINT) values against configured target thresholds. Placing 20 to 50 separate comparison blocks on a CFC chart results in bloated, unreadable charts, high engineering maintenance overhead, and inefficient CPU execution order. This guide contrasts standard CFC comparator blocks (CMP_DI, CompIn02) with an optimized SCL multi-value comparison block.

Architecture and Fundamental Mechanics

Comparison Architecture: CFC Cascading vs SCL Array Looping:

  • Method A: Cascading Comparator Blocks in CFC (CMP_DI / CompIn02):
  • Standard Library Block CMP_DI (FB40) or APL Block CompIn02 (FB1818) compares two DINT values (IN1 and IN2).
  • Comparing 20 values requires 20 separate block instances, cluttering the CFC sheet and consuming run sequence scheduling slots.
  • Method B: Optimized SCL Array Block (CMP_DINT_ARRAY):
  • Encapsulates the 20 comparisons within a single compact Function Block (FB).
  • Declares an input array: Values : ARRAY[1..20] OF DINT; and target constants Targets : ARRAY[1..20] OF DINT;.
  • An internal FOR loop evaluates comparisons in nanoseconds during a single block invocation.
  • Provides composite outputs: AllEqual : BOOL;, AnyEqual : BOOL;, MatchMask : DWORD;.

Step-by-Step Implementation Procedure

Step-by-Step Implementation of an SCL Multi-DINT Comparator Block:

  1. Create SCL Source in SIMATIC Manager:
    - In the S7 Program container, open the Sources folder -> Insert New Object -> SCL Source File.
    - Name it CMP_DINT_20.
  2. Program the SCL Comparison Logic:
    ```scl
    FUNCTION_BLOCK FB2020
    TITLE = 'Compare_20_DINT_Values'
    { S7_m_c := 'false' }
    VAR_INPUT
    Values : ARRAY[1..20] OF DINT;
    Constants : ARRAY[1..20] OF DINT;
    END_VAR
    VAR_OUTPUT
    AllMatch : BOOL;
    AnyMatch : BOOL;
    MatchCount : INT;
    MatchBits : DWORD;
    END_VAR
    VAR_TEMP
    i : INT;
    cnt : INT;
    bits : DWORD;
    END_VAR

cnt := 0;
bits := 16#0;
FOR i := 1 TO 20 DO
IF Values[i] = Constants[i] THEN
cnt := cnt + 1;
bits := bits OR SHL(IN := DWORD#16#1, N := (i - 1));
END_IF;
END_FOR;
MatchCount := cnt;
MatchBits := bits;
AllMatch := (cnt = 20);
AnyMatch := (cnt > 0);
END_FUNCTION_BLOCK
`` 3. Compile SCL Source into Blocks Folder: - In the SCL editor, click **File -> Compile** (or pressCtrl+F9). - Verify compilation completes with zero errors, generatingFB2020in the Blocks container. 4. Insert into CFC Chart: - DragFB2020` into the cyclic interrupt chart (OB35).
- Connect process variables to array elements; connect outputs to downstream interlocking logic.

Verification

To verify multi-value comparison operation:
1. In CFC test mode, force Values[1] := 100 and Constants[1] := 100; verify MatchCount increments to 1 and AnyMatch asserts TRUE.
2. Force all 20 values to match their respective constants; verify MatchCount = 20 and AllMatch asserts TRUE.
3. Change one value; confirm AllMatch drops instantly to FALSE.

Limitations and Operational Precautions

Operational precautions:
- Array loops in S7-400 CPUs must be bounded; never use unbounded WHILE loops in SCL, which can trigger CPU watchdog timeouts (OB80).
- If using standard CMP_DI blocks instead of SCL, organize them inside a nested CFC chart (Chart-in-Chart) to maintain plant hierarchy readability.
- Ensure that DINT sign boundaries are observed (-2,147,483,648 to +2,147,483,647).

Engineer FAQ

What standard library block compares two DINT numbers?

The CMP_DI (FB40) block in the standard library or CompIn02 (FB1818) in the APL.

Why is an SCL block preferred over 20 separate CMP_DI blocks?

An SCL block reduces 20 blocks down to a single compact call, eliminating chart clutter and reducing memory overhead.

Can CFC connect arrays directly?

CFC supports connecting to individual array elements (e.g., Block.Values[1]).

Does an SCL comparison block consume an AS Process Object?

If S7_m_c := 'false', the block is treated as an elementary calculation block and consumes zero Process Objects.

What is the data range of a DINT in Siemens S7?

A 32-bit signed double integer ranging from -2,147,483,648 to +2,147,483,647.

Technical References

Related Articles

Related Tags

  • DINT Comparison
  • SCL Programming
  • CMP_DI
  • CompIn02
  • Array Looping
20 respuestas