该内容不适用于您选择的语言

该内容将以另一种语言显示出来。 您可使用浏览器的翻译功能查看内容。

Passing Python Data Structures To/From LabVIEW with the Python Node

Updated Oct 23, 2023

Environment

Software

  • LabVIEW

Programming Language

  • Python

This article will show how complex data structures such as Tuples/clusters, Arrays/lists etc. can be set up to allow communication between LabVIEW and Python environments using the LabVIEW Python Node. This will allow LabVIEW and Python to inter-communicate, allowing you to use the best features of both languages.

This article will use LabVIEW to read Python data. Note that writing this data from LabVIEW to Python has the same principles but in the other direction.

Note that standard datatypes such as unsigned or signed numerics, strings, booleans are automatically translated by the Python node.

If using LabVIEW Arrays and Python Lists:

The Python Node will automatically translate any LabVIEW Array of a supported Python datatype into a Python List of that datatype, when given the correct datatype as an input.

The following Python code:​​​​​​

TestList = [True, True, False]
def return_list():
    x = TestList
    return x

will return an array of Booleans in LabVIEW:
LabVIEW_OEYlyBO8LN.png

If using LabVIEW Clusters and Python Tuples:

The Python Node will translate Clusters or Tuples of supported datatypes.
The following Python code in conjunction with the above definition of TestList:

TestTuple = (3, 5, "TestList")
def return_tuple():
    x = [TestTuple, TestTuple]
    return x

will return the following LabVIEW cluster when given the correct datatype:
LabVIEW_NjfD5Kodg7.png
This will also work with named tuples.

 

If working with Python Dictionaries and LabVIEW clusters with named pairs:

There is no native translation for a Python Dictionary item, which are stored in key:value pairs.
Instead, a JSON string should be used for this communication. The following Python code:

​​​​
import json
TestDict = {
    "String": "Test",
    "Number": 2,
    "Other number": 3
}

def return_dict():
    x = json.dumps(TestDict)  
    return x

can be read through LabVIEW as follows:
LabVIEW_MYlij8HFjF.png

The naming of the variables inside the LabVIEW cluster is important.

Full Code

A full snippet of the above LabVIEW code can be found below:
Python Call Snippet.png

Similarly, a full Python script for this article can be found below:

import json ##Required for the dict conversion

TestList = [True, True, False]
def return_list():
    x = TestList
    return x
##
TestTuple = (3, 5, "TestList")
def return_tuple():
    x = [TestTuple, TestTuple]
    return x
##
TestDict = {
    "String": "Test",
    "Number": 2,
    "Other number": 3
}

def return_dict():
    x = json.dumps(TestDict) 
    return x