IEC 61131-3: Parameter transfer via parameter list

Parameter lists are an interesting alternative for transferring parameters to PLC libraries. Strictly speaking, these are global constants (VAR_GLOBAL CONSTANT) whose initialization values can be edited in the Library Manager.

When declaring arrays, their boundaries must be defined as constants. At the time of compilation, it must be known how large the array should be. The attempt to define the array boundaries by a variable is rejected with an error message.

PROGRAM MAIN
VAR
  nUpperBound      : INT := 5;  
  aTest            : ARRAY [1..nUpperBound] OF INT;
END_VAR
 
// Compiler error: Border 'nUpperBound' of array is no constant value

Only the use of constants solves the problem.

PROGRAM MAIN
VAR
  aTest            : ARRAY [1..nUpperBound] OF INT;
END_VAR
VAR CONSTANT
  nUpperBound      : INT := 5;
END_VAR

This can lead to problems, especially in PLC libraries. For example, if you want to map a data buffer to an array, it is not necessarily known at the time of library development how large this data buffer has to be in the respective applications.

The solution here is a parameter list, which is inserted when creating a PLC library.

Pic01

A parameter list can be added at any point in the PLC project (the PLC project from which the PLC library originates). Parameter lists are usually created for the global variable lists (GVLs).

Pic02

The parameters are declared as constant global variables.

{attribute 'qualified_only'}
VAR_GLOBAL CONSTANT
  cUpperBound    : INT := 3;
  cString        : STRING := 'Hello';
  cIntArray      : ARRAY [1..cUpperBound] OF INT := [1, 2, 3];
  cStructArray   : ARRAY [1..3] OF ST_Test := [3( (nValue := 1,
                                                   fValue := 3.1,
                                                   wsValue := "abc") )];
  cEnum          : E_Test := E_Test.eValue02;
  cStruct        : ST_Test := (nValue := 1, fValue := 3.1, wsValue := "abc");
END_VAR

The variables can be pre-initialized by initialization values. Even more complex variable structures can be initialized with values.

These parameters can be edited in the Library Manager in the application in which the library was integrated. The Library Manager is opened by double-clicking on the respective library reference.

Pic03

The fields in the column Value (editable) can now be changed as required. These changeable constants can be used within the respective PLC library or in the application that references them.

FUNCTION F_AddArrayValues : INT
VAR_INPUT
  aTest      : REFERENCE TO ARRAY[1..Param.cUpperBound] OF INT; 
END_VAR
VAR
  nIndex     : INT;
END_VAR
 
FOR nIndex := 1 TO Param.cUpperBound DO
   F_AddArrayValues := F_AddArrayValues + aTest[nIndex];
END_FOR

The changed initialization values are stored in the respective PLC project file (*. plcproj). Thus, each application that references a library with a parameter list has its own values. The library itself remains unchanged.

The values set in this way also withstand a PLC reset, both a cold reset and resetting to the original values. The values are not reset until the library is removed and reinserted. This means that parameter lists can also be used to transfer general parameters.

All parameters in a parameter list are usually accessible via ADS. The fact that parameters always have to be declared with CONSTANT does not prevent them from being written via ADS.

If a parameter is used as an array boundary, changing these variables will have serious consequences. Especially if the value of the parameter is increased.

aTest   : ARRAY [1..MyLibrary.Param.cUpperBound] OF INT;

Even if the parameter is increased at runtime of the PLC program, the array retains its original size. If this parameter is also used in the loops, accesses outside the allowed array boundaries can occur.

FOR nIndex := 1 TO MyLibrary.Param.cUpperBound DO
  aTest[nIndex] := nValue;
END_FOR

To prevent this, the attribute tc_no_symbol can be used to prevent ADS symbol generation. Access via ADS using the symbol name is then no longer possible for this parameter.

VAR_GLOBAL CONSTANT
  {attribute 'tc_no_symbol'}
  cUpperBound             : INT := 5;
END_VAR

Author: Stefan Henneken

I’m Stefan Henneken, a software developer based in Germany. This blog is just a collection of various articles I want to share, mostly related to Software Development.

4 thoughts on “IEC 61131-3: Parameter transfer via parameter list”

  1. ADS is a Beckhoff specific protocol. Can you think of generic protocol alternatives available to access the properties of the parameter list?
    I suppose netvars, if they are supported by the target runtime, but what else?

  2. Mr. Henneken thank you for the article. I have a couple of concerning:
    F_AddArrayValues is declared in Library?
    I do not understand well the meaning of {attribute ‘tc_no_symbol’}

    1. Hi,Yes, F_AddArrayValues is an example of a function in a library. In the same library is the parameter list with the variable ‘cUpperBound’. This parameter can be edited in the Library Manager in the application in which the library is integrated.The attribute ‘tc_no_symbol’ is an extention of TwinCAT. This attribute can be used to specify that no symbol is generated for a variable. In other words, symbolic access via ADS is prevented.
      Stefan

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: