10-11-2013 06:25 AM
Hopefully someone out there has some insight into an odd problem I have come across with global variables and VIs in projects. The problem is somewhat tricky to contextualise so please bear with me while I attempt to explain it clearly.
I have written a number of VIs that act in a subordinate manner to some VIs that were supplied with an instrument that we purchased. The Manufactureres VIs can't really be played with, without fear of messing up the functionality of the instrument that they control. The manufacturers VIs contain global variables that provide system information. My VIs access these global variables and perform various functions when the right conditions are met. My VIs cannot influence the Manufacturers VIs, mine are merely watching the global variables (mainly arrays) for the right values. My VIs have been organised in a single project (in an attempt to keep things neat and ordered). This imperfect set-up was working fine until recently. So with the context provided, the problem:
It seems that if I launch my VIs through the project, the data from these global variables are coming back as null. If I find the VIs directly through the hard-disk folder structure and launch it, the globals are accessed fine and return the expected data. If I launch the VI through the project, close the VI (not project), then try to launch the VI from the hard-disk then it also does not work. Basically if the VI has been opened through the project and the project remains open then the VI will not work regardless of how I access it. If the project is open, and the VI not launched through it but through the hard-disk, then all is fine. I hope that is clear.
The set-up has been working fine for months. The only thing that might have changed things recently is that I have started looking into building executables (so that I don't have to give access to the VIs directly to our students) - the executable also gives the same problem. At the moment this is non-critical (as I can successfully run the VIs by launching from the hard-disk folder), but it would be nice to get to the bottom of it so that I can create executables without this issue.
I anyone out there has any thoughts, they would be most welcome.
10-11-2013 06:44 AM
Although didnt read the question but I also faced same problem earlier. Replace global with local. This solved mine
10-11-2013 07:01 AM
Even though it's called global variable, these are only valid within a certain scope or context. You can have 2 instances of the same global variable, once within the project, once outside, and they don't interfere with each other, they are running in independent application scopes and thus have independent values.
So you always have to be careful in which context you use global variables and run your VIs.
Either have the manufacturer VIs run within the context of your project as well (recommended) or have everything running in the Main application instance. Btw, in the lower left corner of a VI you can switch the context it runs in (unless it is in the main application instance).
10-11-2013 07:01 AM
Thanks for the advice Ranjeet, but I'm a little confused. As I explained the globals are part of a software suite that I have little ability to control - they are not part of my VIs. Given this, I thought that I couldn't change the globals to locals (as far as my understanding goes locals have to be defined within your VI). If I have misunderstood please let me know. Thanks again for your input.
10-11-2013 07:14 AM
Thanks Dan. That seems to explain where the problem is rooted. I guess I'll have to think about what the most practical solution will be - I can't run the Manufacturer's VIs under my project, but I could potentially move mine out of it's project and into their structure (it's a mess of projects, libraries and random files), somewhere.
10-11-2013 07:20 AM
What you could try is to access the gloabal variable through VI server. Open an application reference to the manufacturer project and access the global variable through this.
10-11-2013 07:25 AM
No What I mean is where you writing to global variable there you update local variable and use to all the subVIs
10-11-2013 07:26 AM
@p.desai wrote:
I can't run the Manufacturer's VIs under my project
Why not? You can run multiple VIs in the same project. And they will all have the same application context as well.
What are you going to do when you build this project into an executable? Are you going to have a single EXE or multiple? If you have to use multiple, then global variables won't work.
10-11-2013 11:54 AM
@Ranjeet_Singh wrote:
Although didnt read the question but I also faced same problem earlier. Replace global with local. This solved mine
Although I didn't read your response, I'm sure you're wrong.
10-11-2013 12:18 PM
A local variable is called "local" because its scope is local to the vi that it is in, and it is directly connected to a control/indicator in that vi. It won't work with sub-vi's or more importantly, vi's running in parallel to the vi they exist in.