Makers Blog Archive

Using methods in IEC 61131-3 programming language

Sven Lemmens 27 June 2019 min. read
1,274 views 2 comments

PLCnext Engineer supports object-oriented programming for function blocks by using methods. By adding a method to a user-defined function block POU, the function block is defined as an object-oriented function block.

To give you a good overview about methods, we will program a small function block with a counter functionality.

Add a function block with name ‘FB_CounterExample’. We need 3 input variables of type BOOL.

1

To see the result of our small program, we will use an output of type INT with name iValue.

2

Now, we are going to add the first method to increase the counter value. Right click on the function block in the components window and choose ‘Add Method’.

Double click on the method name and choose programming language ST. Now you will get a signature window. Similar to function POUs, methods do have a result type, a variable declaration part and one or more code worksheets.

For each method an access specifier must be specified. This access specifier defines from where the method may be called.

‘Public’: the method can be called from anywhere where it can be used (internal and external calls).

‘Private’: the method can only be called from inside the POU where the method is defined (internal calls only).

3

For this example we select INT and Private. Now we implement the following code.

// Increase the counter value
AddValue:= iValue + 1;

Now we add 2 other methods, called SubtractValue and ResetValue. Both have a return type INT and specifier Private. We need the following functionality.

// Decrease the counter value
IF iValue > 0 THEN
    SubtractValue:= iValue - 1;
ELSE
    SubtractValue:= 0;
END_IF;

// Reset the counter value
ResetValue:= 0;

Now, we are going to add some triggers in order to use the different methods. The following code should be added in the code sheet from the function block.

R_Add(CLK := xAdd);
R_Subtract(CLK := xSubtract);
R_Reset(CLK := xReset);

IF R_Add.Q THEN
    iValue:= THIS.AddValue();
END_IF;

IF R_Subtract.Q THEN
    iValue:= THIS.SubtractValue();
END_IF;

IF R_Reset.Q THEN
    iValue:= THIS.ResetValue();
END_IF;

The result should look like this. Please pay attention to the keyword ‘THIS’.

4

Next point is to use our function block in a program ‘Main’ and have a look towards the result.

7

Ok, so far, so good. Let’s have a look to some other possibilities. Add an extra method with name ‘InitValue’, return type INT and specifier public. Furthermore, we will give this method an input of type INT.

// Set the value equal to the init value
InitValue:= iInitValue;
iValue:= iInitValue;

 Add following code to your program and see what happens.

IF xTestInit THEN
    xTestInit:= FALSE;
    iResult:= FB_CounterExample_Blog.InitValue(5);
END_IF;

To finish, we have one additional option left. We are going to create an own return type.

Add the following data type to your program in the component window.

5

We need this datatype in our last method called ‘CurrentValue’. ‘sTemp’ and ‘sValue’ are both local variables of type STRING.

sTemp:= 'The current value is equal to: ';
sValue:= TO_STRING(iValue,'{0:d}');
sValue:= CONCAT(sTemp,sValue);
CurrentValue.iValue:= iValue;
CurrentValue.sValue:= sValue;

The final result:

8

Note:

The Makers Blog shows applications and user stories of community members that are not tested or reviewed by Phoenix Contact. Use them at your own risk.

Discussion

Please login/register to comment

Login/Register

Leave a Reply

dcshaver@mmm.com 16.08.2023

That's very interesting. The example calls the public InitValue method from an external structured text program. Would that be possible from a LD program?

Login / Register to reply
Martin PLCnext Team 17.08.2023

Yes, that's possible. In the tree view on the right, you should see all the methods listed beneath the Function Block. You can just drag the method on to the FB code sheet. You will need to provide the name of an FB instance, that the method will be called on. Alternatively, you can just type the name of an FB instance on the FB code sheet, then type a full-stop (or dot), and pick the method you want to call.

dcshaver@mmm.com 17.08.2023

Great! Thank you!

mbergman 16.11.2023

Thank you! I used this to understand that you set the name of the method to the value ie. SubtractValue := 0 when SubtractValue is the name of the method. I have to test, but maybe someone has a definitive answer: What happens if I return a value from the method only in certain cases?

Login / Register to reply
Newsletter
Never miss a new article
Sign up for the newsletter
Never miss news about PLCnext Technology
Get interesting content via newsletter four times a year
Receive exclusive information before all other users