Example Code

Communicating between multiple VIs using a functional global variable

Code and Documents

Attachment

This is an example of how to use a functional global variable subVI to communicate between two VIs. In this particular example one VI is sending a boolean signal and the other VI is reading it. Each VI will be described in turn

How to run this example

Open Sender.vi and Receiver.vi and run both VIs at the same time. Change the value of the button on the Sender.vi front panel and observe the LED indicator on the Receiver.vi

The Functional Global Variable (Functional Global Variable.vi)

This VI has three different Actions associated with it - Init, Get and Set. These are controlled using the Action Enum control. It is recommended that if this is used in a project the Action Enum should be made into a Typedef.

The fundamental idea behind the functional global variable (FGV) is the use of the unitialized shift register. This shift register will hold its value between times when the VI is called. So one VI can call the FGV and Set the value in the shift register and then another VI can call the FGV and Get the value that was set by the first VI. In this way data can be transferred between two VIs running in parallel. Each possible action will be explained below:

Set

Functional Global Variable.png

The above image is a snippet showing the Set case for the FGV. The value stored in the shift register is replaced by the value of Set Value.

Get

Get.png

The above image shows the Get case. This particular FGV is self-initializing. This is used because having an unitialized shift register can cause unexpected behavior between executions of the program. The First Call? will return true only the first time that this FGV is used, and this is to make sure that the user does not attempt to Get the value of the shift register before it is set or initialized.

Init

Init.png

The above image shows the Init case. This case is used to reset the boolean to false. In this simple FGV where only a boolean is being passed, the init case can be completely removed and replaced by a Set with a False Set Value, but this initialization case is useful for more complex FGVs so it is included in this example.

The Sender (Sender.vi)


The Sender VI is a simple VI that is sending a boolean command to the receiver VI. It Sets the value of the FGV so the other VI can read it.

sender.png

The Receiver (Receiver.vi)

The Receiver VI is a simple VI that reads the value of the FGV and displays it on the front panel.

receiver.png

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors