VARIANT data type in TIA Portal, Example of using Reference

VARIANT TIA Portal

The VARIANT data type is a pointer or a reference to another data object. You can read out the Variant data type of a referenced tag during program runtime.

In Our example we use “TypeOf” Instruction to query data type connected to input of our function. Solution here is to have 1 standard block for Liquid tank. Each liquid may have different properties, for example from water tank we need only data about temperature of water. For Oil tank we need level, temperature and viscosity.

Important: Following example of Referencing Variant Data Type can be only used in Function written in SCL language

Let’s create first our function for a tank. Our Variant data type is defined as InOut. We will use it to query data type of udt_WaterTank or udt_OilTank

Our VARIANT is the udt of which Values are stored in DataBlock and mapped to InOut of our Function

Writing code for Variant data Type

First step is to check which udt is connected to the function block interface and copy valid data to Temp structure of tank UDT. for this we use to Siemens TIA Portal Instructions: TypeOf to compare data types and MOVE_BLK_VARIANT to copy data. Code look like this:

In Line 16 and 19 we check if connected Udt is the same type as Udt in our function temp area. If condition is True, then we store type Tank type in udtType as Integer , so we can use it later in our program. With MOVE_BLK_VARIANT we copy data from dbtankData to UDT of specified tank type.

Mapping VARIANT data to function output

Now having data store in our function, we can map data to the output of the block. Knowing value of our Integer “udtType” we can use CASE OF instruction to select logic for specific data type.


Note! Direct passing of a tag to a VARIANT tag is not possible, such as myVARIANT := #Variable

Let’s load our code to PLC and see the Results.

What we achived here is that we have one standard block for a tank. To get right data of tank type , we need to connect out tank Data to variant Input of the function and code will handle of processing by quering Variant data Type

Video will be available soon on our Antomatix Channel;

https://youtu.be/83zFa3-napQ

https://www.youtube.com/@antomatix5075/videos

By admin

Related Post

One thought on “VARIANT data type in TIA Portal, Example of using Reference”

Leave a Reply