Any Data Type

Often when creating POUs it is necessary to exchange data with other parts of the program. It is not always possible to know the data type to be exchanged when creating the POUs. In these cases the ANY data type can be used. This allows the programmer to attach numerous data types to an input without compiler errors. It is then up to the POU creator to determine which data type has been attached and how to handle the data.

The general order of steps in this example are:

  1. Create a function block with an input of the type ANY

  2. Add this FB to a program and attach a variable to the input of the FB

  3. Read the information from ANY variable

  4. Determine the appropriate way to handle and use the data.

 

 

 

  1. Create a function block with an input of the type ANY

      In this example the Function Block is named "DataTypeInput" and the input variable is named "anTestInput".

image-20240213-214812.png

 

  1. Add this Function Block to a program and attach a variable to the input of the FB

         Here, an instance of the Function Block "DataTypeInput" has been created in the "PLC_PRG" program. Notice that the input variable is of type "INT".

image-20240213-214805.png

 

  1. Read the information from ANY variable

       At this point it is helpful to load the program and look at how the "ANY" data type behaves during runtime. The screen shot below is from the "DataTypeInput_0" instance of the "DataTypeInput" Function Block during runtime.

 

It can be seen above that the ANY data type does not actually provide the value of INT input. Rather, the ANY variable provides some key information.

The information provided is:

         pValue: Pointer to the location of the data,

         diSize: Memory size of the input variable

         TypeClass: Type of the variable which has been attached to the input. In this case, the type is INT, represented as "TYPE_INT". This is because the variable type that was attached in PLC_PRG is of type "INT"

 

  1. Determine the appropriate way to handle and use the data.

        Now the information about the input variable will be used to retrieve the actual data contained in the INT input variable "iTestVar". There are a number of ways to retrieve the value from the input variable. Pointers will be used to demonstrate this example.

Create a variable of the "Pointer TO INT" type inside of the DataTypeInput" Function Block. Then create a variable of type "INT".

Next, the value of the pointer (pValue) from anTestInput is assigned to pTestVarInternal. Then pTestVarInternal is dereferenced and assigned to iTestVarInternal.

In this case, the data conversion is happening implicitly. When the pointer from the ANY data type is assigned to the internal pointer variable "pTestInternal" the assumption has already been made that the variable is of type "INT" because "pTestInternal" is of type "POINTER TO INT".

 

Logging on and running the code produces the following results. A value of "2" is assigned to "iTestVar" of "PLC_PRG".

 

 

 

Looking at "iTestVarInternal" we see that the value of "2" has been successfully passed inside of the Function Block and can now be used for programming.

 

Additional Information

This example worked because we knew that the Input variable was of type "INT" so the variable "pTestVarInternal" was of type "POINTER TO INT". The purpose of the ANY variable type is that the input type is not known when programming the FB and therefore must be handled at runtime. One possible way of handling this is by checking the TypeClass of the input variable. IF statements can be used to determine the type and take appropriate action.

 

Below an "IF" statement has been created to determine if the type of the input variable "anTestInput" is of type INT or REAL. Then the appropriate pointer variables are used to capture and convert the data to a usable value inside of the program.

 

Next, change the input variable to "DataTypeInput_0" to be a "REAL" type. Log into the device to see the results.

 

Looking inside of the "DataTypeInput_0" block we see that the single input is able to appropriately handle a "REAL" value as well as an "INT" value. The value of 2.222 has been transferred.

Â