Difference Between Call of function block in IEC61131-3

521 Views Asked by At

What the difference between assigning a parameter with dot and put as an array to a function block?

here is a simple code as an example.

Timer1(IN:=TRUE,PT:=T#2S);
IF Timer1.Q THEN
    i:=i+1;
    Timer1.IN:=FALSE;
END_IF

Timer2(IN:=TRUE,PT:=T#2S);
IF Timer2.Q THEN
    j:=j+1;
    Timer2(IN:=FALSE);
END_IF

It expected that Timer1 be reset by this Timer1.IN:=FALSE; assignment but nothing happened, although it shows FALSE in the code as a real-time value!

enter image description here

any help would be appreciated.

1

There are 1 best solutions below

0
On

There are just two characters which make a huge difference between the outcomes: (). The brackets indicate that the function block is called, which means that the implementation part of a function block is executed.

In order to illustrate the differences, let me make an example function block to show the differences.

Example

Here I define a function block with a single input Increment and a single output Count. Every time the function block is called it will run the code in the implementation part. The implementation part will increment the current Count by Increment.

FUNCTION BLOCK FB_Counter 
VAR_INPUT
    Increment : UINT := 1;
END_VAR
VAR_OUTPUT
    Count : UINT := 0;
END_VAR

// Implementation part
Count := Count + Increment; 

Let me make an instance counter of this function block in a program Runner, so we can see what happens when we call it.

PROGRAM Runner
VAR
    counter : FB_Counter;
END_VAR

counter(); // Count is set to 1 since the function block implementation is called
counter.Increment := 2; // Increment is set to 2, Count is still at 1 since the implementation of the function block is not called.
counter(); // Count is set to 3 since the implementation of the function block is now executed
counter(Increment:=1); // Increment is set back to 1 and the function block is called again, increasing the Count to 4.

If you step through the above code by using a break point you can see what happens at every step of the way.