Advanced Datatypes with Python Node in LabVIEW
A recent LinkedIn post on using Python and LabVIEW got me thinking about passing more advanced datatypes between LabVIEW and Python. I have written about the Python node before, but I hadn’t really played with advanced datatypes before, so I thought I would play around and come up with a demo.
Here is the python script I wrote. It is pretty simple. All the code can be found at https://gitlab.com/sas-blog/python-adv-datatypes The code is in LV2020 and requires Python 3.6.8
Multiple Return Values
One of the tricks with the Python node is returning more than one value. LabVIEW only let’s you have one return value. However that one value can be a tuple which translates to a LabVIEW cluster and that cluster can be arbitrarily complex. It can contain nested clusters and arrays (Python lists – as long as all elements are the same datatype).
Passing Maps
Python uses dictionaries very heavily. As of 2019 LabVIEW now has maps. However you can’t just pass a LabVIEW map into Python as dictionary, or return a Python dictionary to LabVIEW as a Map. At least not directly. The trick is to pass the keys and values as separate arrays or lists and then reconstruct the dictionary or map on either side. Now there is still the caveat that for Maps in LabVIEW all the keys must be of the same type and all the values must be of the same type. So that still holds.
Array Special Case
You may have noticed that the Python code has a corresponding output for each input. In general these just pass out the same value that was passed in. The exception to this is arrays. Python passes lists (~ the equivalent of arrays) by-reference. So any changes you make to an array in Python are reflected in the output array. This works even inside clusters.
Need help integrating Python into your LabVIEW or TestStand project? We can help. Let’s schedule a call to discuss.