Programming S71500 using TIA V15 Small tips and Trix. PART#2
Mattias Lindh
Automation Engineer @ Mattias Lindh Automation AB | PLC Programming Expert
Hello again nice that you found the way back here. Don’t forget to register on my forum Swiaf to help me to form the new automation forum. https://swiaf.se/. As said before all this articles with code can be found here soon.
PART #2
Using variant pointer in TIA V15 and the new simplified ‘?= ‘ operator.
What is an Variant pointer? An Variant pointer is nothing else than a pointer that can point to a data area in your controller. Quite similar with the old any pointer but with a more symbolic approach. It can point on a datatype that you created yourself or any other predefined data type and even other blocks. You can easily check which datatype the any pointer points at by using the instruction: TypeOf. E.g.
An Example Calculating area of different objects.
We want to create a simple function that can calculate the area of different objects, in this example we use Circle, Rectangle and Triangle. First, we define our three different objects by means of creating three different data types:
DataType #01: TCircle: 2 reals one radius and one area variable:
DataType #02: TRectangle: 3 reals one Height, Base and one Area.
DataType #03 Triangle, Same data as above.
Now we want to create our function for calculating the area of any of the above objects. We create a new FC called SF_CalcArea. (I use to mark all my standardfunctions With SF in the beginning of the name). We define parameters in head as shown below.
We define one in/Out Parameter That is defined as a variant to be able to point on any object from which we want to calculate the area. Our function will return the calculated area and update the object .area variable. We define 3 Temp variables as references to the defined objects.
E.g.
REF_TO “TCircle” : Defined as a reference to a circle. (Note the REF_TO in front of the declaration, this indicates that this is to be a reference to a variable, this is the new TIA V15 twist saving you time). Do the same for the two other objects Rectangle and Triangle.
We also define one constant PI: I use to name all my local constants to start with CF = Constant in Function. I always write my constants in capital letters, everything to make the program more readable. If I would have selected to make the constant global, (Defined in a tag table) I would name it C_PI instead… Anyway. I also define a return value = An output from my function to be a Real Value= The calculated area.
Okay, Now the code, in older versions of Tia you had to use the instructions VARIANT_GET and VARIANT_Put to read and write values to variables pointed out by variant pointers. This was not that effective, in TIA V15 you simply use the new operators:
=? -> Assign attempt – In this case a reference to a tag of type Variant
^ -> Access the value of a referenced tag.
The code for the calculate object area will look at shown below:
First, we check the type of the object by using a case statement together with the type of instruction. Then we make one case per Object and write the code for calculating each area.
E.g. TCircle:
#TmpCircle ?= #object.
We assign reference to an object of type TCircle. As we have already checked that the object is a circle we do not have to take care of the situation that the reference fails. You can always check the result by comparing it to Null, Se below.
We Calculate the area for the circle as we all know: Radius*Radius * PI. Note the operator **2 that is equal to power of 2 (from TIA V14). Also note when we reference the Circle we use the ^ operator, giving us the referenced value.
E.g. #Tmp_Circle^.Radius.
Since TIA V14 it is also possible to assign more than one variable with the same value by Separating them with := . We use this approach to assign both the result of the function and the object area.
#CalcArea := TmpCircle^Area := [Calculated value]
To test the function, we create a new global DB: MyObjects and adds 3 Shape objects to it:
In OB1 we can now make calls to the function with different objects:
Please note that all this can be done in LAD and other languages as well.
Don’t hesitate to contact me if you have any questions or if u want to learn more about TIA portal.
Make comments, I love your feedback. If you like it, then like the article. If you hate it then write this as well. And finally, do not forget that any contact you make here at LinkedIn may be a new opportunity for yourself. Keep sharing.
Until then Have a nice day and good luck with your Coding.
//Mattias Lindh, Mattias Lindh Automation AB.
Industrial Automation,Warehouse Automation,SIEMENS
5 年When I compile the block,I am getting an error" Access through non-initialized pointer Can you please upload the programme
Automation Engineer @ Mattias Lindh Automation AB | PLC Programming Expert
6 年Thanx for the feedback.
Automationsingenj?r p? AFRY BA Advanced Automation
6 年I like what you are doing!