Generic constant variables - VAR_GENERIC CONSTANT

A generic constant is a variable in the VAR_GENERIC CONSTANT declaration section of a function block that is not defined until the function block instance is declared.

Generic constant variables - VAR_GENERIC CONSTANT 1:

Available from TC3.1 Build 4026

Syntax

Declaration of the function block:

FUNCTION_BLOCK <function block name>
VAR_GENERIC CONSTANT
    <generic constant name> : <integer data type> := <initial value>; //Initialwert wird überschrieben
END_VAR

The generic constant (integer data type) can be used within a function block as usual. For example, you can use the constant to define the size of arrays or the length of strings. The initial value is only needed for compile checks. At runtime, the value is overwritten.

Declaration of the function block instance:

PROGRAM MAIN
VAR
    <fb instance name> : <function block name> < <literal> >;
    <fb instance name> : <function block name> <( <expression> )>;
END_VAR

When the function block instances are declared, the constant is assigned a specific value that is valid only for this instance. For this purpose, the value (<literal>) is appended in angle brackets to the function block acting as data type.

Alternatively, an expression (<expression>) can be appended. However, this must be enclosed in round brackets, since it is allowed to use symbols like < or > in an expression. Otherwise, the uniqueness of the code is no longer guaranteed.

Sample

Generic function block with parameterizable array variable

The following code shows how to define a function block that can process arrays of any length. The function block has an array with a generic but constant length. By "constant" is meant that although each function block instance varies in its array length, it is constant during the lifetime of the object.

Such a construct is beneficial, for example, to a library programmer who wishes to implement a generic library function block.

FUNCTION_BLOCK FB_Sample
VAR_GENERIC CONSTANT
    nMaxLen : UDINT := 1;
END_VAR
VAR
    aSample : ARRAY[0..nMaxLen-1] OF BYTE
END_VAR
PROGRAM MAIN
VAR CONSTANT
    cConst    : DINT := 10;
END_VAR
VAR
    fbSample1 : FB_Sample<100>;
    fbSample2 : FB_Sample<(2*cConst)>;
    aSample   : ARRAY[0..5] OF FB_Sample<10>;
END_VAR

Inheritance

A function block with generic constants can also use the inheritance constructs EXTENDS and IMPLEMENTS.

When implementing, make sure that the declaration of the generic constants is inserted first, followed by EXTENDS and IMPLEMENTS. The reason for this is that generic constants can also be used with base classes.

Sample

INTERFACE I_Sample
FUNCTION_BLOCK FB_Sample
VAR_GENERIC CONSTANT
    nMaxLen   : UDINT := 1;
END_VAR
IMPLEMENTS I_Sample
VAR
    aSample   : ARRAY[0..nMaxLen-1] OF BYTE
END_VAR
FUNCTION_BLOCK FB_Sub_1 EXTENDS FB_Sample<100>
FUNCTION_BLOCK FB_Sub_2
VAR_GENERIC CONSTANT
    nMaxLen2  : UDINT := 1;
END_VAR
EXTENDS FB_Sample<nMaxLen2>
PROGRAM MAIN
VAR
    fbSample1 : FB_Sample<10>;
    fbSub1    : FB_Sub_1;
    fbSub2    : FB_Sub_2<20>;
END_VAR