Variable: VAR_GENERIC CONSTANT
Generic Constant Variable
A generic constant is a variable in the VAR_GENERIC CONSTANT
scope of a function block. The variable is defined only when the function block instance is declared.
Generic constants are allowed to be used in methods. This can be particularly helpful in the VAR_IN_OUT
or VAR_IN_OUT CONSTANT
scope.
Syntax of the type declaration
Syntax of the type declaration of a function block with generic constants
FUNCTION_BLOCK <function block name> VAR_GENERIC CONSTANT <generic constant name> : <integer data type> := <initial value> ; //Initial value will be overwritten END_VAR
| Name of the function block |
| Scope for generic constants You can declare any number of generic constants in the |
| Variable name of the generic constants |
| Construct which types the generic constant. An integer data type or the sub-scope of an integer data type is permitted. Example:
|
| Optional Initialization ImportantThe initial value is needed only for compile checks. At runtime, the value is overwritten. |
The generic constant can be used as usual in the implementation of the function block. These kinds of constants can be used, for example, for the indices of arrays or the lengths of strings.
Syntax of the variable declaration
Syntax of the variable declaration (instantiation of the function block) with a specified constant
The following options are possible:
PROGRAM <program name> VAR <fb instance name> : <function block name> < <literal> > ; <fb instance name> : <function block name> <( <expression> )> ; <fb instance name> : ARRAY [ <index start > .. <index end> ] OF <function block name> < <array length> > ; END_VAR
| Scope |
| Variable; name of the function block instance |
| Types the variable with the function block This is a function block with generic constants. |
| Optional Assigns a specific value to the data type which is valid only for this function block instance. The value is formally inserted Example: If multiple generic constants have been declared for the type-defining function block, then a comma-separated list of values is passed in angle brackets. There is one value for each constant. Example: |
| Optional Assigns a specific expression to the data type which is valid only for this function block instance. The expression is inserted The round brackets are necessary because it is permitted to use symbols such as Example: |
| Optional Types the variable with an array of function blocks. The length of the array is:
This is a function block with generic constants. The constants are used as array indices. Example: |
| Assigns a specific array length to the data type which is valid especially for this function block instance. The specified array length is formally inserted in angle brackets as an suffix to the data type. The specified value must match the array length as defined by the start index and end index. Example: |
Examples
FUNCTION_BLOCK FB_Pou VAR_GENERIC CONSTANT lnstring : DINT := 10; numstring : DINT := 100; END_VAR VAR arraystring : ARRAY[0..numstring-1] OF STRING(lnstring); END_VAR ;
PROGRAM PLC_PRG VAR fbPou : FB_Pou<100, 1000>; END_VAR
FUNCTION_BLOCK FB_SrString VAR_GENERIC CONSTANT maxlen2 : INT(5..10) := 5; //subrange data type END_VAR VAR arrTest : ARRAY[0..maxlen-1] OF BYTE; END_VAR ;
PROGRAM SrMain VAR CONSTANT cconst: INT(5..10) := 5; END_VAR VAR fbMyString1 : FB_SrString<5>; fbMyString2 : FB_SrString<(2 * cconst)>; arrMyString : ARRAY [0..5] OF FB_SrString<6>; END_VAR
The following code demonstrates how to define a function block which can process arrays of arbitrary length. The function block has an array with a generic but constant length. "Constant" means that, although each function block instance varies in its array length, it is constant during the lifetime of the object.
This kind of construct is beneficial, for example, to a library programmer who wants to implement a generic library POU.
FUNCTION_BLOCK FB_MyString VAR_GENERIC CONSTANT maxlen : UDINT := 1; END_VAR VAR test : ARRAY[0..maxlen-1] OF BYTE; END_VAR ;
PROGRAM PLC_PRG VAR CONSTANT cconst: DINT := 1000; END_VAR VAR fbMyString1 : FB_MyString<100>; fbMyString2 : FB_MyString<(2 * cconst)>; arrMyString : ARRAY[0..5] OF FB_MyString<6>; END_VAR ;
Inheritance
A function block can inherit from a base function block with a generic constant (EXTENDS
). The inheriting function block requires its own generic constant. A specific value can then be transferred externally.
Syntax:
FUNCTION_BLOCK <function block name> VAR_GENERIC CONSTANT <generic constant name> : <integer data type> ; END_VAR EXTENDS <function block base> < <generic constant name> >
A function block with a generic constant can implement an interface (IMPLEMENTS
). The interface declares a property (PROPERTY
) with which the specified value can be accessed externally. The interface itself must not declare any generic constants or local variables. Interfaces have no implementation.
Syntax:
FUNCTION_BLOCK <function block name> VAR_GENERIC CONSTANT <generic constant name> : <integer data type> ; END_VAR IMPLEMENTS <interface name>
Tip
When coding, make sure that the declaration of the generic constants is inserted first, followed by EXTENDS
and IMPLEMENTS
. This takes some getting used to, but the reason is that generic constants can also be used with base classes.
Definition of the IString
interface for a generic function block.
It is strongly recommended to define an interface for generic function blocks. The interface should allow the generic function block instance to be used without knowledge of the generic constants.
The Length
property enables access to the generic constant.
INTERFACE IString METHOD Append : BOOL VAR_INPUT strAppend : IString; END_VAR METHOD Assign : BOOL VAR_INPUT stringIn : STRING; END_VAR METHOD ToString : STRING VAR_INPUT END_VAR PROPERTY Length : DINT
Declaration of the function block FB_MyString
with the generic constant maxlen
FUNCTION_BLOCK FB_MyString VAR_GENERIC CONSTANT maxlen : UDINT; END_VAR IMPLEMENTS IString
The function block FB_LongString
is an extension of the specified function block FB_MyString
.
FUNCTION_BLOCK FB_LongString EXTENDS FB_MyString<1000>
Declaration of the function block FB_MySpecialString
with the generic constant maxlen2
as an extension of the specified function block FB_MyString
. The function block is extended by the method METH_ToLatin
.
FUNCTION_BLOCK FB_MySpecialString VAR_GENERIC CONSTANT maxlen2 : UDINT:= 1; END_VAR EXTENDS FB_MyString<maxlen2>
METHOD METH_ToLatin : STRING VAR_INPUT END_VAR
Instantiation of the function blocks with specific constants
PROGRAM PLC_PRG VAR CONSTANT cconst: DINT := 1000; END_VAR VAR string1 : FB_MyString<100>; string2 : FB_MyString<(2 * cconst)>; derived1 : FB_LongString; derived2 : FB_MySpecialString<100>; END_VAR
Calls
string1.METH_Assign ('Welt'); string2.METH_Assign ('Hallo '); string2.METH_Append(string1); derived2.METH_ToLatin('Hello World');