Using a Global Array Data Block in Siemens S7-1500 PLC
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.
Prerequisites
What you will need to follow along with this tutorial:
- The TIA Portal software should be installed on your computer. While version 16 is used in this tutorial, other versions of the TIA Portal are also compatible
- Understanding main types of programming blocks.
- Understanding SCL block creation.
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.
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.
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.
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.
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."
Specify the block interface and compose the program code as depicted in Figure 3.5.
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.
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."
Specify the block interface and compose the program code as depicted in Figure 3.8.
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.
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.
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.
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.
Also, consider creating a data block named DB_MaterialQueue with the type of the "UDT_Queue" PLC data type.
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.
Define new tags as shown in Figure 3.15 in the "Default tag table."
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."
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.
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.
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.
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.