LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Continuosly update variabe in Python node?

Is it possible to get an updated value of LabView variable from inside of a python script?

 

For example, I have a python node running a loop, and I also have a sub-VI that logs a signal continuously. And I want to have access to the most recent signal value in my python loop's iterations. Maybe have a python variable that updates continuously, I am not sure how to approach this. Or is it even possible.

 

Writing to a file is not an option here due to high freq.

 

I am using native Python module (Open Python session, Python Node, Close Python session).

0 Kudos
Message 1 of 7
(1,727 Views)

@Koval84 wrote:

Is it possible to get an updated value of LabView variable from inside of a python script?

 

For example, I have a python node running a loop, and I also have a sub-VI that logs a signal continuously. And I want to have access to the most recent signal value in my python loop's iterations. Maybe have a python variable that updates continuously, I am not sure how to approach this. Or is it even possible.

 

Writing to a file is not an option here due to high freq.

 

I am using native Python module (Open Python session, Python Node, Close Python session).


When the application is a high performance (high logging rate), a mix of LabVIEW and Python is not the preferred use. Instead, do everything either in LabVIEW or in Python.

Santhosh
Soliton Technologies

New to the forum? Please read community guidelines and how to ask smart questions

Only two ways to appreciate someone who spent their free time to reply/answer your question - give them Kudos or mark their reply as the answer/solution.

Finding it hard to source NI hardware? Try NI Trading Post
0 Kudos
Message 2 of 7
(1,712 Views)

I don't need to process the signal at the same frequency it's being logged though, just be able to get the last value it got atm. In fact, I only mentioned the speed bc it makes it impossible to communicate through a file.

0 Kudos
Message 3 of 7
(1,707 Views)

@Koval84 wrote:

 

For example, I have a python node running a loop


Running a loop or running in a loop? In other words, is the loop in the Python code or in the LabVIEW code?

 

Also what kind of loop?

0 Kudos
Message 4 of 7
(1,687 Views)

@Koval84 wrote:

Is it possible to get an updated value of LabView variable from inside of a python script?

 

For example, I have a python node running a loop, and I also have a sub-VI that logs a signal continuously. And I want to have access to the most recent signal value in my python loop's iterations. Maybe have a python variable that updates continuously, I am not sure how to approach this. Or is it even possible.

 

Writing to a file is not an option here due to high freq.

 

I am using native Python module (Open Python session, Python Node, Close Python session).


Best bet is to decide on a form of inter process communication if you want to have two applications interact with each other. If you are just writing the apps now, as mentioned, pick labview or python but not both. If using both apps is a requirement, check out pipes, rabbitMQ, or TCP/IP, either way it it will be some work to get it going. 

______________________________________________________________
Have a pleasant day and be sure to learn Python for success and prosperity.
0 Kudos
Message 5 of 7
(1,674 Views)

@JimB. wrote:

@Koval84 wrote:

 

For example, I have a python node running a loop


Running a loop or running in a loop? In other words, is the loop in the Python code or in the LabVIEW code?

 

Also what kind of loop?


Loop in the Python code. Any loop. A simple "for" or a comprehension list, doesn't matter. Something that iterates and uses the latest value of a signal.

Another example is minimization function.

 

The point is to get a value from LabView to Python script that is running already.

0 Kudos
Message 6 of 7
(1,672 Views)

@Koval84 wrote:

@JimB. wrote:

@Koval84 wrote:

 

For example, I have a python node running a loop


Running a loop or running in a loop? In other words, is the loop in the Python code or in the LabVIEW code?

 

Also what kind of loop?


Loop in the Python code. Any loop. A simple "for" or a comprehension list, doesn't matter. Something that iterates and uses the latest value of a signal.

Another example is minimization function.

 

The point is to get a value from LabView to Python script that is running already.


Well, there could be different approaches but here's one example.

 

  • The Python module defines the following:
    • startloop() : This function starts _myloop in its own thread.
    • stoploop() : This function signals the data processing thread to stop and returns after it does.
    • setlatestvalue() : This function sets the value of a global variable. This is the "latest value of your signal".
    • getdatalist() : In this example our "processing loop" is just adding the latest value to a list. This function returns it so we can display it as an array in LabVIEW.
  • LabVIEW does the following:
    • Calls startloop().
    • Waits before starting data acquisition to demonstrate the "No value present" behavior if setlatestvalue() has not been called yet.
    • "Acquires data" in a loop and calls setlatestvalue() to update the value in the Python module.
    • Calls stoploop() after the data acquisition is done. Note that even if you are using a For loop or other set number of iterations, it's still probably a good idea to check that the thread has terminated at some point. In this case it's very important that you call this or you'll notice that niPythonHost never exits. (And you'll get no error or other indication from the Close Python Session VI.)
    • Calls getdatalist() to retrieve the results of our "data processing".

 

Should you do it like this? Probably not.

 

This is only a very simple implementation and probably has a lot of unsafe stuff I'm not even thinking about. Off the top of my head, a couple things I was too lazy to work out for this example:

 

  • The is no consideration for thread safety on any of the global variables. There are no read-modify-write operations in this example and I believe it should work reliably, but you should be aware of these possible issues that crop up any time you share variable access if you expand on this.
  • You can call startloop() all you want and keep creating new threads. You'll also lose track of them since each call will overwrite the _loopthread variable. That should definitely be changed if you go down this road.
  • I'm not sure if there is a way to better protect the _myloop function, but that should never be called outside of startloop(). If you call that in a Python Node it will never return and you'll be stuck in an infinite loop. (I think there might be a way to better protect it by encapsulating all this in a class, but I don't really intend to play with this much more.)
Download All
0 Kudos
Message 7 of 7
(1,647 Views)