PLC

ANY and ANY_<type>

If one of the function inputs (VAR_INPUT) has a generic IEC data type (ANY or ANY_<type>) when implementing a function, the data type of the call parameter is not defined unambiguously. Variables with different data types can be passed to this function. The passed value and its type can be queried within the function via a predefined structure.

The generic IEC data types shown below are supported for use with function inputs. The table shows which generic data type allows which elementary data types.

Generic data types
Elementary data types
ANY
ANY_BIT
 
BOOL, BYTE, WORD, DWORD, LWORD
ANY_DATE
 
DATE_AND_TIME, LDT, DATE, TIME_OF_DAY, LTOD
ANY_NUM
ANY_REAL
REAL, LREAL
ANY_INT
USINT, UINT, UDINT, ULINT
SINT, INT, DINT, LINT
ANY_STRING
 
STRING, WSTRING

Example:

FUNCTION ANYBIT_TO_BCD : BCD
VAR_INPUT
    value : ANY_BIT;
END_VAR

When the ANYBIT_TO_BCD function is called, a variable with the data type BOOL, WORD, DWORD or LWORD can be passed to the function as a parameter.

Predefined structure

When the code is compiled, an ANY data type is replaced internally with the following structure:

TYPE AnyType :
STRUCT
// the type of the actual parameter
    typeclass : __SYSTEM.TYPE_CLASS ;
// the pointer to the actual parameter
    pvalue : POINTER TO BYTE;
// the size of the data, to which the pointer points
    diSize : DINT;
END_STRUCT
END_TYPE

The actual call parameter is then assigned to the structure elements at runtime.

Example:

This compares whether the two variables passed have the same type and value.

FUNCTION Generic_Compare : BOOL
VAR_INPUT
    any1 : ANY;
    any2 : ANY;
END_VAR
VAR
    nCount: DINT;
END_VAR
Generic_Compare := FALSE;

IF any1.typeclass <> any2.typeclass THEN
   RETURN;
END_IF

IF any1.diSize <> any2.diSize THEN
    RETURN;
END_IF

// Byte comparison
FOR nCount := 0 TO any1.diSize-1 DO
    IF any1.pvalue[nCount] <> any2.pvalue[nCount] THEN
        RETURN;
    END_IF
END_FOR

Generic_Compare := TRUE;
RETURN;
// END_FUNCTION