LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

control refnum vs global variable - best programming practice?

Hi all,
    I want to use a subVI-1 to set a bunch of parameters which will be stored in some sort of variable.  I then want to use those parameters in a different subVI-2 that will only read them (no writing).

One obvious way to do this is to use global vars where I can write in subVI-1 and read in subVI-2., but I'm trying to do this better, so I've read about control refnums, and I'm not sure about whether it's any better.

I was thinking that I could put the controls in the mainVI and hide them from the UI, and then when subVI-1 is called, it has a duplicate of the controls in mainVI (maybe I could use a typedef and a custom control?? -- or maybe a property node??), when subVI-1 is finished, it will update the control in mainVI.   then when subVI-2 is called, the updated controls can be passed to it from mainVI. 

this way I avoid using globals, but I'm not sure why it really makes any difference if I'm breaking the dataflow anyway???

can someone please clarify?  I've read a lot of the other threads about control refnums, but I still can't get it clear in my head!
thanks
-z 
0 Kudos
Message 1 of 8
(4,067 Views)


    I want to use a subVI-1 to set a bunch of parameters which will be stored in some sort of variable.  I then want to use those parameters in a different subVI-2 that will only read them (no writing).
....
I was thinking that I could put the controls in the mainVI and hide them from the UI, and then when subVI-1 is called, it has a duplicate of the controls in mainVI (maybe I could use a typedef and a custom control?? -- or maybe a property node??), when subVI-1 is finished, it will update the control in mainVI.   then when subVI-2 is called, the updated controls can be passed to it from mainVI. 


Sorry, maybe I didn' get you point... but why don't you just uses wires to pass the data from subVI1 to subVI2 ??
0 Kudos
Message 2 of 8
(4,058 Views)
oops, sorry - thats not obvious- or maybe i'm overthinking it!... - because i want the parameters to be retained for the duration of mainVI in case subVI2 is used more than one time. (i.e.-the user repeats the experiment)

does that help?

-Z
0 Kudos
Message 3 of 8
(4,054 Views)
Use shift registers in the main VI to hold your parametes and then wire the data into your subVIs.  That way, it does not matter how many times you call the sub VIs.  Also, control references and property nodes are slower to access and set data then variables or wires are.  The quickest method is to use the wire itself.  Usign variables increases the need for memory allocation. 
0 Kudos
Message 4 of 8
(4,037 Views)


@zskillz wrote:
oops, sorry - thats not obvious- or maybe i'm overthinking it!... - because i want the parameters to be retained for the duration of mainVI in case subVI2 is used more than one time. (i.e.-the user repeats the experiment)

does that help?

-Z


like nate said already, shift registers are a way to go. you use them basically as a buffer for the various runs of your subVI1/subVI2 sequences. see attachment for a possible simple solution
0 Kudos
Message 5 of 8
(4,035 Views)
ok - i was definitely making this more complicated then I need to, but I still have some questions...

subVI1 creates a cluster full of controls.  I then return this cluster to mainVI and stick it into a shift register...

I want to subVI2 to take that cluster as input.  in the block diagram of subVI2, I want to unbundle the controls in the cluster and use them as inputs into a variety of other subSubVIs.  The problem that I'm having is that I don't know how to get the control cluster into the block diagram of subVI2 so that I can do the stuff I just said I want to do.  The only way that I can think of doing this is to copy the cluster from subVI1 and paste it into subVI2, then to just make it invisible on the front panel of subVI2, but this seems like a really kludgy way of doing this....

can you guys help me out with this one.  Obviously, I'm still not totally understanding how some of this stuff works.

thanks
-z
0 Kudos
Message 6 of 8
(4,018 Views)

subVI1 creates a cluster full of controls.  I then return this cluster to mainVI and stick it into a shift register...

I want to subVI2 to take that cluster as input.  in the block diagram of subVI2, I want to unbundle the controls in the cluster and use them as inputs into a variety of other subSubVIs.  The problem that I'm having is that I don't know how to get the control cluster into the block diagram of subVI2 so that I can do the stuff I just said I want to do.  The only way that I can think of doing this is to copy the cluster from subVI1 and paste it into subVI2, then to just make it invisible on the front panel of subVI2, but this seems like a really kludgy way of doing this....

ok, if you want to pass the data cluster into subVI2, you need a control of that type on the front panerl (FP) of sub2. Copy and paste is one option, but you can also define a cluster of this type as a new (custom) type in labview, this is called a type definition, or typedef in labview jargon. You might look in the manuals or the help system to find out how to deal with them. Typedefs are most useful if you use a specific custom type over and over in your program. But Copy and Paste is also fine to start with.

But there is no way around having a control of that type on the FP of sub2 when the data are passed in with wires. It is not kludgy but the regular way of doing things in labview. Is the front panel of sub2 open when it's running in your application? If not, there is no need to make the cluster invisible. Even if it is open, you might want to have it visible in order to check that corretc data are passed. But normally most subVIs are invisible during execution, they behave like any non-user-interface subroutine in any language.
If you have the FP open, you can also move the control outside the visible part of the window to make it invisible.

Within sub2 you then proceed by unbundling  and feeding the components to the subsubs.

BTW, bundling just for the purpose of reducing the number of wires or FP controls is often not the best idea. The strcuture of the data should primarily be determined by the meaning of your data. So you might want to feed the data into sub2 by using various controls instead of grouping them in one big cluster. Of course there is some upper limit of what is preactical.Usually I prefer to have not more than 6...8 controls which I feed into a subVI.
Message 7 of 8
(4,011 Views)


@alkazaa wrote:

ok, if you want to pass the data cluster into subVI2, you need a control of that type on the front panerl (FP) of sub2. Copy and paste is one option, but you can also define a cluster of this type as a new (custom) type in labview, this is called a type definition, or typedef in labview jargon. You might look in the manuals or the help system to find out how to deal with them. Typedefs are most useful if you use a specific custom type over and over in your program. But Copy and Paste is also fine to start with.

But there is no way around having a control of that type on the FP of sub2 when the data are passed in with wires. It is not kludgy but the regular way of doing things in labview. Is the front panel of sub2 open when it's running in your application? If not, there is no need to make the cluster invisible. Even if it is open, you might want to have it visible in order to check that corretc data are passed. But normally most subVIs are invisible during execution, they behave like any non-user-interface subroutine in any language.
If you have the FP open, you can also move the control outside the visible part of the window to make it invisible.

Within sub2 you then proceed by unbundling  and feeding the components to the subsubs.

BTW, bundling just for the purpose of reducing the number of wires or FP controls is often not the best idea. The strcuture of the data should primarily be determined by the meaning of your data. So you might want to feed the data into sub2 by using various controls instead of grouping them in one big cluster. Of course there is some upper limit of what is preactical.Usually I prefer to have not more than 6...8 controls which I feed into a subVI.

well - I feel much better about the way I was doing it then.  I did copy/paste, but I ended up just using a strict typedef of the cluster.

also - The controls are all of the settings for a scope.  there's appx 20 of them which are set in subVI1 and It's just easier to move that cluster as one wire to subVI2 then to move ~20 separate wires (it also makes for a lot fewer shift registers Smiley Wink)... I understand that bundling/unbundling takes cpu time, but this control is passed very infrequently, so I'm not worried about the overhead on that. 

thanks for all your help!
-Z

Message Edited by zskillz on 10-22-2006 11:57 PM

Message Edited by zskillz on 10-22-2006 11:57 PM

0 Kudos
Message 8 of 8
(3,978 Views)