Cookies are important for this site to function properly, to guarantee your safety, and to provide you with the best experience. By clicking OK, you accept all cookies. For more information, please access our Privacy Policy.
Table of Contents
Tutorials / 
Using a Global Array Data Block in Siemens S7-1500 PLC
Intermediate

Using a Global Array Data Block in Siemens S7-1500 PLC

PLC Programming
TIA Portal
Siemens
S7-1500
Software Engineering

Introduction

Variable data utilized by the user program is stored in data blocks, which serve as a storage location for program data. Global data blocks contain data available for use by all other blocks.

The CPU type being used determines the maximum size of data blocks. Global data blocks can be structured according to your preferences. PLC data types (UDT) can also be used as a template for creating global data blocks.

Data can be read from or written to a global data block by any function, function block, or organization block. The data persists in the block even after exiting the data block. Simultaneous opening of instance and global data blocks is allowed.

Figure 1.1 - Siemens TIA Portal PLC Data Blocks | Global and instance data blocks
Figure 1.1 - Siemens TIA Portal PLC Data Blocks | Global and instance data blocks

Prerequisites

What you will need to follow along with this tutorial:

Global Array Data Blocks

A specific type of global data block is the Array data block. These are made up of an Array that can hold any data type, such as an Array composed of a PLC data type (UDT). Apart from the Array, there are no other elements within the DB. Array data blocks' flat structure is a great help. Why? Because it simplifies accessing the Array elements and transferring them to called blocks.

Navigate to the Move operations -> Array DB section in the Instructions -> Basic instructions task card. There, you can find additional addressing options for Array DBs. These instructions offer various choices, including indirectly addressing the DB name:

  • ReadFromArrayDB: Read data stored in an Array block.
  • WriteToArrayDB: Write data to an Array block.
  • ReadFromArrayDBL: Access data stored in an Array block in load memory.
  • WriteToArrayDBL: Write data to an Array block in load memory.
Figure 2.1 - Siemens TIA Portal PLC Data Blocks | SCL instructions for dealing with Array data blocks
Figure 2.1 - Siemens TIA Portal PLC Data Blocks | SCL instructions for dealing with Array data blocks

Array Data Blocks in Action: An Example

A conveyor belt is used to transport individual pieces of material. A scanner reads the information carried by these material pieces as they pass. Then, the readout information is transmitted to a control panel. Due to the varying clock cycles/speeds of the scanner and the control panel, the material information needs to be cached.

The setup of the example is made flexible. It allows for the absence of prior knowledge about which Array data block will be read or written during the program code creation. Hence, by using the DB_ANY data type, you can work with Arrays of different lengths. You can also achieve flexibility in specifying the value to be written or read through using the data type of VARIANT.

Figure 3.1 - Siemens TIA Portal PLC Data Blocks | A practical example of industrial automation using an Array data block
Figure 3.1 - Siemens TIA Portal PLC Data Blocks | A practical example of industrial automation using an Array data block

After creating a new project and configuring an S7-1500 PLC, construct a PLC data type named "UDT_Queue." Both "FC_Enqueue" and "FC_Dequeue" functions use this PLC data type. It becomes crucial when accessing the tag "#Queue.Used." How so? Since the "FC_Dequeue" function decreases the tag by one, while the "FC_Enqueue" function increases it by one.

Figure 3.2 - Siemens TIA Portal PLC Data Blocks | Information of created project for implementing an Array data block
Figure 3.2 - Siemens TIA Portal PLC Data Blocks | Information of created project for implementing an Array data block

In the project tree, locate the "PLC data types" folder and double-click on the "Add new data type" command. A declaration table will be generated and opened to enable the creation of a new PLC data type. Define the PLC data type by including the following lines: DB with the data type of DB_ANY, Size with the data type of DINT. Also, it includes Used with the data type of DINT, ReadPos with the data type of DINT, and WritePos with the data type of DINT.

Figure 3.3 - Siemens TIA Portal PLC Data Blocks | Creating a new PLC data type for implementing in an Array data block
Figure 3.3 - Siemens TIA Portal PLC Data Blocks | Creating a new PLC data type for implementing in an Array data block

Develop the "FC_Enqueue" function to write the material information values into the Array data block. The knowledge of the specific Array data block and the value data type is not essential at this stage. Why? Because the interfaces are being programmed using the data types of VARIANT and DB_ANY.

Generate an SCL function and assign it the name "FC_Enqueue."

Figure 3.4 - Siemens TIA Portal PLC Data Blocks | Creating an SCL function to write data into an array data block
Figure 3.4 - Siemens TIA Portal PLC Data Blocks | Creating an SCL function to write data into an array data block

Specify the block interface and compose the program code as depicted in Figure 3.5.

Figure 3.5 - Siemens TIA Portal PLC Data Blocks | Specifying an SCL function’s block interface for writing data into an Array data block
Figure 3.5 - Siemens TIA Portal PLC Data Blocks | Specifying an SCL function’s block interface for writing data into an Array data block

A) Using this function, you can verify if there is available space in the data block. If space exists, the value specified in the parameter value is written into the data block indicated by the parameter db.

B) Upon writing each new item of material information, the pointer tag "#Queue.WritePos and the tag "#Queue.Used" both experience an increment of one.

C) Once the cursor reaches the data block's end, it is automatically reset to the initial position of zero.

D) If the data block becomes occupied, the error code #4711 will be returned.

Figure 3.6 - Siemens TIA Portal PLC Data Blocks | Composing the program code for an SCL function to write data into an Array data block
Figure 3.6 - Siemens TIA Portal PLC Data Blocks | Composing the program code for an SCL function to write data into an Array data block

Develop the "FC_Dequeue" function to retrieve the material information from the Array data block and transfer it to the panel. Afterward, this information can be exhibited on the panel as a demonstration.

Generate an SCL function and assign it the name "FC_Dequeue."

Figure 3.7 - Siemens TIA Portal PLC Data Blocks | Creating an SCL function for retrieving data from an Array data block
Figure 3.7 - Siemens TIA Portal PLC Data Blocks | Creating an SCL function for retrieving data from an Array data block

Specify the block interface and compose the program code as depicted in Figure 3.8.

Figure 3.8 - Siemens TIA Portal PLC Data Blocks | Specifying an SCL function’s block interface for retrieving data from an Array data block 
Figure 3.8 - Siemens TIA Portal PLC Data Blocks | Specifying an SCL function’s block interface for retrieving data from an Array data block 

A) Using this function, you can verify the availability of material information in the data block. If that is the case, retrieve the value indicated by the pointer "#Queue.ReadPos" and store it in the tag "#Value."

B) Upon reading each item of material information, the pointer tag #Queue.ReadPos is incremented by one, and the tag "#Queue.Used" is decremented by one.

C) Once the cursor reaches the data block's end, it is automatically reset to the initial position of zero.

D) If the data block contains no information, the error code #4712 will be returned.

Figure 3.9 - Siemens TIA Portal PLC Data Blocks | Composing the program code for an SCL function to retrieve data from an Array data block
Figure 3.9 - Siemens TIA Portal PLC Data Blocks | Composing the program code for an SCL function to retrieve data from an Array data block

To retain the material data, set up an Array data block. Use the PLC data type "UDT_Material" for the Array data block's data type.

In the first step, generate the PLC data type "UDT_Material." The material information received from the scanner is captured within the structure of this PLC data type.

In the project tree, locate the "PLC data types" folder and double-click on the "Add new data type" command. A declaration table will be generated and opened to enable the creation of a new PLC data type. Define the PLC data type by including the following lines: ArticleNumber with the data type of DINT, ArticleName with the data type of STRING. Also, it contains Amount with the REAL data type and Unit with the STRING data type.

Figure 3.10 - Siemens TIA Portal PLC Data Blocks | Creating another new PLC data type for implementation in an Array data block
Figure 3.10 - Siemens TIA Portal PLC Data Blocks | Creating another new PLC data type for implementation in an Array data block

Generate the Array data block named "DB_MaterialBuffer." Data records within the Array data block should hold material information of the "UDT_Material" data type. The "FC_Enqueue" function is being used to transfer material information to the Array data block.

By double-clicking on the "Add new block" command, you can access the dialog box to add a new block. Click on the button marked "Data block (DB)." Enter the name "DB_MaterialBuffer." Pick "Array DB" as the type of data block. Specify the data type of the Array as the PLC data type "UDT_Material." Set the high Array limit to 1000 and then press ok.

Figure 3.11 - Siemens TIA Portal PLC Data Blocks | Creating an Array data block
Figure 3.11 - Siemens TIA Portal PLC Data Blocks | Creating an Array data block

Construct the startup organization block (OB) labeled "OB_MaterialQueue." Within this organization block, set up the initialization of the DB and Size tags.

By double-clicking on the "Add new block" command, you can access the dialog box to add a new block. Click on the button marked "Organization block (OB)." Enter the name "OB_MaterialQueue." Choose "Startup" as the type. Set the organization block's language as SCL and press ok.

Figure 3.12 - Siemens TIA Portal PLC Data Blocks | Creating a startup organization block for implementing in an Array data block
Figure 3.12 - Siemens TIA Portal PLC Data Blocks | Creating a startup organization block for implementing in an Array data block

Also, consider creating a data block named DB_MaterialQueue with the type of the "UDT_Queue" PLC data type.

Figure 3.13 - Siemens TIA Portal PLC Data Blocks | Creating a data block using a PLC data type for implementation in an Array data block
Figure 3.13 - Siemens TIA Portal PLC Data Blocks | Creating a data block using a PLC data type for implementation in an Array data block

Write the program code shown in Figure 3.14 within the startup organization block.

A) Through the data block assignment, you establish a connection between the Array data block and the SCL functions.

B) Input the Size parameter with the Array data block's size.

C) The Used parameter is initialized with a value of zero.

D) The material information for the first item is written to the Array element zero.

Figure 3.14 - Siemens TIA Portal PLC Data Blocks | Programming a startup OB for implementation in an Array data block
Figure 3.14 - Siemens TIA Portal PLC Data Blocks | Programming a startup OB for implementation in an Array data block

Define new tags as shown in Figure 3.15 in the "Default tag table."

Figure 3.15 - Siemens TIA Portal PLC Data Blocks | Creating tags for implementing them in the main program of an Array data block
Figure 3.15 - Siemens TIA Portal PLC Data Blocks | Creating tags for implementing them in the main program of an Array data block

Call the SCL function "FC_Enqueue" in the Main OB to capture the material information read by the scanner. Within the block interface's "Temp" section, define the tag "ConnectionToUDT." Then establish a link between this tag and the PLC data type "UDT_Material."

Figure 3.16 - Siemens TIA Portal PLC Data Blocks | Calling an SCL function to write data into an Array data block in the main OB
Figure 3.16 - Siemens TIA Portal PLC Data Blocks | Calling an SCL function to write data into an Array data block in the main OB

Associate the function call with the tags illustrated in Figure 3.17. Set up the positive signal edge at the enable input EN. Establish a link between the signal edge and the global tags specified in the standard tag table.

Figure 3.17 - Siemens TIA Portal PLC Data Blocks | Configuring a called SCL function to write data into an Array data block
Figure 3.17 - Siemens TIA Portal PLC Data Blocks | Configuring a called SCL function to write data into an Array data block

Call the SCL function "FC_Dequeue." Associate the function call with the tags shown in Figure 3.18. Set up the positive signal edge at the enable input EN. Establish a link between the signal edge and the global tags specified in the standard tag table.

Figure 3.18 - Siemens TIA Portal PLC Data Blocks | Configuring a called SCL function to retrieve data from an Array data block

What happens if a positive signal edge is detected? The instruction "WriteToArrayDB" starts recording material information in the Array data block. Then the information is delivered to the panel with the assistance of the "ReadFromArrayDB" instruction.

Figure 3.19 - Siemens TIA Portal PLC Data Blocks | Simulating the execution of an Array data block
Figure 3.19 - Siemens TIA Portal PLC Data Blocks | Simulating the execution of an Array data block

Conclusion

In conclusion, you learned about Array data blocks through a practical example. You figured that Array data blocks are a type of global data block that consists of an Array that can hold any data type, including a PLC data type. You understood these data blocks have a flat structure. How so? Because it simplifies accessing the Array elements and transferring them to called blocks. It makes them a convenient and efficient way to organize and manage data in a programming context.