LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Attempting to initialize global variable

Solved!
Go to solution

I am a beginner to LabView and am trying edit two global variables based on collected digital inputs. However when I run the program the values from the previous execution are what is output. I've tried inserting a flat sequence structure and writing a constant to the variables first, but it doesn't seem to execute. Thanks for any help you can give.

0 Kudos
Message 1 of 5
(522 Views)
Solution
Accepted by edawgie

Hi edawgie,

 

You are not using global variables, you are using Network Published Shared Variables.

Those variables are shared over the network through the NI Shared Variable Engine. A consequence compared to plain global variables is that the value you obtain from the Shared Variable Node may take a little time to reflect the last value written to it, typically causing race conditions like you have in your code.

 

A solution would be to only write to the Shared Variable (not read), and have a local copy of the value in a Shift Register:

raphschru_1-1718901494875.png

 

Regards,

Raphaël.

0 Kudos
Message 2 of 5
(497 Views)

I hate Network Published Shared Variables.  They are slow (requires a timeout, writing to a database, and then waiting for the update) and insecure (anybody on the network can write to them).  There are so many things that can go wrong and you have little to no readability into anything if there is an issue.  NPSVs are actually evil (this coming from the guy who gave an entire presentation on why Global Variables are not evil).

 

When communicating with cRIOs, I just use raw TCP.  I know many like Network Streams.  Both of these options are a lot better than NPSVs.  Write a main application for the cRIO that can run your two subroutines when desired and have it stream the data back to the PC.  You PC application can then read the data, parse it, and do whatever else is required from there.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 5
(491 Views)

@edawgie wrote:

I am a beginner to LabView 


Are you writing this yourself, or is this something you've inherited?

 

For a beginner, my advice would be to avoid global variables altogether. They break data flow and can easily read to race conditions. There are almost always better ways.

 

On top of that, do you need to publish all of these values to the network? If not, you should not be using Shared Variables. That's not the same thing as a global variable.

0 Kudos
Message 4 of 5
(485 Views)

Thank you! I did not know the difference between such variables. I will do a deeper dive into those to learn more.

 

Everett

0 Kudos
Message 5 of 5
(474 Views)