Sign in to follow this  
Followers 0
ct8356

Function blocks: create new instance, or reuse the same instance?

8 posts in this topic

In my program, I have about 100 rungs for 100 faults. Each rung looks very similar. I thought I could make the code more maintainable by using function blocks, to avoid having to change each rung individually (if I needed to). Would it be best practice to create a new instance for each fault, or to reuse the same instance for each fault? I come from a C and java background, so I am confused as to whether a function block is supposed to be like a function (in C), or supposed to be like a class (in Java), or neither...

Share this post


Link to post
Share on other sites
If you reuse the values from function block then you should use new instance for every call (that would be e.g. if timer is used inside the block, if the same instance is called twice timer would not work properly, if it is called only once, which is the case when you use multiple instances, timer would work correctly), and if you do not (e.g. if you just have output = input * 23) you can use the same instance in every call.
1 person likes this

Share this post


Link to post
Share on other sites
Every call of a function block should always have its own instance name, never use the names more than once. As tashinz said, if there is no need for data to be stored inside the function block, it is considered legal to use the same instance name. But it's typically a bad practice to get into, since the one time you do need to maintain data a second function block of the same instance name will overwrite it.

Share this post


Link to post
Share on other sites
Yeah Crossbow, I totally agree, but sometimes you need to save program resources in which case one would use same fb instance wherever it is possible. regards to all

Share this post


Link to post
Share on other sites
I dont know if it could apply to your application, but you could use 1 instance and indirect adressing for inputs and outputs of your FB. I did it for a FB that was used to transfer PID settings for 32 zones on DeviceNet. So with 1 instance I was able to transfer all 96 (and non consecutive) words.
1 person likes this

Share this post


Link to post
Share on other sites
You can perfeclty use multiple instances using the same name. Just be sure that the FB can be processed in 1 scan, so no timers/counters/PID loops. A simple mathematic scale FB for instance with some underrange/overrange detection can be used as much as you would like using the same instance name. It sure saves a whole lot of FB memory...

Share this post


Link to post
Share on other sites
PdL, if there is no internal data like timers or counters it should be created as a function not a function block. Then no instance names are required. Maybe its different in CX Programmer, but in NJ I can create functions and not need instance names. Same with the Mitsu stuff I worked with in the past.

Share this post


Link to post
Share on other sites
It is different in CX-Programmer. You cannot create Functions in CX-Programmer. Creating a FB and then using the same instance name over and over in CX-Programmer achieves the same result as creating a Function in Sysmac Studio.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0