Reader small image

You're reading from  PLC and HMI Development with Siemens TIA Portal

Product typeBook
Published inApr 2022
PublisherPackt
ISBN-139781801817226
Edition1st Edition
Right arrow
Author (1)
Liam Bee
Liam Bee
author image
Liam Bee

Liam Bee has worked in automation for over 16 years, after starting his career at 16 years old as an instrument technician in the water industry. He began his automation journey by maintaining PLCs and the instruments connected to them. He found very early on that he had an interest in PLCs and automation, taking the time to learn PLC programming in his own time, as well as exposing himself as much as possible to automation while working. After 8 years of working in maintenance, Liam started his own side business, providing bespoke controls using lower-range PLC solutions. This experience proved invaluable to his progression; he learned quickly, often through failure, and his knowledge of control design improved significantly. 12 years into his career, he moved roles again; this time, he was looking for something to fill knowledge gaps and target Siemens as he was yet to use Siemens extensively. Liam started at Aquabio Ltd and quickly found himself immersed in Siemens SIMATIC Manager and TIA Portal. Over the next 3 years, he worked hard to understand Siemens' tools and development environments, calling on knowledge from other PLC environments that he had previously worked with. Over his years working with automation, he has learned many different languages and development environments. He has worked with Siemens, Allen Bradley, Schneider, Mitsubishi, and a host of other PLC platforms, all of which have helped shape the design concepts that he uses today. Liam has also taught himself computer programming languages such as VBA, VBS, VB.NET, C#, Java, and more. Closing the space between IT and industrial automation is important as time moves forwards and he has always tried his hardest to be at the forefront of innovation.
Read more about Liam Bee

Right arrow

Chapter 3: Structures and User-Defined Types

This chapter explores how to create, utilize, and deploy structures as structs and User-Defined Types (UDTs) effectively in a TIA Portal project. UDTs are the most underutilized tool in PLC programming and are extremely efficient and provide the rigid data structure required for asset-based programming. This chapter also expands on previously learned information about interfaces and how they affect data usage in logic objects.

After reading this chapter, a programmer should feel comfortable with both structs and UDTs in TIA Portal and have enough knowledge to do the following:

  • Add structs and UDTs to a project.
  • Know where UDTs are stored in a project.
  • Know the differences between structs and UDTs.
  • Know how to simplify interfaces to program blocks.
  • Know of potential drawbacks of using structs/UDTs and how to mitigate or overcome the issues that arise.

The following topics will be covered in this chapter:

...

What are structs and UDTs?

Essentially, structs and UDTs can be described as performing the same function: to hold data that has different types under a parent variable. While they perform the same fundamental function, a UDT is much more powerful than a struct. This is because a struct is a singular instance of a group of variables and a UDT is an actual data type.

Both of these datatypes can be described as a structure.

Structs

Structs are groups of variables defined under a parent variable with the datatype of struct. Siemens regards the use of structs as use only when necessary as they can add considerable overhead to a PLC's performance if used too often. Figure 3.1 shows two sample pumps' global data blocks for storing information associated with the asset:

Figure 3.1 – Example of structure definition in global data blocks

Both of these pumps use exactly the same struct, Raw_IO; however, they are not tied together in any way...

Creating struct/UDTs – best practices

When creating a struct or UDT, it is always best to think of the structure of the data. Remembering that other programmers need to easily pick up and use the data is the best way to ensure it is structured efficiently.

Understanding what is required

Choosing which type of structure is best for the application that is being built is fundamentally down to how it will be used.

Anonymous structures (structs) are best for grouping variables or other structures together, but not for use in interfaces.

The reason for this is that structs are not linked to each other. If 10 FBs make use of the same struct layout in interfaces and a new requirement is added to the struct, all 10 FBs require editing. UDTs are instantiated as a data type, so if a UDT is updated, all instances of the UDT update too. This is what makes UDTs more suited for creating standard code.

Defining structure variables

Before creating a structure, define exactly...

Simplifying interfaces with structs/UDTs

Structures (structs/UDTs) can be declared in the following interface scopes:

  • Input
  • Output
  • InOut
  • Temp
  • Static

Structures cannot be declared as either of the following:

  • Constant
  • Return

With this in mind, structures can be used to help simplify interfaces with program blocks. They can also help multiple blocks come together to access different parts of common data.

Passing inputs as a single struct

Passing data in and out of function blocks can help keep the complexity of the program object to a minimum, as shown here:

Figure 3.12 – Example of grouping inputs into a struct data type

Figure 3.12 demonstrates how using a struct could minimize the size of the interface used with the Structure_Pass_Example_FB function block. By accessing the data block that is holding the instance data for the function block, the Input interface data can be accessed and written to...

Drawbacks of structs and UDTs

Despite Siemens' excellent approach toward UDTs and structs, there are some drawbacks to using them.

Most of the issues are small and are easily managed, however, a potentially confusing issue (especially to programmers that are new to using libraries and UDTs) occurs when a UDT and a block dependent upon the UDT both appear in a Project library.

Libraries

UDTs that are stored in a library and are utilized in many places can cause a large number of objects to upgrade to a newer version if modified.

Remember

A program block is dependent upon a UDT if its interface contains a declaration of the UDT.

When a project library has no objects in test, it would look something like the following:

Figure 3.20 – Project library containing UDTs and function blocks

If UDT_Sample_Pump_Raw_IO is modified and a new version is created, the project library indicates that the UDT is in test:

Figure...

Summary

This chapter has introduced concepts behind using structured data in the form of structs and UDTs. It has highlighted the benefits of using structures, and also some of the pitfalls and issues that can be faced in large projects by using them. Remember that a structured project is far easier to navigate through, standardize, and work with in the future.

After reading this chapter, the asset-oriented approach should make sense from a structural perspective: containerizing data so that data that is only relative to a particular asset lends itself to structures (structs or UDTs).

Identifying what is needed in structures is always the hardest part, as structures can be difficult to keep updating in a system that is already operational, so it's best to try and plan for common use cases first.

TIA Portal offers a robust and highly flexible solution to managing data using structures. With two different types of structured data (struct and UDT), programmers can simplify...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
PLC and HMI Development with Siemens TIA Portal
Published in: Apr 2022Publisher: PacktISBN-13: 9781801817226
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Liam Bee

Liam Bee has worked in automation for over 16 years, after starting his career at 16 years old as an instrument technician in the water industry. He began his automation journey by maintaining PLCs and the instruments connected to them. He found very early on that he had an interest in PLCs and automation, taking the time to learn PLC programming in his own time, as well as exposing himself as much as possible to automation while working. After 8 years of working in maintenance, Liam started his own side business, providing bespoke controls using lower-range PLC solutions. This experience proved invaluable to his progression; he learned quickly, often through failure, and his knowledge of control design improved significantly. 12 years into his career, he moved roles again; this time, he was looking for something to fill knowledge gaps and target Siemens as he was yet to use Siemens extensively. Liam started at Aquabio Ltd and quickly found himself immersed in Siemens SIMATIC Manager and TIA Portal. Over the next 3 years, he worked hard to understand Siemens' tools and development environments, calling on knowledge from other PLC environments that he had previously worked with. Over his years working with automation, he has learned many different languages and development environments. He has worked with Siemens, Allen Bradley, Schneider, Mitsubishi, and a host of other PLC platforms, all of which have helped shape the design concepts that he uses today. Liam has also taught himself computer programming languages such as VBA, VBS, VB.NET, C#, Java, and more. Closing the space between IT and industrial automation is important as time moves forwards and he has always tried his hardest to be at the forefront of innovation.
Read more about Liam Bee