Leveraging the Power of Python in LabVIEW

Tags: LabVIEW

Python is becoming an increasingly popular tool for software development for a number of reasons:

  • Easy to Learn – The syntactic simplicity of Python allows for newcomers to get up-to-speed in a short amount of time.
  • Cross-Platform Compatibility – Python will not only run on Windows, but it will also run great on Linux and Mac machines.
  • Strong Community Backing – There is a large community supporting Python, and because of this there is a large array of Python libraries available for use that can provide a solution to virtually any problem.
  • Flexibility – When integrated with LabVIEW-based programs, Python can provide a significant level of flexibility due to its accessibility.

Many test systems rely on LabVIEW-based control and data acquisition systems, which can make integrating Python-based software a challenge. In this blog post, we’ll be reviewing different methods and tools for integrating Python into LabVIEW software.

Leveraging the Power of Python in LabVIEW Blog_Social

Inside-out vs. Outside-in

When it comes down to exactly how you integrate Python into your LabVIEW application, the tools available typically fit into one of two paradigms: Inside-out or Outside-in. The diagram below depicts the two different styles, but to put it simply, Inside-out is defined as LabVIEW instructing Python to perform an action and return a result, while Outside-in is defined as Python instructing LabVIEW to perform an action and return a result.

Inside-out Outside-in Diagram

The diagram above illustrates Inside-out and Outside-in integration methods

INSIDE-OUT (Using LabVIEW to call Python)

Inside-out methods of integration allow you to leverage Python resources from within your LabVIEW application, taking advantage of the built-in drivers and the familiar GUI that LabVIEW offers. In this setup, the Python code acts independently from the overall LabVIEW framework and because of this you are limited only to the commands executed by the LabVIEW program. The simplest example of Inside-out integration is using the LabVIEW System Exec.VI to launch Python. Information can be passed to Python through the standard input pipe or through arguments to the Python script. Results from Python then can be returned on the standard output pipe and error pipe.

System Exec.VI

System Exec.VI

While Inside-out integration can offer flexibility and customization to execute commands beyond LabVIEW, the time it takes to repeatedly call a System Exec.VI to launch the Python script from the command prompt and retrieve a response from Python can be cumbersome for time-sensitive operations.

LabVIEW Python Node

Another example of Inside-out integration is LabVIEW’s native Python Node. Python Node allows you to command a Python script from a native LabVIEW block diagram. This method is advantageous due to its low latency and support from LabVIEW.

Python Node VI

Python Node VI

OUTSIDE-IN (Using Python to Control LabVIEW)

The Outside-in method of integration leverages LabVIEW resources for Python operations. You can build any LabVIEW application into a Dynamic Linked Library (DLL) and access this DLL from Python. A DLL built in LabVIEW will expose the methods you have configured to Python. The example below demonstrates how you can call a DLL built in LabVIEW in Python. In contrast to Inside-out integration, LabVIEW’s visibility into the Python framework is limited.

DLL

Calling a LabVIEW DLL from Python

Another way to implement the Outside-in architecture is by using a RESTful Web API Service in LabVIEW. REST stands for Representational State Transfer – in most cases, it is an HTTP API for your application. You can call methods into LabVIEW by sending HTTP requests to the server and Python will execute these HTTP requests with relative ease, as is seen in the example below.

RESTful

Calling LabVIEW through a RESTful Web API Service

TestScript

A third option for integrating Python with LabVIEW is Genuen’s TestScript, which blends both Inside-out and Outside-in integration. TestScript is a free connector that allows for continuous communication between LabVIEW and Python within the larger framework of operation (such as within a test). Unlike either of the previously mentioned methods, TestScript provides you with a bigger picture view of available commands and resources. TestScript can also be operated Inside-out or Outside-in.

Summary

There are a variety of options out there for integrating either Python into your LabVIEW application or LabVIEW into your Python application. Each methodology has its advantages and disadvantages and the specific architecture you choose will depend on your needs. This blog post isn’t a comprehensive list of all the options, so do your research to see if there is something else out there that fits your needs. For more information about integrating Python into LabVIEW, visit our TestScript page or contact a Genuen engineer to discuss other software integration solutions.