LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Passing Data to Python and Receiving Back Outcome from Python in Real Time

Solved!
Go to solution

Hello, 

 

I am trying to pass three data points from instruments in LabVIEW within a while loop to a Python script which will compute one output, and pass that output back to LabVIEW in real time. Since it is in a loop, I need this to be as efficient as possible. 

 

I have seen a lot about LabPython, but it seems this is not being maintained for newer versions of Python. I also have seen some recommendations of passing the data out of LabVIEW to a .csv file, reading the data into the Python script from the .csv file, calling the Python script in LabVIEW, writing the output from Python to another .csv file, and then reading this output from the .csv file back into LabVIEW. This method seems very complicated and inefficient. 

 

I have tried using the Python node in LabVIEW, and have it wired to accept the three channel inputs and spit out a single output, but cannot find any resources on how to handle the data on the Python side. The current Python script I'm using is set up to read data in from an input.csv and output to an output.csv for post-processing of data, but this isn't useful to me. 

 

Thanks for the help in advance!

0 Kudos
Message 1 of 10
(10,600 Views)

Hello,

 

The following KB might help get you started with a small snippet of python script. 

https://forums.ni.com/t5/LabVIEW/Passing-Data-to-Python-and-Receiving-Back-Outcome-from-Python-in/m-...

Malkolm A.
Applications Engineer
National Instruments
0 Kudos
Message 2 of 10
(10,544 Views)

Malkolm, 

 

The link you sent is the link to the forum I started. I have also looked around NI forums and have yet to find anything helpful.

 

Zach

0 Kudos
Message 3 of 10
(10,516 Views)

Hi Zach,

Here's a link with a bunch of information about getting started with Python in LabVIEW Hopefully it helps! 
http://docs.enthought.com/python-for-LabVIEW/guide/start.html#guide-start

Here's the knowledgebase article that linked there for your reference:
https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z0000019WHGSA2&l=en-US

Cheers,
Nick

0 Kudos
Message 4 of 10
(10,499 Views)

Nick, 

 

Thanks! I had not seen the Enthought integration toolkit for Python before so that was interesting, but it still doesn't go into detail with Python code. I'm using a Python Node in LabVIEW so that is all set up, but I don't know how to take in the channels from LabVIEW in my Python script. I can send the data out to Python and receive it back from Python, but I do not know how to set up the Python code to accept the channel inputs from LabVIEW and then send the output to LabVIEW. Any experience with this? I have attached the part of my block diagram where I pass the data from four channels out to a Python script and receive one output back for reference. 

 

 

0 Kudos
Message 5 of 10
(10,484 Views)

Hi Zach,

What do you mean when you say you can't take in the channels from LabVIEW in your python script? A little more context will help!
This doc shows the data types that the python node can handle:
https://zone.ni.com/reference/en-XX/help/371361R-01/glang/python_node/

Cheers,

Nick

0 Kudos
Message 6 of 10
(10,461 Views)

Nick, 

 

Sorry for the lack of context. I am new to LabVIEW and even newer to Python so my issue here is more with coding in Python. I'm not sure how to code the python script to read in three individual channel values (One RTD temperature value and two pressure values). A coworker of mine wrote the code to accept data in from a simple data file instead of directly reading the measured values from LabVIEW. If I had to write the one temperature and two pressure values to an input text file, read these into Python from the input text file, write the result back to an output text file, and read the result back into LabVIEW from the output text file each iteration of my while loop, my LabVIEW control method would be highly inefficient. I'm trying to find a more efficient way of passing the three measurements to Python and receiving back the result. You can see the three channels I have wired into the Python Node as inputs in the code snippet from my last message. There has to be a way to read in values from specific instrumentation channels on the DAQ to Python, but I simply don't know how. Does that help you at all?

 

Best, 

 

Zach

0 Kudos
Message 7 of 10
(10,454 Views)

My understanding is that you just use input parameters to pass data to the python node. the same way that you would read data from any channel in LabView but just send it to python instead of an indicator.

Alternatively this may be another way to go about it
https://stackoverflow.com/questions/6596974/send-data-from-labview-to-python-and-get-back

Cheers,
Nick

Message 8 of 10
(10,419 Views)
Solution
Accepted by topic author zhale

Here's an example that might help you get started.

The example only adds two numbers, but should show the basic idea.

 

Before this would run on my computer, I installed Python 3.6.8 (32-bit version, to match the LabVIEW version I used) from here: python-368.

The code in the python file is simply:

def MyFunction_Add(num1, num2):
    return num1+num2

with the LabVIEW code looking like this:

pySnippet.png

 


GCentral
Message 9 of 10
(10,407 Views)

cbutcher, 

 

Thank you! This is perfect to help get me started. Thanks for taking the time to put this together.

 

Zach 

0 Kudos
Message 10 of 10
(10,389 Views)