How to make your LabVIEW code accessible from other languages using HTTP

In this article, I will be showing you how to make your LabVIEW code accessible to just about any other programming environment (Python, JavaScript, C/C++, you name it!).  We will accomplish this by creating an HTTP interface that provides “hooks” into your LabVIEW application.  Since just about every programming environment has an HTTP library of some sort, it will be able to interact with your LabVIEW program quite easily.  I’ve used this technique in the past to make my applications “scriptable” via Python scripts, but when you are using an interface as universal as HTTP, the options are truly endless.

Languages

A word of caution before we begin: since this technique uses HTTP you might be tempted to use this to make your LabVIEW applications accessible over the web.  However, On to establish the HTTP connection this example uses the .NET HTTPListener Class which is not recommended to be used to host a web server that is accessible over the open internet.  I suggest that you use this technique for localhost connections only, as doing otherwise could be a security risk and would leave your system vulnerable.

Download example code here:

HTTP LabVIEW Interface Example

In the example project (and in the image below) you will see that the HTTP interface code is set up to resemble the familiar LabVIEW “queued message handler” design pattern.

HTTP Example

The code starts by calling the “Start” VI, which initializes the localhost HTTP web server at the specified port.  The default port is 10000, but you can change it you have a port conflict.  Inside the loop, the example calls the “Listen” VI that waits for an incoming HTTP request body.  You are then free to parse the incoming request as you see fit.  In the example, I chose to use a JSON based message of this format:

{
  "State": "Add",
  "Data": {
    "Num1": 5,
    "Num2": 7
  }
}

The “State” field is parsed out and used to select the case of the case structure.  Once inside the case structure, the “Data” field is parsed and used to perform the requested function (in this case, simply adding the two provided numbers together).  Once the function is complete, the result is serialized into the following format:

{
  "State": "Add",
  "Result": 12
}

The response string is then passed to the “Send Response” VI which will send the JSON response method back to the client.  Sending a JSON message with the “State” set to “Stop” will stop the LabVIEW code.

A nice tool for testing this example is called Postman.  Once you download and install Postman, set the URL to http://127.0.0.1:10000 (unless you are using a different port, then use that post instead of 10000).  Change the HTTP Method dropdown from GET to POST and select the Body tab and change the type from Text to JSON (application/json).  Then simply type in your message and click Send.  You should receive the response in the text field at the bottom.

There you go, you’re now all ready to start interfacing your LabVIEW code with whatever environment you need!  Have any questions or comments?  Feel free to post to the comments section below.  Thanks for reading!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s