JZGKCHINAIndustrial Control Technology Sharing Platform
Introduction
In automation control systems, PLCs (Programmable Logic Controllers) play a crucial role, especially in data processing and control within industrial processes. As a PLC engineer, mastering array operations and data processing techniques is key to improving program efficiency and optimizing system performance. Today, we will explore a common yet very practical PLC program: Array Reversal Function.
In PLC programming, array reversal is a common requirement. Its applications are extensive, such as in data sorting, output control, and real-time monitoring, where the reversal function is often used to change the order of data flow to achieve the desired effect. This article will demonstrate how to implement array reversal output through a specific program example and analyze its potential in actual industrial applications.
1
Program Code and Introduction
The following is the core code of the PLC program (the code is based on the SCL language of the TIA platform):
FUNCTION_BLOCK "ArrayReverseOrder"{ S7_Optimized_Access := 'TRUE' }VERSION : 0.1 VAR_INPUT InputArray : Variant; END_VAR VAR_OUTPUT OutputArray : Array[1..5] of Int; END_VAR VAR statArray : Array[1..5] of Int; array_en : UDInt; j : DInt; k : DInt; END_VARBEGIN (* Output an array in reverse order. *) #array_en := CountOfElements(#InputArray); VariantGet(SRC:=#InputArray, DST=>#statArray); #k := UDINT_TO_DINT(#array_en); FOR #j := 1 TO #k DO #OutputArray[#j] := #statArray[#k - #j + 1]; END_FOR;END_FUNCTION_BLOCK
2
Code Analysis
Input and Output Variables:
-
InputArray: Input array, usingVariant type, meaning it can accept arrays of different types.
-
OutputArray: Output array, an integer array of size 5.
Local Variables:
-
statArray: Used to store the array converted fromInputArray.
-
array_en: Used to store the number of elements in the input array.
-
j and k: Control loop variables for the array reversal output.
Main Logic:
-
VariantGet function transfers the inputInputArray tostatArray.
-
#array_en := CountOfElements(#InputArray) is used to calculate the number of elements inInputArray.
-
Then, through theFOR loop, the reverse output is achieved by assigning the elements fromstatArray in reverse order toOutputArray.
3
Application Scenarios
The application scenarios for this program can be widely used in various automation systems, especially in the following fields:
-
Data Sorting: When the input data order does not meet requirements, it can be adjusted by reversing the output order.
-
Motion Control: In automated devices like robotic arms, it is sometimes necessary to execute actions in a specific order, and reverse output control signals are a common requirement.
-
Real-time Monitoring Systems: In certain real-time monitoring systems, it may be necessary to process measurement data in reverse, and reverse output can help better display the data.
For example, in data acquisition systems on production lines, data collected by sensors may need to be analyzed and processed in reverse order, or during historical data playback, reverse display of data can help engineers better understand equipment operating status.
4
Extended Thoughts
Dynamic Array Processing: The design of this program is relatively simple, assuming the input array length is fixed at 5 elements. In practical applications, we may need to handle arrays of uncertain lengths, which requires more flexible processing methods. Dynamic memory allocation or more complex data structures can be used to optimize the program.
Reusability of Function Modules: By definingFUNCTION_BLOCK, we have created a reusable module. In different projects, if similar needs arise, we can simply call this functional module, avoiding redundant coding and greatly improving development efficiency.
Multi-dimensional Array Reversal: The current program only handles one-dimensional arrays. If the application scenario involves multi-dimensional arrays, how to perform reversal operations, even for two-dimensional or three-dimensional arrays, may require extending this functional module and considering using recursion or other efficient algorithms for implementation.
Conclusion
In this article, we introduced how to implement the array reversal function in PLCs and explained it through specific program code. Array reversal not only has wide application scenarios in control systems but also, through flexible programming ideas, can solve many data processing and sorting problems. As automation systems become increasingly complex, mastering this fundamental yet practical skill will provide PLC engineers with more opportunities for development and optimization.
PLC programming is not only about hardware control but also about the art of data flow. Continuously expanding our thinking about data processing can enable us to handle more complex control tasks with ease.

Feel free to ask questions, and I will answer them

We welcome everyone to propose some standard function block requirements in the comments section. If suitable, we will share them in future articles.

Previous Issues
Weekly Programming Example: Comparison of Two Implementations for Bit to Word Conversion
Weekly Programming Example: Recording Device Operating Time
Weekly Programming Example: Start Control System Based on Long Press Detection
Weekly Programming Example: Exploring PLC Data Logging Function Block
Weekly Programming Example: Heartbeat Detection and PLC Applications
Weekly Programming Example: Ramp Generator
Weekly Programming Example: Rotational Speed Calculation
Weekly Programming Example: Calculating PLC Cycle Period | Routine Requirement Collection!
Weekly Programming Example: How to Elegantly Split Strings
Weekly Programming Example: Valve Control Program with Feedback
Weekly Programming Example: Pulse Relay Function Block in Siemens LGF Library
Weekly Programming Example: Control Application for Curve Acceleration and Deceleration
Weekly Programming Example: Easily Remove Leading Characters from Strings
Weekly Programming Example: Update Input Based on Threshold
Weekly Programming Example: Official Analog Input Processing Module
Weekly Programming Example: Timing Control of Master-Slave Motors
Weekly Programming Example: Optimization Scheme Based on First-Order Lag Filtering
Weekly Programming Example: The Amazing Conversion from Hexadecimal to Floating Point
Weekly Programming Example: PLC Long Press Control Program
Weekly Programming Example: PLC Program for Splitting Multi-digit Integers
Weekly Programming Example: Flow Accumulation Calculator
Weekly Programming Example: Standard Control Module for Motors Used in Chains
Weekly Programming Example: Implementation and Application of PID Control Algorithm in PLC Systems
Weekly Programming Example: Custom Seed Generation for Random Numbers
Weekly Programming Example: Adjustable Time Pulse Generator
Weekly Programming Example: Standard Analog Input Function Block with Filtering
Weekly Programming Example: Using PLC to Convert Strings to Date Format
Weekly Programming Example: Comparing Two DB Blocks (STL)

November 2025
1. Smart200 & V90 Servo System: 15 lessons updated
2. Beckhoff Live Courses: 30 lessons updated
3. PKS Quick Start: Beginner & Intermediate – All
4. Classic Public Course on Industrial Control: 35 lessons updated5. EPLAN Quick Start: All 20 beginner lessons