LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get/set a shared variable programmatically?

I've seen discussions about how to get access to a shared variable programmatically, but I'm having trouble filling in the blanks.  We are not using DSC or DataSockets.  Here's what I want to do: I want to write a VI that operates on a pair of network shared variables.  I have a number of shared variable pairs that I want to use my VI with.  The VI reads one shared variable (numeric), does some operations, and writes to another variable (boolean).  How do I write my VI so that it takes as an input references to the shared variables?  Can someone please give me an example?

Thanks,
Keith.

0 Kudos
Message 1 of 20
(5,233 Views)
Don't know if this will help you but here goes... the shared variables are linked via an IP address. You can "link" the SV by setting the "link to variable" property (as I think about this). If you want to do this programmatically, I would look at the examples included in LV8.
0 Kudos
Message 2 of 20
(5,220 Views)
You would only need to use DataSocket if you need to configure and host shared variables on non-Windows systems.

You should be able to use references to access shared variables. You just need to make sure you create a reference to a control that's been bound to a shared variable. See attached example project that you can try to play with. Note that there is a delay prior to checking the value of the shared variable. I have found that sometimes you cannot immediately access the value of a shared variable, as the engine hasn't updated the value.
0 Kudos
Message 3 of 20
(5,212 Views)
Thanks for putting together an example for me, but I get unresolved linkage errors.  You have 1 SV called "Variable1" but the VI refers to a different name.  But I'm still not sure the subVI is doing what I wanted.  It's taking a refnum that refers to a control on the front panel, not a shared variable.  If you modify your example to have 4 SVs: 2 numeric and 2 boolean, then you would have a use case like what I'm trying.

Thanks!
Keith.
0 Kudos
Message 4 of 20
(5,207 Views)
Actually, that was what I had. I have no idea why the shared variable library only got saved with one shared variable. Here's an updated ZIP file:
0 Kudos
Message 5 of 20
(5,204 Views)
Oh!  I get it!  You're not passing the SV by reference -- you're passing a reference to a control/indicator as a "proxy" because it happens to be bound to a SV.  But this only works if the SV has an associated control/indicator.  Is there a way to do this if this is not the case?

Thanks!

0 Kudos
Message 6 of 20
(5,198 Views)
You can do that as well, if you wish: How Can I Get a Reference to a Shared Variable?
0 Kudos
Message 7 of 20
(5,187 Views)
Ah -- this is what I was referring to for "connecting the dots."  I saw this answer before and tried it, but all it does is give me the name of the SV.  I already know the name -- I want to change the value.  I can't find a method on the variable reference for setting or getting the value.

0 Kudos
Message 8 of 20
(5,185 Views)
Yeah, that doesn't expose the "Value" property. I don't know of another way to access the value programmatically without using Datasockets, or having the DSC module. Perhaps someone more familiar with shared variables can either confirm, or offer an alternative.
0 Kudos
Message 9 of 20
(5,181 Views)
Here's a re-spin of what richjoh suggested, combined with smercurio's example.
 
I know you don't have DSC.  You don't need it.  But I didn't see a reason in this thread about not being able to use datasockets.  They are standard equipment in LabView, or am I mistaken?
 
Anyway, assuming that you DO have datasockets, here's what you do:
 
1.  Right-click on "Untitled Library1.lvlib" in the example.  Select "deploy".  This creates shared variables in the Shared Variable Engine.
 
2.  Create a new VI.  On the block diagram right-click and select "Data Communication" then "DataSocket" and choose "DataSocket Select URL.vi".  Nothing needs to be wired to this control other than creating an indicator at the output of the URL string. 
 
3.  When you run this VI, the DataSocket Select URL VI will prompt you  to navigate to your shared variables.  You should find "Untitled Library 1" on the list somewhere.  Expanding it should show you the 4 variables deployed.  Select one and click "ok".  The output string should show you "psp:\\localhost\Untitled Library 1\Boolean1" for example.  You use this URL structure for DataSocket reads and writes to have direct access to the shared variables hosted in the Shared Variable Engine.
 
4.  For further enlightenment, find "Variable Manager" from the National Instruments folder.  You should see "Untitled Library 1" there as well, with the 4 variables.  You can drag these variables to the right-hand window and view them as they change due to datasocket writes from your VI, or you can update them manually in this window, and see how your VI reacts to the changes.
 
I hope this helps.
Mike
0 Kudos
Message 10 of 20
(5,161 Views)