ZeroMQ in LabVIEW
It may not surprise you, that communication also plays an important role in software engineering. No, I am not talking about communication skills, but I am referring to the communication of software.
A software application often requires that one piece of code can communicate with another piece of code. When both pieces of code are written in the same programming language, you do not need to worry too much about e.g. the underlying communication protocols. You “just” need to implement the right code.
But this becomes a different story when the pieces of code are written in different programming languages. How does e.g. LabVIEW code know how to communicate with Python code?
In that case, you need a messaging library that is supported by both programming languages, like Zero MQ.
What is ZeroMQ?
Zero MQ, also written as ØMQ, 0MQ, or zmq, is an open-source and free asynchronous messaging library that enables you to send messages over a network and between processes.
It supports different message types (e.g. binary data, serialized data, or simple strings), different communication protocols (e.g. TCP, multicast, or WebSocket), and different messaging patterns (e.g. publish/subscribe, request/reply, client/server).
Zero MQ provides different kinds of sockets, that are related to the supported messaging patterns, and a message queue (MQ). Note, however, that it does not require a message broker.
In addition, it is also known for its high performance and there are several APIs available for using Zero MQ in multiple programming languages (e.g. C++, C#, and Python).
But what about LabVIEW? Is it possible to use ZeroMQ in LabVIEW, too?
Can ZeroMQ be used in LabVIEW?
Well, LabVIEW does not include native functions for ZeroMQ. But the ZeroMQ Socket Library or LVZMQ developed by Martijn Jasperse provides a library for using ZeroMQ in LabVIEW.
This library is available for LabVIEW 2010 and newer (considered to be stable since 2016), and it is available for 32-and 64-bit versions of Windows and Linux. The included VIs are wrappers for ZeroMQ’s C interface.
But how to actually use this library?
Let’s take a look at a simple example
Suppose that you would like to build a simple request/reply application that includes one server and one client. The client should send a request to the server by sending a string similar to “Client: Can you send me some data?”. In return, the server should reply by sending the string similar to “Server: Here comes the data.”
One possible implementation of the LabVIEW client looks as follows:
And one possible implementation of the LabVIEW server looks as follows:
After running the server and the client, you will get the requested outcome. As a side note: Take care that you run the server before running the client. Otherwise, it will not work.
So, Zero MQ seems to work for LabVIEW-to-LabVIEW communication. But what about the communication between code pieces that were written in different programming languages, like LabVIEW and Python?
How to implement Zero MQ for LabVIEW-Python communication?
Let’s take the above example of the request/reply application and extend it considering the communication between LabVIEW and Python. In order to do that, you need to write the code for the Python client and server.
One possible implementation of the Python client is as follows:
And one possible implementation of the Python server is as follows:
From this point on, two scenarios are possible. On the one hand, you could use the LabVIEW client to send a request to the Python server and to send a reply from the Python server to the LabVIEW client:
On the other hand, you could use the Python client to send a request to the LabVIEW server and send a reply from the LabVIEW server to the Python client:
Now you have had an introduction on how to use Zero MQ for communicating between LabVIEW and Python. You are welcome to try it yourself. Or maybe you want to go a step further and e.g. call LabVIEW code from Python. If you are curious about that click here.