R1.0 – Example of constant limits and how they are used normally in the logic

In Siemens TIA Portal, it is possible to create Arrays that use Constants to define the size of the Array. This means that the Array’s size can be “Semi-dynamically” defined between different projects.

Use Case Scenario

Imagine that you had 3 projects for a conveyor belt system that consisted of the following:

  • N1 number of Proximity Sensors for material detection
  • N2 number of Actuators for direction control of material
  • N2 number of Weight Cells for storage bin full detection

Without using Arrays that are bound by Constants, you would need to re-size your Array every single time that a project’s N1 or N2 value changes.

However, by using Constants, you may only need to change it in one place…

Different Types Of Constants

There are two different ways that you can define Constant array boundaries (limits), either using Local (#) Constants, or by using Global ("Variable") Constants.

The difference between the two is all about Hierarchical Access to the Constant.

Local Constants

R1.1 – Local Constants example

The above image (#R1.1) shows an example of a Bool Array being declared with the Lower Boundary set as #X and the Upper Boundary set as #Y.

In order for this to work, both #X and #Y need to be set in the Constant scope area of the block the Bool_Array resides within:

R1.2 – Local Constant declaration

#R1.2 is a declaration in the Constant scope section of the interface that defines what value #X and #Y represent.

In this case, #X = 0 and #Y = 9

This is no different to physically typing the Data type declaration of Bool_Array used in #R1.1 as Array[0..9] of Bool.

📝Note
Constants used in defining Array Boundaries (limits) must be a numerical Data type. This means that they cannot be Hexadecimal types such as Byte or DWord:

R1.3 – Incorrect Data type used

The above shows that although the Constant is declared correctly, the Array is not happy with the configuration of the data type.

The most important thing to remember with Local Constants, is that they are only usable in the Program Block that they reside in. You cannot address Local Constants from outside of the Function or Function Block.

Global Constants

R1.4 – Global Constant example

Unlike Local Constants which are defined inside of a Program Block, Global Constants are defined in the PLC Tags area, on the tab called User Constants

R1.5 – Global Constant declaration

#R1.5 above shows where Global Constants are defined.

📝Note
You can define User Constants, (Global Constants), in every PLC Tag object created. You can therefore organise your User Tags as you wish

The main difference with Global Constants is that they can be accessed anywhere in the project. This means that any Function Block or Function can have their Array resized by simply changing the value assigned at the User Constant‘s Value.

📝Note
User Constants are still bound by the same rules for Data type as Local Constants. You can’t use Bytes for example

Accessing Array Elements

R1.6 – Accessing Array Elements

The above image (#R1.6) shows that accessing an element from the Bool_Array is achieved in the “Normal” way. Simply using an index value, such as 0, will access that value within the Array.

📝Note
You can also use Constants to access Array elements:

R1.7 – Using a Constant to index an Array

The above shows that the Local Constant #X is being used to index the Bool_Array. This means that the index value being used is actually 0.

Additional Considerations When Using Global Constants

[membership level=”-1″]

Members
Currently Learning With Do & Grow!

[/membership]

[membership level=”1″]

Do & Grow promotes a Standardized and Structured approach to development of PLC Projects, Global Constant usage can damage this concept very quickly!

It is really important to understand how Libraries and Typed Objects manage access to external resources.

R1.8 – Global PLC Constant warning

The above warning is thrown by TIA Portal when trying to version the Program Block that contains references to Global Constants. The reason this message appears is to remind you that this block is dependant upon the Global Constants. The message that is actually given by TIA is really poor here and doesn’t really explain anything!

If you are releasing versions of library objects that are dependent upon User Constants, you must ensure that the same User Constants are available in any project in which your library object will be used. Because User Constants are not another Program Block, or a UDT, Types (in Project / Global Library) that use User Constants are not able to set a true dependency path to the User Constants that are used.

What this means is that when the Array_Limits_Example (the block being added to a library in #R1.8) is dragged into the Project Tree, it cannot create the User Constants required. The Array_Limits_Example block is therefore broken and cannot compile when used from the Library.

You must ensure that the User Constants exist in the project in order to fix the compile issues. TIA does not recommend that Typed objects be dependent on anything that cannot also be added to the Type folder in the Library.

🎯 Do & Grow…
This could be confusing if your experience with Project Libraries is low, but the best thing to do in this area is to try it out!

Try adding an Array that has its boundaries set by User Constants. Then, try to grab the object into the Type folder in the Project Library.

Now, delete the object from your Project Tree, and delete the User Constants.

Drag the Project Library version of the block into your Project Tree and observe that it is broken due to User Constants missing. Now observe that you cannot fix the issue inside the object without creating a new version… This can start to get messy if you are not careful!!!

If you get stuck with anything, ask on Discord, leave a comment here, or ask me a question on LinkedIn!

[/membership]

Key Takeaways…

  • Arrays that use Constants are passively dynamic. This means that you can change many different array sizes by simply changing one or two constants
  • Be careful with Local and Global constants, depending on what you are doing, they have different strengths in their usage
  • Remember that Constants cannot be changed in Runtime
  • Remember that Arrays that use Constants require the Constant to be a whole Numerical value. Usually INT is the default choice

More About Arrays…


Check Out Another Post

Leave a Reply