Join us today!

VAR_INST vs VAR_STA...
 
Notifications
Clear all

VAR_INST vs VAR_STAT in TwinCAT

1 Posts
1 Users
0 Reactions
763 Views
twinControls
Posts: 114
Admin
Topic starter
(@twincontrols)
Member
Joined: 2 years ago

In the previous tutorial, we have explained how to retain the variable values in a method every method call. Another alternative way is to use VAR_STAT. However, memory address of the variable for every instantiation of the function block of where the method resides will be the same. 

To observe this, we have added an output of the nCounter memory address in the M_Count method. Let's test it with VAR_INST first. 

FB_Utilities.M_Count when VAR_INST is used: 

METHOD M_Count : BOOL
VAR_INPUT
	bStart : REFERENCE TO BOOL;
END_VAR
VAR_INST
	nCounter : INT;
END_VAR
VAR_OUTPUT
	nCounterADR : ULINT;
END_VAR
IF bStart THEN
	nCounter := nCounter +1 ;
	IF nCounter > 50 THEN
		ADSLOGSTR(ADSLOG_MSGTYPE_WARN, 'Counter limit has been reached!', '');
		bStart := FALSE;
		M_Count := TRUE;
	END_IF
END_IF

nCounterADR := ADR(nCounter);

 

In the main program, create two instances of FB_Utilities. 

VAR
	fbUtilities : FB_Utilities;
	bStart : BOOL;
	counterADR : PVOID;
	
	fbUtilities_2 : FB_Utilities;
	bStart_2 : BOOL;
	counterADR_2 : PVOID;
END_VAR
fbUtilities.M_Count(bStart := bStart,nCounterADR =>counterADR);

fbUtilities_2.M_Count(bStart := bStart_2,nCounterADR =>counterADR_2);

 

Login the plc and check the counterADR and counterADR_2 values. You will see that each counter variable in M_Count method have a different memory address since we have used VAR_INST. 

adr var inst

 

Now, let's use VAR_STAT instead. Make the changes in the method: 

FB_Utilities.M_Count when VAR_STAT is used: 

METHOD M_Count : BOOL
VAR_INPUT
	bStart : REFERENCE TO BOOL;
END_VAR
VAR_STAT
	nCounter : INT;
END_VAR
VAR_OUTPUT
	nCounterADR : ULINT;
END_VAR

 

You can see that nCounter variables in the function block instances of the method point to the same memory address since VAR_STAT is used. 

adr var stat

 

Now, let's start the first method and check the counter value in the second instance. 

Since the both counters point to the same memory address, even though we have executed the first instance, counter value in the second instance has also the same value. 

 

Reply
Share: