Create 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 or general information on the QMH architecture.

A Message Handling Loop (MHL) represents a task the Queued Message Handling (QMH) application can perform, such as acquiring or logging data, in parallel with other tasks. Each MHL can be divided into sub-tasks that correspond to states. MHLs consist of the following components:

  • A message queue
  • A While Loop that reads messages from the message queue
  • A Case structure that contains a subdiagram, also known as a message diagram, for each possible message that can be read, where each message corresponds to a state of the task
  • (Optional) Data that each message diagram of the MHL can access
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 add a message handling loop to the template.

  1. Launch LabVIEW and select Create Project. From the Create Project dialog, launch the Queued Message Handler template.
  2. Open Main.vi from the Project Explorer. Explore the block diagram content to familiarize yourself with the architecture. 
    • Notice that this template contains a typedef (Data Cluster) that defines the cluster that holds the refnums for all message queues. By default, this typedef has space for only one queue.
  3. Complete the following steps to add a second queue to the All Message Queues typedef:
    1. In the Project Explorer window, open Support Vis >> Message Queue.lvlib >> support >> Create All Message Queues.vi.
    2. On the block diagram, right-click the Message Queues constant and select Open Typedef. LabVIEW will launch the Control Editor window.
  1. In the Control Editor window, expand the border of the Message Queues cluster.
  2. Duplicate the UI queue refnum in the cluster.
  3. Name the new queue refnum for example New Task as in the image below.
  4. Select File»Apply Changes and close the Control Editor window. The Message Queues typedef now contains an additional message queue.
    • Notice that the Message Queues out cluster on the front panel has updated to reflect changes made to the typedef.
  1. Modify Create All Message Queues.vi to execute the following steps so that the block diagram matches the image below:
    • Obtain the message queue reference
    • Bundle this queue into the Message Queues out cluster
    • (Optional) Send an initial message to the new MHL​​​​​​
  1. (Optional) If the MHL needs access to data, create a typedef that represents this data.
  2. In Main.vi, create the Message Handling Loop that represents the task:

 
Notes:
  • Recall that in step 4, you bundled the New Task queue refnum into the Message Queues out cluster. The code above shows where this cluster is unbundled and how the wire branch for the New Task queue goes to the New Task MHL.
  • Recall that in step 4, you had the option to send an initial message to the message queue. The image shows the initial message “Initialize”. The code above shows the message diagram (Initialize) that executes when this message is received.
  • Recall that in step 4, you had the option to create a typedef. The code above shows how you wire this typedef, New Task Data, so the MHL can use it.
  • If you want the new task to send messages to the UI queue, branch the wire for the UI queue refnum into the New Task loop.
  • The code above shows the FALSE constant wired to the conditional terminal of the While Loop. In each MHL, only one message diagram should be able to stop the MHL. This design prevents accidental and partial shutdowns by guaranteeing the following conditions:
    • Shutdown code runs only right before the MHL shuts down.
    • Shutdown code always runs to completion.
  • To keep the block diagram of Main.vi compact and readable, you can encapsulate each MHL into a subVI. To further organize the project, you can put each subVI, any supporting VIs, and its data typedef into a project library. Refer to the Continuous Measurement and Logging sample project, available from the Create Project dialog box, for an example of this design.
  • The MHL shown above does not need access to the Stop Event wire. The MHL in the template uses this wire to execute the Fire User Event - Stop VI, which shuts down the Event Handling Loop. No other MHL needs to do this.
     
  1. Add message diagrams to the Case structure in the MHL. To minimize errors and unexpected behavior, ensure each MHL has the following message diagrams:
    1. A message diagram that initializes the task; for example, this diagram could connect to a hardware device, open files for data logging, and so on.
    2. A message diagram that handles unrecognized messages.
    3. A message diagram that, when executed, releases the message queue and stops the loop. For example, see the image below.
      • By default, the message that triggers this message diagram is Exit, but you can change this.
  1. (Optional) If the application requires that the MHL stop performing its task but stay active (to potentially restart the task), create a message diagram that uses the Flush Queue function to remove any pending messages.
    • For example, in a continuous measurement application, you may have Start Measurement and Stop Measurement buttons. Clicking Start Measurement initiates the MHL, which continues execution by sending itself the same message continually. In this application, clicking Stop Measurement should not only stop the measurement but also flush the message queue without stopping the MHL. If you do not flush the message queue, clicking Stop Measurement will have no effect. The message queue contains more messages to continue the measurement, and each of these messages causes another message of its kind to be produced.
​​​
  1. Add code to the EHL that instructs the new MHL to stop in the event of an error or when the application stops; that is, execute the message diagram you created in step 5c. Add this code to the Event Structure and Error case shown in the following figure:
  1. Send messages to the MHL.

Next Steps