Technical Forum

STEP 7 / STEP 7 Lite

How functions works

Thread Starter: fury_omen   Started: 5/1/2012 5:20 PM   Replies: 22

« Previous thread Next thread »
123» Page 1 of 3 (23 items)
  5/1/2012, 5:20 PM
Joined 10/17/2010
Last visit: 10/22/2012
Posts: 16
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 

Hi folks….. I have question about functions in s300. Function are procedures that do not save data so I can use only temp variables in local data stack. So if I have function and I call it twice for example does cpu reserve different local data for the second instance of the function. Im asking that because I have this situation. I have function and in I set a bit (temp var)in it and on the next instance this bit is already set …. Is that mean that function safe the state of temp vars ?

Top
  5/1/2012, 6:11 PM
Joined 3/24/2009
Last visit: 5/21/2013
Posts: 372
Rating:
Rated: Outstanding [3.88 out of 5 / rated 58 time(s)]. (58) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 

Dear fury_omen,

you can not relay on a value in the local stack trace. The value may change from instance to instance or from cycle to cycle.

Best regards,
yy1
Top
Thank you for this post from:
  5/1/2012, 6:15 PM
Joined 3/15/2008
Last visit: 5/20/2013
Posts: 4434
Rating:
Rated: Outstanding [3.88 out of 5 / rated 409 time(s)]. (409) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
fury_omen wrote:

... I have function and in I set a bit (temp var)in it and on the next instance this bit is already set ….



Danger!

You are not allowed to READ a TEMP - Variable befor you WRITE it!

If you read it without writing, the status is NOT DEFINED!

Industrieautomation: Softwareerstellung und Inbetriebnahme weltweit.
http://www.ibn-service.com
Top
Thank you for this post from:
  5/1/2012, 6:18 PM
Joined 9/27/2005
Last visit: 5/8/2013
Posts: 934
Rating:
Rated: Above avarage [2.61 out of 5 / rated 36 time(s)]. (36) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Hi fury_omen!
You talk
about the global data
outside of a a function?



=== Edited by gre_m @ 5/1/2012 6:19 PM [GMT ] ===


Top
  5/1/2012, 6:26 PM
Joined 3/15/2008
Last visit: 5/20/2013
Posts: 4434
Rating:
Rated: Outstanding [3.88 out of 5 / rated 409 time(s)]. (409) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
gre_m wrote:
Hi fury_omen!
You talk
about the global data
outside of a a function?


fury_omen wrote:

... (temp var)  ...


Industrieautomation: Softwareerstellung und Inbetriebnahme weltweit.
http://www.ibn-service.com
Top
Thank you for this post from:
  5/1/2012, 7:03 PM
Joined 4/30/2007
Last visit: 5/9/2013
Posts: 185
Rating:
Rated: Excellent [4.13 out of 5 / rated 15 time(s)]. (15) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 

Hello fury_omen

For FC’s you cannot use TEMP’s to store data, the option for you will be FB’s with Instance DB, where you can declare STAT  statics variables.

The following link 
Difference ...   explains weell the differences between FC's and FB's
Regards
Marciano

Top
Thank you for this post from:
  5/1/2012, 8:01 PM
Joined 10/17/2010
Last visit: 10/22/2012
Posts: 16
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
I dont want to save data by fc. I just want to know waht happed with temp vars if i have multiple instance of one function.

Let look that example. I have two function. The first ‘fc4’ called the second ‘fc3’ twice. In ‘fc3’ I have set coil ‘temp1’. The output of ‘fc3’ depends on two inputs var ‘in1’ and ‘in2’ and local var ‘temp1’. On second instance of function ‘fc3’ I have only to set ‘in1’ and the output ‘pass’ is on. The ‘temp1’ var is set in the second instance on function ‘fc3’. Is what mean that local data is saved? I just copied these function in STL just past them in stap7 and turn the view to LAD and test it will PLCSim. So my question is if I set tamp var  in function how to know what happen with this var in second instance of this function is it will be clear, set or undefined   

 

fc 3

in: in1 – bool; in2 – bool;

out: out1 – bool

temp: temp1 – bool

 

A     #in1

S     #temp1

A     #in2

A     #temp1

=     #out1

 

fc4

in: n1 - bool; n2 - bool

out: pass – bool

temp:  t1 – bool; t2 – bool

A     M     10.1

=     L      1.0

BLD   103

A     M     10.2

=     L      1.1

BLD   103

A     #n1

JNB   _001

CALL  FC     3

in1 :=L1.0

in2 :=L1.1

out1:=#t1

_001: NOP   0

A     M     10.3

=     L      1.0

BLD   103

A     M     10.4

=     L      1.1

BLD   103

A     #n2

JNB   _002

CALL  FC     3

in1 :=L1.0

in2 :=L1.1

out1:=#t2

_002: NOP   0

A     #t1

AN    #t2

=     #pass

 

ob1

 

A     M     20.0

=     L     21.0

BLD   103

A     M     20.1

=     L     21.1

BLD   103

A     M      1.0

JNB   _001

CALL  FC     4

n1  :=L21.0

n2  :=L21.1

pass:=M30.0

_001: NOP   0

Top
  5/1/2012, 10:05 PM
Joined 4/30/2007
Last visit: 5/9/2013
Posts: 185
Rating:
Rated: Excellent [4.13 out of 5 / rated 15 time(s)]. (15) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
On second instance of function ‘fc3’ I have only to set ‘in1’ and the output ‘pass’ is on.
Here I believe you mean set the 'In2' since the second input is a direct condition for the output

You should see the TEMP as a sketch or draft , temporarily variables valid inside the block only.
Now answering your question, they are not cleared, actually they ” keep the state”., and not officially if your program have only one OB and one FC you could have your TEMP’s holding values… However because this area is shared with other blocks with same priority, the TEMP area will be constantly been updated /several updates during one single scan – so I think you can say the result would be undefined.

Top
Thank you for this post from:
  5/1/2012, 11:05 PM
Joined 10/17/2010
Last visit: 10/22/2012
Posts: 16
Rating:
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 

Yep, my mistake is ‘in2’.

So in one OB, I do NOT have fresh instance on same function if I call it more than ones. And this happed because the local date stack is not cleared after end of function.  If I want everything to be fresh I have to clear all set bit and so on before exit from function. Is that right ?  

Top
  5/2/2012, 5:29 AM
Joined 10/31/2005
Last visit: 5/22/2013
Posts: 2092
Rating:
Rated: Excellent [4.08 out of 5 / rated 200 time(s)]. (200) 
Users with special expertise supervising conferences
Rated: no ratings [0 out of 5 / rated 0 time(s)]. (0) 
Well, you should just use general rule - TEMP variables are written FIRST and read AFTERcool
Top
Thank you for this post from:
123» Page 1 of 3 (23 items)
Actions