PLC Basics - Complex Data Types: Arrays and Strings

 



In the previous article, we introduced the basic data types of PLC, explaining the meanings of "bit," "byte," "double word," "integer," and "real." In today's article, we will delve into advanced topics within data types, specifically discussing the concepts of arrays and strings.

What is an "array"? An array is a collection of a specific number of elements with the same data type. From the definition, we can observe two key characteristics of arrays: 1) the number of elements is predetermined, and 2) the elements have the same data type.

The statement "the number of elements is predetermined" implies that when declaring an array, it is necessary to specify the upper limit (maximum value of element indices) and lower limit (minimum value of element indices).

The statement "the elements have the same data type" means that when declaring an array, the basic data type must be specified, and this type applies to each element in the array.

Next, let's explore how to declare arrays in the TIA Portal environment. First, create a new DB block, and then create a variable within it, such as "sensor." Click on the right side where it says "Data type," and you will see the array declaration format: Array[lo..hi] of type, as shown in the following figure:


In this context, "lo" represents the lower limit of the array, "hi" represents the upper limit of the array, and "type" indicates the basic data type of the array elements.

After selecting the array type declaration, click on the downward arrow on the right. A small window will pop up, as shown in the following figure:




In the "Data type" section, you can choose the basic data type of the elements. Let's assume we choose "Boolean (Bool)." In the "Array limits" section, you can select the upper and lower limits of the array. Additionally, you can increase the array dimensions by inputting a comma "," in English.

What are array dimensions? Typically, the arrays we use have one dimension, referred to as a "one-dimensional array." In a one-dimensional array, data is arranged in "rows" and "columns," where the number of rows is greater than 1, and the number of columns is equal to 1. For a two-dimensional array, data is arranged in rows and columns, where both the number of rows and columns are greater than 1. A three-dimensional array's data arrangement can be understood as three-dimensional space, involving "rows," "columns," and a vertical dimension. Arrays can have four or more dimensions, but it's essential to note that increasing the array dimensions leads to an exponential growth in the number of elements, occupying significant memory. Typically, one-dimensional and two-dimensional arrays are the most commonly used.

The declaration of a Boolean two-dimensional array is shown in the following figure:


This array has two rows and three columns, totaling 2x3=6 elements.

Now, let's discuss strings. A string is a collection of characters (Char) used to store data such as "name" or "VIN code," and it has a specific length. In the Siemens S7 series PLC, the definition of the string structure is as follows:





The first byte of the string represents the maximum number of characters the string can hold, and the second byte indicates the actual number of characters in the string. Starting from the third byte, the subsequent bytes contain the character data of the string, with a maximum of 254 characters.

Suppose we want to declare a string type for a VIN code with a length of 20. In the "Data type" section on the right, you can select "String" and then write "[20]" after it, as shown in the following figure:



Related Reference Articles:

PLC Basics: Data Types

A detailed explanation of the POINTER data type in STEP7.