Send a Message to a Message Handling Loop in LabVIEW Queued Message Handler Template

Updated Aug 3, 2023

Environment

Software

  • LabVIEW

This article is part of the Queued Message Handling series. Refer to the Queued Message Handling (QMH) overview documentation for general information on the QMH architecture
Messages are strings that instruct a Message Handling Loop (MHL) to execute one of its message diagrams. Messages are produced by the Event Handling Loop (EHL) and are stored in the message queue. Every iteration of the MHL reads the oldest message in the message queue and executes the corresponding message diagram.
There are multiple ways to send a message to a MHL: two ways discussed in this tutorial are (1) sending a message as a string to the Messagge Diagram and (2) creating a UI control that triggers and event in the EHL to send a message to the MHL. Additionally, you may require special procedures once those messages are received like shutting down your program via a "stop message" or controlling a UI object. Sending these special messages are addressed in the Send a Message that Stops the MHL and Send a Message the Programmatically Modifies UI Objects sections below.

This tutorial utilizes the Queued Message Handler LabVIEW template. This an advanced LabVIEW architecture and this tutorial assumes knowledge with basic programming practices. In this tutorial, you will send messages to the Message Handling Loop that you created in previous exercises.

Send a Message to the MHL

  1. Launch the LabVIEW Queued Message Handler template and create a Message Handling Loop (MHL) and create a Message Diagram.
  2. Decide what part of the application will send the message and which MHL will receive the message.
    • You can send messages from the EHL or from a message diagram.
  3. Decide what message diagram will execute when the MHL receives this message. Ensure the message diagram exists and has the same name as the message you want to send.
    • If the message diagram does not exist, create it.
  4. In Main.vi, access the wire that represents the message queue of the receiving MHL. You access this wire by unbundling it from the Message Queues out cluster that is returned from the Create All Message Queues VI. Main.vi already contains the following code that unbundles the UI queue refnum. 
    • Expand this Unbundle by Name function to access the wires of message queues for all MHLs.
  1. In the part of the application that will send the message, create the code pictured below.
    • Note that Message is the text that matches the message diagram you created, and the message queue refnum is the wire you identified in step 4.
  1. (Optional) To specify that this message supersedes others already in the queue, wire TRUE to the Priority Message? input of the Enqueue Message VI.
    • High-priority messages are typically reserved for emergency stop situations. These messages are placed at the front of the message queue, guaranteeing that the receiving MHL will consume that message next.
  1. (Optional) To send data with the message, wire a value to the Message Data input of the Enqueue Message VI. This terminal can accept any data type. For example, the following code sends a double-precision floating-point number along with the message.

Note: LabVIEW displays a coercion dot on the input terminal because the data type of this terminal is variant.
 

Create a Control that Triggers Sending a Message to the MHL

  1. In Main.vi, add a control to the front panel.
    • For example, add a push button (like New Button in the image below). When this button is pushed, an event will be triggered
    • (Optional) If you want a message diagram to programmatically modify this control, bundle the control refnum into the typedef for that MHL.
  2. Add an Event case to the Event structure in the Event Handling Loop.
  3. Configure the event to trigger when the value of this new button changes.
  1. Once the event is configured, click OK. LabVIEW creates an event case in the Event structure.
  2. Associate the terminal with the event by dragging the block diagram terminal for the new control inside this event case.
  3. Add code to this event case that sends a message to an MHL.


Send a Message that Stops the MHL

The QMH template defines one message to stop the MHL; the Exit message is defined in Message Queue.lvlib:Dequeue Message.vi.
Because messages are sent as string datatypes, you can create or change a message without modifying a typedef. If you want your MHLs to shut down on a message other than Exit, change the message in the Dequeue Message VI.


 

Send a Message that Programmatically Modifies UI Objects

To enable a message sent to the MHL to modify a control programmatically,  you should create a refnum for the control and bundle it into the typedef that stores data for that MHL.
The following procedure uses UI Data.ctl as an example.
  1. Create the control refnum and move it into the Initialize subdiagram of the MHL.
  2. Open UI Data.ctl and add space for the refnum to the cluster. 
  3. Select File>>Apply Changes to the update typedef and then Save.
  4. In the Initialize subdiagram of Main.vi, expand this new terminal on the Bundle by Name function and wire the control refnum to it.
The refnum for this control now is available to any message diagram that has access to UI Data.ctl. For example, the following code shows the Disable Button message diagram, which uses the control refnum to disable and gray out the front panel button: