You are on page 1of 104

Exercise Guide

LabVIEW Core 2

Address | City, St Zip Code


ni.com
Exercise 1-1: Parallel Loops with a Stream Channel
Goal
▪ Use a Stream channel to transfer data between two parallel loops.
Scenario
You have a simple VI which has two While Loops. The Writer Loop produces data and the Reader Loop
consumes that data running in parallel. Create a Stream channel to transfer data between these two
parallel running loops.
Guided Instruction
1. Open a blank VI and save it as Parallel Loops with a Stream Channel.vi in the
C:\Exercises\LabVIEW Core 2\Stream Channel directory.
2. Build the front panel as shown in the figure below.

1. Buffered Data – This indicator returns the number of elements in the channel
after the endpoint updates the channel.
2. Stop button – Use this button to stop both loops.

2
3. Open the block diagram of the VI. The figure below shows an example of the block diagram as it
appears after your modifications.

1. Write VI – Write endpoint writes an element to a Stream channel.


2. Read VI – Reads an element from a Stream channel.

4. Set front panel and block diagram options for comfortable code development.
▪ Navigate to Tools»Options and select Block Diagram from the Category list.
▪ Enable Show subVI names when dropped option to display VI, function, and other nodes name
labels when you place them on the block diagram.
▪ Manipulate other options if necessary.
Steps 5 and 6 instruct you on how to create a Stream channel to transfer the output data elements of the
sine function to the Reader Chart indicator.
5. Create a Stream channel.
▪ Right-click the output of the Sine function and select Create»Channel Writer from the shortcut
menu.
▪ Select Stream as a Channel Template and Write for the Endpoint in the Select Channel
Endpoint dialog box.
▪ Right-click the output of the Write VI and select Create» Channel Reader from the shortcut
menu.
▪ Select Stream as a Channel Template and Read for the Endpoint in the Select Channel
Endpoint dialog box.
▪ Place Read VI inside of the Reader Loop.

3
6. Make appropriate connections to transfer data from the Writer Loop to the Reader Loop, to show
buffered data elements, and to stop both loops with one Stop button.
▪ Connect the output of the Stop button to the last element (F) input of the Write VI.
▪ Connect the last element Output of the Read VI to the conditional terminal of the Reader
Loop.
▪ Connect the count output of the Read VI to the Buffered Data indicator.

7. Save and run the VI.


8. Note that the results of the Writer Chart and the Reader Chart are the same.

9. Click the Stop button to stop the execution.


10. Set the input value of the Wait function of the Reader Loop to 500 ms.
11. Run the VI and pay attention on the value of the Buffered data indicator.
12. Click the Stop button and observe that the reader loop continues executing until the buffer contains
data.
13. Stop and close the VI.

End of Exercise 1-1

4
Exercise 1-2: A Simple Tag Channel
Goal
▪ Use a Tag channel to stop multiple parallel loops with a single stop button.
Scenario
You have a simple VI with multiple independent While Loops. Create a Tag channel to stop these loops
with a single Stop button.
Guided Instruction
1. Open a blank VI and save it as A Simple Tag Channel.vi in the C:\Exercises\LabVIEW
Core 2\Tag Channel directory.
2. Place a Stop button on the front panel of the VI.
3. Build the block diagram as shown in the figure below.
Step 4 instructs you on how to create a Tag channel to transfer data between parallel running loops.

1. Write VI – Writes a value to a Tag channel.


2. Read VI – Reads a value from a Tag channel.
3. Number To Decimal String function – Converts a number to a string of decimal digits.
4. Concatenate Strings function – Concatenates input strings and 1D arrays of strings into a
single output string.

5
4. Create a Tag channel to pass the data of the stop button between loops.
▪ Right-click the output of the Stop button and select Create»Channel Writer from the shortcut
menu.
▪ Select Tag as a Channel Template and Write for the Endpoint in the Select Channel
Endpoint dialog box.
▪ Right-click the output of Write VI and select Create»Channel Reader from the shortcut menu.
▪ Select Tag as a Channel Template and Read for the Endpoint in the Select Channel
Endpoint dialog box.
▪ Place Read VI inside of the Loop B.
▪ Repeat the previous three steps and create one more Read VI. Place the Read VI inside
the Loop C.
5. Right-click each Concatenate Strings function and select Create Indicator to create Loop
A, Loop B, and Loop C indicators.
6. Make appropriate connections as shown in the figure above.
7. Save and run the VI.

End of Exercise 1-2

6
Exercise 1-3: Channels, SubVIs, Cases
Goal
▪ A Writer Loop generates random numbers. Use a Stream channel to pass the higher ones to one
loop and the lower ones to another loop.
Scenario
You have a simple VI which has multiple While Loops. The Writer Loop generates random numbers and
writes the higher ones to one channel and the lower ones to another channel. Create a Stream channel
to pass these data elements with indexes from the Writer Loop to the two reader loops.
Guided Instructions
1. Open a blank VI and save it as SubVIs and Cases.vi in the C:\Exercises\LabVIEW Core
2\SubVIs and Cases directory.
2. Create the front panel as shown in the figure below.

3. Open the block diagram panel.


4. Create the block diagram as shown in the figure below following upcoming instructions.

7
5. Create a writer loop to generate random numbers and write the higher ones with their indexes to one
channel and the lower ones to another channel.
▪ Create a While Loop.
▪ Place a Case structure inside of the While Loop.
▪ Select the False case and place a Case structure inside it.
▪ Configure the outer Case structure to compare a random number with a constant.
▪ Place a Bundle function and create a Stream channel in each case in the second Case structure
to write higher numbers in one channel and the lower numbers in the other channel.

6. Create two Reader Loops to read generated data.


▪ Place two While Loops with the Reader A and Reader B sub diagram labels.
▪ Place the Mean VI from the C:\Exercises\LabVIEW Core 2\SubVIs and Cases directory
inside each reader loop.
7. Complete the connections as shown in the figure above.
8. Configure the True case of the outer Case structure as shown in the figure below to stop all loops
with one Stop button. This is the reason we need 2 case structures in this code.

9. Save and run the VI.


End of Exercise 1-3

8
Exercise 1-4: Fix Buffer Overflow Error Using Stream Communication
Goal
▪ Fix a buffer overflow problem using Producer/Consumer (Data) design pattern.
Hardware Setup:
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-6212 multifunction I/O device paired
with the BNC-2120 shielded connector block. Analog Input 2 should be connected to the Sine/Triangle BNC connector. Analog Input 3 should
be connected to the TTL Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Note: This hardware setup is going to be used throughout the course in several exercises.

(Simulated) In the absence of hardware solutions, we use a simulated PCI-6221 multifunction I/O device.

Observe Buffer Overflow Error


1. Open C:\Exercises\LabVIEW Core 2\Fix Buffer Overflow\Fix Buffer Overflow.lvproj.
2. From the Project Explorer window, open the Buffer Overflow VI.
3. Examine the block diagram.

9
▪ DAQmx VIs outside the While Loop are configuring the DAQmx device to continuously acquire
data at a sample rate of 1,000 Hz.
▪ The code in the While Loop repeatedly performs the following actions.
- Read 100 samples from the DAQmx buffer in the computer memory.
- Check how many samples are left in the DAQmx buffer.
- Simulate the duration of processing code (for example, analysis nodes, write to file, and
so on) by using a Wait (ms) function.
4. Observe a buffer overflow error.
▪ On the front panel, set the controls to the following values:
Sample Rate 1000
Number of Samples 100
Process Duration(ms) 25

▪ Run the VI.

10
▪ Describe the behavior of the Samples in DAQ Buffer indicator.
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

Note: This indicator shows the number of samples in the buffer remains within the same range
and does not continue increasing redundant over time. This indicates that the DAQmx Read is
reading and taking out the samples in the DAQ buffer frequently enough.
▪ Increase the Process Duration (ms) control by increments of 25. This simulates putting more
and more time-intensive processing into the While Loop. This can cause each While Loop
iteration to take longer, which causes the DAQmx Read to read less frequently from the DAQ
buffer.
▪ Each time you increment, check if the Samples in DAQ Buffer indicator starts to increase
redundant over time.
▪ By the time you increase the value of the Process Duration (ms) control to 100 or more, you
should notice that the number of samples left in the DAQ buffer will continue to increase because
the VI is not reading samples from the DAQ buffer frequently enough.
Eventually, the VI will return the following error because the DAQ buffer has a finite maximum
number of samples it can hold.

Note: Increase the Process Duration to 1,000 to get this error faster. In this VI, this error will
occur when Samples in DAQ Buffer is greater than 10,000.

11
5. Predict how fast the processing code must execute to prevent the buffer from growing.
▪ If Sample Rate is 1,000 and Number of Samples is 100, what values of Process Duration will
prevent the DAQ buffer from continuously growing larger and larger?
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

(Answer): If the Sample Rate is 1,000 Hz (samples per second) and the number of samples the
DAQmx Read reads every iteration is 100, then the DAQmx Read must execute at least once
every 0.1 seconds (the sample rate divided by the number of samples read per iteration) to
prevent the DAQ buffer from growing.)

For the DAQmx Read to execute at least once every 0.1 seconds, the processing code duration must be
at least less than 100 ms on average. Remember the DAQmx Read also has an execution duration.

Fix Buffer Overflow Problem

1. From the Project Explorer window, open the Fix Buffer Overflow with Queue VI.
2. Examine the block diagram. Notice that the acquisition code and processing code are now separated
into two different loops.

12
3. Modify the block diagram, as shown in the following figure, to stream data from the acquisition loop to a separate processing loop.

1. Obtain Queue function – Creates a queue and returns a reference to that queue.
2. Waveform Constant – Add a waveform constant to the block diagram and wire it to the element data type input. This sets the data
type of the queue to Waveform.
3. Enqueue Element function – Adds an element to the queue.
4. Dequeue Element function – Removes an element from the front of the queue and returns the element.
5. Case structure – Executes processing code only if the Dequeue Element function does not time out. Make sure the processing code
is in the False case.
6. Get Queue Status function – Returns information about the current state of the queue, such as the number of elements currently in
the queue.
7. Release Queue function – Notice that this VI uses dataflow to ensure that this function does not execute until all three loops have
finished executing. In general, you do not want to release the queue until all loops accessing the queue have finished executing.

13
▪ Wire the queue reference wires to connect the queue functions. The queue reference wire
specifies which queue the queue functions should perform their action on.
4. Examine the behavior of the VI.
▪ On the front panel, set the controls to the following values.
Sample Rate 1000
Number of Samples 100
Process Duration(ms) 25

▪ Run the VI. Notice that the Samples in DAQ Buffer indicator never goes above a certain
number. This means the acquisition loop is reading samples from the DAQ buffer frequently
enough.
▪ Example:

14
▪ Describe the behavior of the # Elements in Queue indicator.
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

▪ Increase the Process Duration (ms) control to 1,000.

What is the behavior of the # Elements in Queue indicator now?


_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________

Note: If this indicator is steadily increasing, that means that overall the VI is enqueueing elements
at a faster rate than the VI is dequeuing elements. Therefore, the number of elements in the
queue is growing over time. It will continue to grow until either the enqueue rate becomes equal
to or slower than the dequeue rate.
▪ Notice that although the number of elements in the queue is growing, the LabVIEW buffer for the
queue is much larger than the DAQ buffer, so the VI can hold a much greater number of samples
before breaking.
Note: If the enqueue rate remains faster than the dequeue rate forever, the number of elements
in the queue will continue to grow until LabVIEW eventually runs out of memory. LabVIEW does
its best to optimize memory usage, but eventually LabVIEW will run out of memory and crash if
the queue keeps growing forever.

5. Click the Stop Acquisition Loop button on the front panel.


▪ Notice that the VI continues to run. The # Elements in Queue indicator now starts to decrease
because the Dequeue Element loop continues to remove elements from the queue and process
them. Eventually, this loop will dequeue and process all the remaining elements.
Note: To speed up the processing, decrease the Process Duration (ms) control to 10 or less.

6. When the # Elements in Queue is down to 0, click the Stop Processing Loop button and click the
Stop Queue Monitoring Loop button.

15
On the Job
Do any of your applications require streaming data from a writer loop to a reader loop?
Can you use a queue to stream data from the writer loop to the reader loop?
If so, what does the writer loop need to do? What does the reader loop need to do?
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

End of Exercise 1-4

16
Exercise 2-1: UI Event Handler Design Pattern
Goal
▪ Develop an application using the User Interface Event Handler design pattern.
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
You want to create an application that has a front panel with multiple buttons. Clicking each button
should trigger specific code to execute. You will use the User Interface Event Handler design pattern to
implement this application.
The following table lists the events you will implement in the User Interface Event Handler VI you create.

Event Event Description

“Exit”: Value Change Stops the While Loop

“Acquire”: Value Change Acquires a finite acquisition

“Analyze”: Value Change Analyzes data

“Save Data”: Value Change Logs data to a text file

“Clear”: Value Change Clears the graph data.

“Generate Stimulus”: Value Change Updates the Analog Output channel signal.

Guided Instruction
1. Open C:\Exercises\LabVIEW Core 2\User Interface Event Handler\User
Interface Event Handler.lvproj.
2. From the Project Explorer window, open the User Interface Event Handler VI. Notice that the
front panel has already been created for you. In this application, clicking each Boolean button
should trigger its corresponding code to execute.

17
3. Explore the block diagram.
▪ Notice that the block diagram contains Boolean button terminal next to the code that
corresponds to each Boolean button.

18
4. Locate the While Loop already on the block diagram. Place an Event structure inside the While Loop.
5. Wire the error wire through, as shown in the following figure.

1. Error Wire – Wire the error wire through the Event structure so that it creates input and output tunnels.
2. Shift register – Create shift register. Storing the acquired data in the shift register allows multiple event cases to update and
read the current acquisition data.

19
6. Configure the Event structure to handle the “Acquire”: Value Change event.

1. “Acquire”: Value Change event – Right-click the Event structure border, then click Edit Events Handled by This Case. In
the Event Sources section of the Edit Events window, click the Controls»Acquire, and in the Events section select Value
Change.
2. Acquire control and corresponding code – Move this control and code inside the event case.
3. Shift register – Wire the data output of the DAQmx Read VI into the shift register. This updates the current acquisition data
stored in the shift register. You will update and read this data in other event cases.
4. Graph indicator – Move and wire this indicator as shown. Rewire the error wires as shown.
5. Event Data Node – Identifies the data that the Event structure returns when an event occurs. You can resize the node to
display only one or multiple elements.

20
7. Configure the Event structure to handle the “Analyze”: Value Change event.

1. “Analyze”: Value Change event – Right-click the Event structure border and select Add Event Case.
2. Edit Events window – In the Event Sources section of the Edit Events window, click the Controls»Analyze, and in the Events
section select Value Change.
3. Analyze control and corresponding code – Move this control and code inside the event case.
4. Shift register – Wire the shift register data to the N-Channel Data input of the Analyze subVI to read the current acquisition
data. Wire the shift register data through the event case to store the data back in the shift register.

21
8. Configure the Event structure to handle the “Save Data”: Value Change event.

1. “Save Data”: Value Change event – Right-click the Event structure border and select Add Event Case.
2. Edit Events window – In the Event Sources section of the Edit Events window, click the Controls»Save Data, and in the
Events section select Value Change.
3. Save Data control and corresponding code – Move this control and code inside the event case.
4. Shift register – Wire the shift register data to the input of the Transpose 2D Array function to read the current acquisition data.
Wire the shift register data through the event case to store the data back in the shift register.

22
9. Configure the Event structure to handle the “Clear”: Value Change event.

1. “Clear”: Value Change event – Right-click the Event structure border and select Add Event Case.
2. Edit Events window – In the Event Sources section of the Edit Events window, click the Controls»Clear, and in the Events
section select Value Change.
3. Clear control and corresponding code – Move this control and code inside the event case.
4. Shift register – Wire the empty 2D DBL array constant to the shift register. This clears the stored acquisition data in the shift
register by replacing it with empty data.

23
10. Configure the Event structure to handle the “Generate Stimulus”: Value Change event.

1. “Generate Stimulus”: Value Change event – Click the Event structure border and select Add Event Case.
2. Edit Events window – In the Event Sources section of the Edit Events window, click the Controls»Generate Stimulus, and in
the Events section select Value Change.
3. Generate Stimulus control and corresponding code – Move this control and code inside the event case.
4. Wire the tunnels – This case does not update or read the acquisition data.
5. Error Case structure – Wrap the case structure containing the dialog boxes in another one and connect error wire to its
conditional terminal, then connect error and DAQmx task wires throughout the case structure in the Error and No Error cases.
We do this to prevent the dialog box from appearing in case of a VI error.

24
11. Configure the Event structure to handle the Exit event.

1. “Exit”: Value Change event – Right-click the Event structure border and select Add Event Case. In the Event Sources
section of the Edit Events window, click the Controls»Exit and in the Events section select Value Change.
2. Exit control – Move this control inside the event case.
3. Wire the tunnels –This case does not update or read the acquisition data.
4. True constant – This causes the Exit event case to stop the While Loop.
5. Event Tunnel – Wire the True constant through an event tunnel to the OR function. Make sure that when right-clicking the
tunnel, the Use Default If Unwired option is enabled, which means that any case with no constant wired defaults to False.

12. Save the VI.

25
Test the VI
1. Run the VI.
2. Set the following control values.

AI Voltage Channel PCI 6221/ai0, PCI 6221/ai3

Sample Rate (Hz) 1000

Samples per channel 100

AI Voltage Channel PCI 6221/ao0

3. Click the Acquire button. Verify that acquisition data appears on the Graph.
4. Click the Analyze button. Verify that a dialog window appears showing analysis data.
5. Click the Save Data button. Verify that a file dialog appears and that the log file is created.
6. Click the Generate Stimulus button. Verify that a file dialog appears and that the Analog Output
channel signal is updated.
7. Click the Clear button. Verify that the VI clears the Graph.
8. Click the Exit button. Verify that the VI exits.

Your Turn
1. Add a new control.
2. Configure an event for the control.
3. Add code that executes each time the event occurs.
4. Test if your new code executes as expected.

On the Job
Do you have any UI event-based applications? If so, describe the desired event-based functionality for
the application.

End of Exercise 2-1

26
Exercise 2-2: Event-Driven State Machine
Goal
▪ In this exercise, you will configure the VI that uses the Event-Driven State Machine design pattern
and explore its limits and the caveats. After that, you will analyze the Event-Driven State Machine
template provided by LabVIEW.
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
You will use an unconfigured Event-Driven State Machine the front panel of which is shown in the
following figure.

27
The following table lists the events in this application and their corresponding state diagram logic.

Event State Diagram Logic


“Acquire”: Value Change Acquires data, analyzes the data, and logs
data if it exceeds the threshold, updates the
graph, and then waits for another event.

“Clear”: Value Change Clears the graph data, and then waits for
another event.

“Generate Stimulus”: Value Change Generates either 5V (ON position) or 0V


(OFF position) at the selected analog output,
and displays a dialog box informing the user
about it, and then waits for another event.

“Exit”: Value Change Exists the application.

Examine the state diagram for this application in the following figure.

28
This application uses the Event-Driven State Machine design pattern for the following reasons.
▪ This application is event driven. The user will click many buttons while the application is running,
and those Value Change events must trigger the corresponding code to execute.
▪ The code that corresponds to Value Change events can be described by a state diagram.

Guided Instructions
In this section, you will implement the state transition logic described by the arrows in the previous figure.
1. Open C:\Exercises\LabVIEW Core 2\Event-Driven State Machine\Event-Driven
State Machine.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine VI.
3. Examine the front panel. In this application, clicking the buttons described in the table on the first
page will execute the corresponding code.

29
4. Examine the Event-Driven State Machine design pattern.
▪ Explore the block diagram and notice the state machine components as shown in the following figure.

1. While Loop
2. Type Definition enum – Click on this enum and notice that it contains all the states described in the state diagram shown in
the previous figure.
3. Case structure – Click on the Case Selector and notice that there is a case for every state described in the state diagram
shown in the previous figure.
4. Cases – Each case contains both state code and the state transition code.
5. State Machine Data – The Type Definition cluster constant defines the state machine data. The shift register stores the data
and allows multiple states to access and modify the data.

30
Question 1: What is the first case that this VI will execute?
_______________________________________________________________________

5. Examine the Initialize case.


Question 2: What case will the next iteration of the While Loop execute after the Initialize case?
___________________________________________________________________________
___________________________________________________________________________
6. Examine the Wait for Event case. This case exists in every application based on the Event-Driven State Machine design pattern.
▪ Notice that this case contains an Event structure.
▪ Based on the state transition diagram, what events do you expect to see defined in the Event structure?
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
▪ Click the event selector of the Event structure to view the defined event cases.
▪ Based on the state transition diagram, write the state transition enum value you need in each event case.
States Enum Value (Initialize, Wait for
Event Case Event, Acquire, Analyze, Log, Clear,
Update Graph, Update Stimulus, Exit)

“Acquire”: Value Change

“Clear”: Value Change

“Generate Stimulus”: Value Change

“Exit”: Value Change

31
▪ In the Project Explorer window expand support folder, and drag the States.ctl enum to the following event cases on the block
diagram. Set this enum to the values (Acquire, Update Stimulus, Clear, Exit) shown in the following figures. This defines which
state the state machine will transition to next.

32
33
7. Define what happens when you click the Acquire button.
▪ As seen in the state transition diagram, when you click the Acquire button, the next iteration of the While Loop will execute the
Acquire case.
▪ Go to the Acquire case of the Case structure, which corresponds to the Acquire state in the state transition diagram.
▪ Based on the state transition diagram, which state should the state machine go to after the Acquire state? _____________
▪ Modify the Acquire case to go to the Analyze state next, as shown in the following figure.

▪ Which state should the state machine go to after the Analyze state?
________________________________________________________________________________________________________
____________________________________________

34
▪ Modify the Analyze case to go to either the Log or Update Graph state next, as shown in the following figure.

▪ Which state should the state machine go to after the Log state?
________________________________________________________________________________________________________
____________________________________________

35
▪ Modify the Log case to go to the Update Graph state next, as shown in the following figure.

36
▪ Which state should the state machine go to after the Update Graph state?
________________________________________________________________________________________________________
____________________________________________

▪ Modify the Update Graph case to go back to the Wait for Events state next, as shown in the following figure.

8. Define what happens when you click the Generate Stimulus button.
▪ Examine the Wait for Event state.
▪ Select the Wait for Event case of the Case structure, and then select the “Generate Stimulus”: Value Change event case of
the Event structure.
▪ Notice that the enum tells the state machine to execute the Update Stimulus case next.
▪ Examine the Update Stimulus state.
- Select the Update Stimulus case of the Case structure.
▪ Based on the state transition diagram, which state should the state machine go to after the Update Stimulus state?
________________________________________________________________________________________________________
____________________________________________

37
▪ Modify the Update Stimulus case to go back to the Wait for Events state next, as shown in the following figure.

9. Define what happens when you click the Clear button.


▪ Examine the Wait for Event state.
▪ Select the Wait for Event case of the Case structure, and then select the “Clear”: Value Change event case of the Event
structure.
▪ Notice that the enum tells the state machine to execute the Clear case next.
▪ Examine the Clear state.
▪ Select the Clear case of the Case structure.
▪ Based on the state transition diagram, which state should the state machine go to after the Clear state?

38
▪ Modify the Clear case to go to the Update Graph state next, as shown in the following figure.

39
10. Define what happens when you click the Exit button.
▪ Examine the Wait for Event state.
▪ Select the Wait for Event case of the Case structure, and then select the “Exit”: Value Change event case of the Event
structure.
▪ Notice that the enum tells the state machine to execute the Exit case next.
▪ Examine the Exit state.
▪ Select the Exit case of the Case structure.
▪ Notice that this case outputs a True constant that stops the While Loop and exits the application.
▪ Even though the application will exit after the Exit case, you still need to wire a value into the enum output tunnel of the Case
structure to ensure that the output tunnel value is defined in all cases.
▪ Modify the Exit case to wire an Exit enum value to the enum output tunnel, as shown in the following figure, even though the
state machine will exit immediately after the Exit case.

40
11. Examine how the state machine allows multiple states to access the State Machine Data cluster data.
▪ Notice that the Acquire, Analyze, Log, Clear, and Update Graph states all write to or read from the Acquisition Data element of the
State Machine Data cluster.
▪ Notice how the Wait for Event» ”Generate Stimulus”: Value Change and Update Stimulus states both access the Stimulus Value
element of the State Machine Data cluster.
12. Save the VI.

Test
1. Run the VI.
2. Set the controls to the following values.

AI Voltage Channel PCI-6221/ai0, PCI-6221/ai2:3

Sample Rate (Hz) 1000

Samples per channel 100

Threshold (rms) 1

AO Voltage Channel PCI-6221/ao1

3. Click the Generate Stimulus button to set it to True.


4. If you have a real DAQ device, the analog output channel should now be outputting 5 V.
5. Click the Acquire button.
▪ Verify that the graph indicator displays the acquired data.
▪ If necessary, adjust the Threshold (rms) value to a low enough number, so that the acquired data is above the threshold. Click
Acquire again. This will cause the VI to log the data to a file.
▪ In Windows Explorer, navigate to the C:\Exercises>\LabVIEW Core 2\ Event-Driven State Machine directory.
Notice that a log file is created each time the acquisition data exceeds the threshold.
6. Click Clear button.
▪ Verify that the VI clears the graph.
7. Click the Exit button.
▪ Verify that the VI exits.

41
Explore Limits/Caveats of Event-Driven State Machine Design Pattern
The Event-Driven State Machine design pattern is good for applications where users click controls on the front panel to trigger corresponding
code to execute and the code logic can be described by a state diagram. This design works best when the corresponding code duration is
short.
If the code duration is long, such as 10 seconds, that means the user interface (front panel) will appear unresponsive to the user for those 10
seconds.
Explore what happens when corresponding code duration is long.
1. Modify the Analyze case code execution duration to be 10 seconds.
▪ Add a Wait (millisecond) node in the Analyze case. Right-click the input and select Create» Constant. Set the constant to 10000.

2. Examine the behavior of the VI.


▪ Run the VI.
▪ Click the Acquire button. This causes the state machine to go into the Acquire case and then the Analyze case, which will execute
for 10 seconds.

42
▪ Click the Clear button. Usually, the Clear button would show a latching mechanical action (ON, and then reset back to OFF).
Notice that the Clear button appears unresponsive because it appears like it is still pressed (ON). This happens because the code
is still in the Analyze case. When the code eventually goes back to the Wait for Event state, it will read the Clear button click in the
“Clear”: Value Change event case, and the Clear button switches back off. The code to clear the graph will then execute.
▪ Click Exit to stop the VI.

3. Delete the Wait node from the block diagram to return to the original behavior of the VI.
There are two techniques to solve this problem and make the user interface still appear responsive while code is taking a longer time
to execute. You will learn about these techniques later.
▪ Method 1: If code is taking a longer time to execute, disable controls to let the user know that the use of those controls id
impossible. When the code is done executing and the application can once again process events, then re-enable those controls.
▪ Method 2: Use a design pattern based on the Producer/Consumer (Events) design pattern. This design pattern uses two loops:
▪ Loop 1: Handle events to keep the user interface responsive.
▪ Loop 2: Execute the code that corresponds to the events.

Explore an Event-Driven State Machine Template


LabVIEW includes an Event-Driven State Machine template. Open the template by following these steps:
1. Open LabVIEW.
2. From the Getting Started window click the Create Project button.
3. Select Simple State Machine from the list and click Finish to create a project based on an event-driven state machine.
4. Explore the project.

Your Turn
Try making the following improvements to the application that you created in this exercise.
Challenge #1
When developing an application that outputs signals, you should ensure that the application sets the outputs to a safe value when the
application exits.

43
Modify this VI, so that the Exit case sets the analog output channel (PCI-6221/ao1) to 0 V.
Challenge #2
Modify this VI, so that every time the VI starts executing, the VI initializes the Generate Stimulus control to False.
Refer to the Challenge Hints section at the end of this exercise if you need a hint.
On the Job
Do any of your applications have a need for an event-driven state machine? If so, draw the state diagram for the application below.

44
Challenge Hints
Challenge #1
Method 1 - Shift Register: Add an AO Voltage Channel item to the State Machine Data shift register, so you can access the most current AO
Voltage Channel in multiple event cases (Generate Stimulus, Exit).
Method 2 - Local Variable: Use a local variable in the Exit case to access the current value of the AO Voltage Channel control. Using
duplicate terminals during initialization or shutdown generally does not result in race conditions if you properly sequence application.
Challenge #2
Use a local variable in the Initialize case to initialize the value of the Generate Stimulus control.
Refer to the C:\Solutions\LabVIEW Core 2\Exercise 2-2 (Challenge Solution) for the solution code.
Answers
Question 1 - Answer: The first case this VI executes is the Initialize case. Notice that the enum shift register is initialized with Initialize.
Question 2 - Answer: The next case the While Loop executes is the Wait for Event case.

End of Exercise 2-2

45
Exercise 2-3: Producer/Consumer (Events)
Goal
▪ Explore an example using the Producer/Consumer (Events) design pattern.
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-6212 multifunction I/O device paired
with the BNC-2120 shielded connector block. Analog Input 2 should be connected to the Sine/Triangle BNC connector. Analog Input 3 should
be connected to the TTL Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
You have a VI that uses the Producer/Consumer (Events) design pattern to process messages.
Guided Instructions
Part-1 Disable Controls
1. Open C:\Exercises\LabVIEW Core 2\Producer Consumer (Events)\Producer Consumer (Events).lvproj.

46
2. From the Project Explorer window, open the Main VI.

3. Run the VI.


4. Set the Sample Rate control value.

47
5. Click the Generate button.
▪ Notice the Current Consumer State indicator displays Generate Signal message.
▪ Notice that the Waveform Graph indicator displays the generated signal and the Actual Sample Rate indicator displays the
sample rate of the generated signal.
▪ Notice that the RMS Result and the Threshold Exceeded indicators haven't updated yet.
6. Change the Sample Rate control value and click the Generate button again.
▪ Notice that the Actual Sample Rate indicator updated to show the sample rate that you set.
7. Set the Threshold control value.
8. Click the Analyze button.
▪ Notice that Current Consumer State indicator displays the Analyze Signal message.
▪ Notice that the RMS Result indicator shows the RMS value of the generated signal.
▪ Notice that depending on the Threshold control value, the Threshold Exceeded indicator will also update to show if the RMS
value is over the threshold value or not.
9. Click the Exit button to stop the execution.
10. Inject an error in the Producer Loop, and see what happens.
▪ Navigate to the “Generate Signal”: Value Change event case.
▪ Add an Error Cluster From Error Code VI on the block diagram, as shown in the following figure.
▪ Right-click the error code (0) input and select Create» Constant.
▪ Set the constant to –1.
▪ Wire the error out output to the output tunnel of the Event structure in the “Generate Signal”: Value Change event case. The
Error Cluster From Error Code VI will now pass an error with a code of -1 on its error out output.

48
▪ Run the VI again and click the Generate button.
▪ Notice that the VI does not behave properly anymore. This is because an error in the Producer Loop has caused the Consumer
Loop to shut down.
▪ Because the Consumer Loop was doing the bulk of the work, the VI no longer behaves correctly. The Producer Loop is still
running.
11. Close the VI and the project. Do not save your changes.
This VI does not include any error handling.

End of Exercise 2-3

49
Exercise 2-4: Channeled Message Handler
Goal
▪ Explore an example using the CMH design pattern.
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
You have a VI that uses the CMH design pattern to process messages, like the Producer/Consumer
(Events) LabVIEW project in the previous exercise.
Guided Instructions
1. Open C:\Exercises\ LabVIEW Core 2\Channeled Message Handler\Channeled
Message Handler.lvproj.
2. From the Project Explorer window, open the Main VI.
3. Explore the Message Queue VIs included in this project.
▪ In the Project Explorer window, expand the Support VIs folder, notice the Message Queue
VIs under the Message Queue LabVIEW library.
▪ These three VIs are used in the Main VI block diagram.
▪ Enqueue Message
▪ Dequeue Message
▪ Abort Message Queue
▪ Switch to the block diagram of the Main VI.
▪ Open the Context Help window.
▪ Notice that the Enqueue Message and Dequeue Message VIs have message (string data
type) and data (variant data type) terminals.
▪ If you look at the block diagrams of the Message Queue VIs, you will see that these VIs use
the channel wire writer and reader endpoints (in this case — messenger channel).
4. Examine the block diagram similarities of the CMH design pattern compared to the
Producer/Consumer (Events) design pattern from the previous exercise.
▪ The Event Handling Loop in the CMH design pattern is similar to the Producer Loop in the
Producer/Consumer (Events) design pattern.
▪ The Message Handling Loop in the CMH design pattern is similar to the Consumer Loop in
the Producer/Consumer (Events) design pattern.
5. Explore how the CMH behaves if an error occurs in the Event Handling Loop.
▪ Imagine an error occurs in the Event Handling Loop. The error will be passed into the Error
Handler - Event Handling Loop VI.
▪ Double-click the Error Handler - Event Handling Loop VI to open its block diagram.
▪ Examine the block diagram of this VI to explore how it handles the error. This VI passes the
error into the Check Loop Error VI.
▪ Notice that the Exit on Error? input of the Check Loop Error VI isn't wired, so we are not
passing any value to it.

50
▪ Double-click the Check Loop Error VI to open its block diagram.
▪ Notice that the Exit on Error control will read default False value when this VI executes. This
means that the Check Loop Error VI will enqueue an Error message along with the error
message data on the message channel.
Note: If the error is listed in the Ignore Errors array, the Check Loop Error VI will ignore the
error and do nothing.
▪ Go back to the Main VI block diagram. Because the error handling VIs enqueued an Error
message, the Dequeue Message VI in the Message Handling Loop now dequeues the
Error message and the error message data. This causes the inner Case structure of the
Message Handling Loop to execute the Error case.
▪ The Error case displays an error dialog using the Simple Error Handler VI.
6. Explore how the CMH handles shutting down both loops if an error occurs in the Message
Handling Loop.
▪ Imagine an error occurs in the Message Handling Loop. The error will be passed into the
Error Handler - Message Handling Loop VI.
▪ Examine the block diagram of this VI to explore how it handles the error. This VI passes the
error into the Check Loop Error VI.
▪ Notice that now a true boolean constant is wired to the Exit on Error terminal of the Check
Loop Error VI.
▪ Open the block diagram of the Check Loop Error VI.
▪ Based on the value of the Exit on Error control (which is TRUE), the corresponding Case
structure executes the Abort Message Queue VI instead of queuing an Error message.
▪ Now open and examine the block diagram of the Abort Message Queue VI. Abort Message
Queue VI will enqueue an Exit message along with the error message data on the message
channel. It also wires the TRUE boolean constant to the abort terminal of the Messenger
channel writer endpoint.
▪ Go back to the Main VI block diagram. Because the Error Handler - Message Handling Loop
VI enqueued an Exit message, the error message data, and the abort terminal’s value, the
Dequeue Message VI dequeues the same data: Exit message, the error message data, and
the true value from the channel aborted terminal.
▪ Channel aborted output of the Dequeue Message VI passes the true value to the conditional
terminal of the outer Case structure.
▪ The True case performs the following actions.
▪ Displays an error dialog using the Simple Error Handler VI.
▪ Writes Exit Application message to the Current Consumer State indicator.
▪ Uses the Write VI to tell the Event structure in the Event Handling Loop to execute the
<channel value>: User Event event case next.
▪ Sends a True value to the Condition terminal, which stops the Message Handling Loop.
Note: To learn more about this technique, refer to the following Events Functions topics in the
LabVIEW Help: Create User Event , Register For Events , Generate User Event , and Destroy
User Event.
▪ Select the <channel value>: User Event event case in the Event Handling Loop. Notice that
this case sends a True value to the conditional terminal, which stops the loop.
7. Test how the CMH handles an error in the Message Handling Loop.

51
▪ Select the Generate Signal case in the Message Handling Loop. Delete the error wire
between the Variant To Data function and the Merge Errors function.
▪ Add an Error Cluster From Error Code VI to the block diagram as shown in the following
figure.
▪ Right-click the error code input and select Create» Constant.
▪ Set the constant to –1.
▪ Wire the error out output of the Error Cluster From Error Code VI to the Merge Errors
function.

52
▪ Run the VI.
▪ Click Generate. Notice that the VI handles the error by exiting.
▪ To follow the flow of data, run the VI again.
▪ Turn on the execution highlighting.
▪ Click Generate, and follow the flow of data on the block diagram.
▪ When finished, change the Generate Signal case back to its original code

53
8. Test how the CMH handles an error in the Event Handling Loop.
▪ Select the “Generate Signal”: Value Change event case in the Event Handling Loop.
▪ Add an Error Cluster From Error Code VI on the block diagram, as shown in the following
figure.
▪ Right-click the error code input and select Create» Constant.
▪ Set the constant to –1.
▪ Wire the error out output of the Error Cluster From Error Code VI to the error in input of the
Error Handler — Event Handling Loop VI.

▪ Run the VI.


▪ Click Generate. Notice that the VI handles the error by displaying an error dialog.
▪ To follow the flow of data, run the VI again.
▪ Turn on the execution highlighting and click Generate again. Follow the flow of data on the
block diagram.
▪ When finished, change the “Generate Signal”: Value Change event case back to its original
code.
9. Close the VI and the project.

54
Explore a Channeled Message Handler Template
Follow the instructions below to use that template in LabVIEW 2019.
1. Navigate to C:\Exercises\LabVIEW Core 2\Channeled Message Handler Template directory.
2. Copy the Channeled Message Handler folder into this directory: C:\Program Files
(x86)\National Instruments\LabVIEW 2019\ ProjectTemplates\Source\Core.
3. Install the Channeled Message Handler.xml file into this directory: C:\Program Files
(x86)\National Instruments\LabVIEW 2019 \ProjectTemplates\MetaData.
4. Go to the Getting Started window.
5. Select File» Create Project.
6. In the Templates page, select Channeled Message Handler and click Finish.
7. Close the project without saving when finished.

On the Job
Do any of your applications require using the CMH design pattern because the Event-Driven State
Machine design pattern does not meet the application requirements?
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
If so, continue to study the Channeled Message Handler design pattern.

End of Exercise 2-4

55
Exercise 3-1: Property Nodes and Control References
Goal
▪ Use Property Nodes and control references to change the properties of front panel objects
programmatically.
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
When you acquire data, you want to ensure that the front panel controls remain unchanged during the
acquisition. Use references and Property Nodes to programmatically disable the controls to prevent them
from being changed while the VI is acquiring data. When the acquisition is complete, re-enable the
controls.

Instructions
Part-1 Disable Controls
1. Open C:\Exercises\LabVIEW Core 2\Property Nodes\Property Nodes.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine Property Nodes VI.
3. Run the VI and then click the Acquire button.
▪ Notice that while the acquisition occurs, the controls are still enabled. You can change the
values on the controls during the acquisition.
▪ Click the Exit button.

56
4. Create control references for each of the front panel controls you want to disable during
acquisition. On the front panel, press <Shift> and select all of the following controls and then
right-click the selection and select Create» Reference.
▪ AI Voltage Channel
▪ Sample Rate (Hz)
▪ Samples per channel
▪ Threshold (rms)
▪ AO Voltage Channel

57
5. Modify the block diagram as shown in the following figure to disable the controls when the VI is
performing an acquistion.

1. Select the Wait for Event case.


2. Select the “Acquire” – Value Change event.
3. Move all the control references into the “Acquire” – Value Change event case.
4. Build Array – Use this function to combine the control references so you can use a single
Property Node to disable them during acquisition.
5. For Loop – Wire the array of control references through a For Loop.
6. Property Node – Add a Property Node inside the For Loop, set it to write by right-clicking the
Property Node and selecting Change All To Write. Connect the array of control references and
error output to the Property Node. After that, select the Disabled property from the drop-down
list. Then right-click the Property Node input and select Create» Constant and set the enum
constant to Disabled.
7. Make sure the error wire is connected through a shift register on the For Loop.

▪ Complete block diagram wiring as shown.


Test
1. Run the VI and click the Acquire button. Notice that the controls are disabled.
2. Click Exit to stop the VI.
3. Run the VI again and notice that the controls are still disabled.

58
Part 2—Enable Controls
You want to disable the controls during the acquisition, but re-enable them when the acquisition is
complete.
1. Modify the block diagram as shown in the following figure to enable the controls in the Update UI
case.

1. Select the Update UI case.


2. Create copies of the control references and place them in the Update UI case.
3. Build Array – Combine the control references into an array.
4. For Loop – Place For Loop and wire the array into it.
5. Property Node – Add a Property Node inside the For Loop, set it to write by right-clicking the
Property Node and selecting Change All To Write. Connect the array of control references and
error output to the Property Node. After that select the Disabled property from the drop-down list.
Then right-click the Property Node input and select Create» Constant and set the Enum
constant to.
6. Make sure the error wire is connected through a shift register on the For Loop.

59
Test
Run the VI and click the Acquire button.
Notice that the controls are disabled for the duration of the acquisition and then re-enabled when the
acquisition is complete.
End of Exercise 3-1

60
Exercise 3-2: Customize the VI Window
Goal
▪ To further affect the attributes of a VI by using Property Nodes and Invoke Nodes.
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
You can set the appearance properties of a VI statically by using the VI properties page. However, robust
user interfaces commonly modify the appearance of a front panel while the program is running.
Modify the VI to have the following appearance and behaviors when the VI is running:
▪ Hide the tool bar
▪ Hide the menu bar
▪ Hide the scroll bars
▪ Move to the center of the screen

Design—Properties

Use the following properties and methods on the VI Class:


▪ Front Panel Window: Show Menu Bar - When this property is False, the menu bar of the VI is
not displayed.

▪ Tool Bar: Visible - When this property is false, the tool bar of the VI is not displayed.

Design—Methods
Unlike properties, a method has an effect every time you call it. Therefore, you should call methods only
when you want to perform an action. For example, if you call the Front Panel: Center method during
each iteration of a loop, the VI is continually centered, thereby preventing the user from moving it. You
can use a Case structure to control calling the method in a given iteration of a loop. Use the following
method on the VI class:
▪ Center - Each time this method is called, the VI moves to the center of the screen

Use the Context Help window to view descriptions of each property and method.

61
After you implement the changes to the VI, when you run the Temperature Limit VI, it should move to the
center of the screen and look similar to the figure below.

62
Implementation
Set Appearance Properties
1. Open C:\Exercises\LabVIEW Core 2\Customize the VI Window\Cuztomize the VI
Window.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine - VI Window
Customization VI.
3. Modify the block diagram as shown in figure below to hide the scroll bars, menu bar, and tool bar,
and center the front panel on the screen while the VI is running.

63
1. Property Node – Press the <Ctrl-Space> keys to display the Quick Drop dialog box. Search and place Property Node. Right-click the
Property Node and select Link to» Pane.
▪ Right-click and select Change All to Write.
▪ Expand the node to display two properties and set them to Horizontal Scroll Bar Visibility and Vertical Scrollbar Visibility.
2. Off While Running constant – Right-click one of the inputs to the Pane Property Node and select Create» Constant.
3. Property Node – Right-click the Property Node and choose Select Class» VI Server» VI» VI.
▪ Right-click and select Change All to Write.
▪ Expand the node to display two properties.
▪ Click the top property and select Select Property» Front Panel Window» Show Menu Bar.
▪ Click the lower property and select Select Property» Tool Bar» Visible.
4. Invoke Node – You must wire the reference from the VI Property Node before setting this method. Click Method and select Select
Method» Front Panel» Center.

64
Notice that the scrollbar visibility properties apply to the Pane class, not the VI class. The front
panel can be split into multiple panes.

4. Save the VI.

Test
1. Run the VI.
2. Verify that the scroll bars, tool bar, and menu bar are not displayed, and that the front panel
window is centered on the screen while the VI runs.
3. Stop and close the VI.
End of Exercise 3-2

65
Exercise 4-1: Managing Configuration Settings Using an INI File
Goal
▪ Modify a DAQmx-based application to load its hardware configuration settings from an INI file.
Instructions
Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario
You developed an application that acquires and generates data, analyzes that data, and writes to a log
file when the analysis determines that the values are out of range.
After using this application for some time, you decide that you would like to use it with a variety of DAQ
hardware, without maintaining separate copies of the code to handle different settings. Instead, you
decide to use INI files to store the settings needed for each type of hardware that you work with. That
way, you can change the hardware settings by choosing a different INI file.
Design
You will develop a utility to create new INI files. Default.ini should contain these settings for your analog
input device:
▪ AI Channel = “PCI-6221/ai0”
▪ Sample Rate = 1000
▪ Samples per Channel = 100
Default.ini should contain these settings for your analog output device:
▪ AO Channel = “PCI-6221/ao1”
You will modify the Initialize case sub diagram of your application to load the values from Default.ini.
You will add a button to the user interface to allow the user to load settings from a different INI file. When
the user clicks that button, the application should prompt them to choose an INI file to load.

Guided Instructions
1. Open C:\Exercises\LabVIEW Core 2\Loading and Storing Configuration\Load
Config Settings.lvproj.

2. Create a VI that generates an INI file.


▪ From the Project Explorer window, open the Write Config Data VI from the support
folder.

66
▪ Modify the block diagram as shown in the following figure.

1. File Dialog VI – Specify the path and name of the INI file that you want to create or modify.
2. Open Config Data VI – Creates or opens the INI file.
3. Write Key VI – Specify the section, key, and value that you want to write to the INI file. This VI automatically adapts to whatever data
type you wire to the value input.
4. Close Config Data VI – Closes the INI file.

67
3. Save the VI.
4. Specify values to controls on the front panel.

5. Run the VI.


▪ Name the file Default.ini in Choose or Enter Path of the INI File window.
▪ Save it in the same directory that the .lvproj file resides in.
▪ When the VI stops, open Default.ini and review the format of the INI file.

- Values are written to two sections: Analog Input and Analog Output.
- Each section contains one or more keys with their associated values.

6. Create a VI that reads configuration data from an INI file.


▪ From the Project Explorer window, open Read Config Data VI from the support folder.

68
▪ Modify the block diagram as shown in the following figure.

1. Open Config Data VI – Opens the specified INI file.


2. Read Key VI – Specify the section and key that you want to read from the INI file. Use the Polymorphic VI Selector to specify the data
type that the function will read from the INI file. The values for the section and key constants must match the values in the INI file
exactly.
3. Close Config Data VI – Closes the INI file.
4. Hardware Settings Type Definition and Bundle By Name function – Place the Hardware Settings type definition on the block
diagram from the Project Explorer window and wire the Read Key VIs’ outputs as shown in the picture above using the Bundle By
Name function.

69
7. Save the VI.

8. Verify that the front panel looks similar to the following figure.

70
9. Verify the icon and terminals, as shown in the following figure.

10. Test the read config file VI.


▪ On the front panel, browse the configuration file in control and navigate to C:\Exercises\LabVIEW Core 2\Loading
and Storing Configuration\Default.ini.
▪ Run the VI.
▪ Verify that each indicator is populated with the value that you specified in the INI file.
11. From the Project Explorer window, open the Event-Driven State Machine – Load Configuration VI.
12. Modify the Event-Driven State Machine – Load Configuration VI as shown in the following figure, to load values from Default.ini each
time the VI starts running.

71
1. Application Directory, Build
Path functions and Check if File
or Folder Exists VI – Use these
tools to determine if a Default.ini
file exists in the same directory as
the LabVIEW project file (.lvproj)
file for this application.
2. Case Structure – If the
Default.ini file exists, then the
application executes the True
case, which uses the Read Config
Data VI to read the configuration
data from the INI file and store the
configuration data into the
Hardware Settings shift register.
If the Default.ini file does not exist,
then the application executes the
False case and use the default
values that initialized the Hardware
Settings shift register. The False
case passes the cluster and error wires through, as shown in the following figure.

72
13. Modify the front panel to include a Select Config File button that the user can click to select and load an INI file containing different
DAQ configuration settings.

73
74
14. Modify the Wait for Event case to handle the event when the user clicks on the Select Config File button.

1. “Select Config File”: Value


Change event – Select the event
structure border and add an
event case.
2. File Dialog VI – Use this VI to
launch a file dialog for the user to
select an existing configuration
file (.ini). In the Configure File
dialog window, set the options to
File and Existing. Use the
Application Directory VI to set the
start path of the file dialog to the
directory containing the INI file.
3. Case Structure – Wire the
exists output of the File Dialog VI
to the Selector input of the Case
structure.
If the user selects a file that
exists, the Case structure
executes the True case, which
uses the Read Config Data subVI
to read the configuration data
from the INI file and stores the
configuration data into the Hardware Settings shift register.
If the user selects a file that does not exist or clicks the Cancel button, the Case structure executes the False case, which continues to
use the previously-stored hardware settings. The False case also uses the Clear Errors VI to clear Error 43, which the File Dialog VI
passes out if the user clicks the Cancel button. This ensures that a user clicking Cancel in a file dialog will not cause the application to
exit. You will learn more about the Clear Errors VI and error handling strategies in a later lesson.

75
▪ Wire the False case as shown in the following figure.

▪ The False case, which executes when the user cancels, passes the existing cluster values through and wires an empty error to
the output terminal. Canceling Choose or Enter Path of File dialog box should not cause the application to halt or exit.

76
15. Run the Event-Driven State Machine – Load Configuration VI.
▪ Use execution highlight and probes to verify that the VI is loading configuration data from the INI file.
▪ Examine the values passed in the following sections of code.

Case Sub-diagram Expected Behavior


Verify that the Read Config Data VI reads correct
Initialize Case
values if a Default.ini file exists in the LabVIEW
project directory.
Click the Acquire button. Verify that the Acquire
Acquire Case
case outputs the AI Voltage Channel, Sample Rate
(Hz), and Samples per Channel values specified in
the INI file.
Click the Generate Stimulus button. Verify that the
Update Stimulus case
Update Stimulus case outputs the correct AO
Voltage Channel value specified in the INI file.
Click the Select Config File button. Choose the
Wait for Event»”Select
Default.ini file. Verify that the Read Config Data VI
Config File”: Value Change
reads the correct values from the INI file.
event case
Click the Select Config File button again. In the file
dialog box, click the Cancel button. Verify that the
Case structure executes the False case, passes the
previous shift register cluster data, and clears Error
43.

▪ Click Exit to stop the VI.

Challenge
▪ Create additional INI files with different settings and observe the effects of those settings on the acquired data. For example, use a
different AI Channel, such as PCI-6221/ai2:3, and a different number of Samples per Channel, such as 500.

77
▪ Modify the code to handle the situation where the INI file is missing one or more keys.
▪ Modify the Initialize case to programmatically create a new Default.ini file with default settings if Default.ini does not exist.

On the Job

Would any of your applications benefit from using a configuration file?


If so, write a draft below of the contents for your configuration file. For example, if you will use an INI file, write down the Section names, Key
names, and Key values.
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
________________________________________________________________________________________________________________
__________________________________________________________________

End of Exercise 4-1

78
Exercise 5-1: Inject Errors
Goal:
▪ Test how an application handles an error by injecting an error.
Hardware Setup:
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-6212 multifunction I/O device paired
with the BNC-2120 shielded connector block. Analog Input 2 should be connected to the Sine/Triangle BNC connector. Analog Input 3 should
be connected to the TTL Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Instructions:
1. Open C:\Exercises\LabVIEW Core 2\Inject Error\Inject Error.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine - Inject Error VI.
3. Run the VI.
4. Double-click the text in the AI Voltage Channel control and set it to Undefined_PCI-6221/ai0:8.
5. Click the Acquire button. This will cause an error in the Acquire case of the state machine because Undefined_PCI-6221/ai0:8 does
not exist in your system.
6. How does the application respond to this error?
_______________________________________________________________________________________________________________________
_______________________________________________________________________________________________________________________
_______________________________________________________________________________________________________________________
_________________________________________________________________________

Notice that the application responded to the error by displaying the following error dialog, which explains the error. After you click
the Continue button, the application stops executing and exits.

79
80
Simulate an error occurring by injecting the corresponding error cluster in the application code
If you want to test how your application handles a specific error occurring at a specific location, you can inject the corresponding error cluster
at that location in your application.
1. On the block diagram, go to the Acquire case of the state diagram.

1. Error Cluster From Error Code VI – Right-click the error code input and select Create» Constant. Set the constant to -200220.
Use the Context Help to learn the details of this VI.

2. Run the VI.


3. Click the Acquire button. This will cause Error -200220 in the Acquire case of the state machine because of your modifications to the
code.
4. Using the Error Cluster From Error Code VI to inject an error is another way of testing how your application responds to a specific
error.
On the Job

81
What errors do you need to inject into your application to test how your application handles errors?
___________________________________________________________________________

End of Exercise 5-1

82
Exercise 5-2: Handle Specific Errors Immediately
Goal:
Handle a specific error immediately by asking the user to retry an action.
Hardware Setup:
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-6212 multifunction I/O device paired
with the BNC-2120 shielded connector block. Analog Input 2 should be connected to the Sine/Triangle BNC connector. Analog Input 3 should
be connected to the TTL Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Scenario:
In Exercise5-1, you tested how this application responds to the user entering an invalid DAQmx channel. The application manages it by
displaying an error dialog and exiting.
In this exercise, you want to define how the application handles this error.
The application will display a “AI Voltage Channel is invalid. Please enter valid channel(s)” dialog box to the user and then allow the user to
re-enter a different channel.
Instructions:
Handling an Error Locally:
1. Open C:\Exercises\LabVIEW Core2\Handle Error Locally\Handle Error Locally.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine – Handle Error Locally VI.
3. On the block diagram, go to the Acquire case of the state diagram.
4. Modify the block diagram as shown in the following figure, to perform a finite acquisition if the DAQmx Create Channel VI does not
output Error -200220.

83
1. Clear Errors VI – Clears a specific error or warning from the error wire if it matches a code you specify. Use the context help to learn
more about this VI.
2. Case Structure – If the Clear Errors VI did not find error -200220 in the error wire, then perform a finite acquisition.

Note: To make the current case the False case, right-click the border of the Case structure and select Make This Case False.

5. Modify the block diagram as shown in the following figure, to locally handle Error -200220 by displaying an instructive dialog to
the user and going back to the Wait for Event state.

84
1. Case Structure – Select the True case.
2. One Button Dialog – Display a dialog to the user to specify a valid channel. Right-click the message input and select Create»
Constant. Set the constant to AI Voltage Channel is invalid. Please enter valid channel.
3. States Enum Constant (type definition) – Right-click the blue enum output tunnel on the Case structure and select Create»
Constant. Set the constant to Wait for Event. If Error -200220 occurs, there is no need to go to the Analyze state, so the application
should go back to the Wait for Event state instead.

6. Examine the new error handling behavior of the application.


▪ Run the VI.
▪ Set the AI Voltage Channel control to a valid channel, such as PCI-6221/ai0, PCI-6221/ai2:3.
▪ Click the Acquire button. The VI should run.
▪ Double-click the text in the AI Voltage Channel control and set it to an invalid channel, such as Undefined_PCI-
6221/ai0:8.

85

Click the Acquire button. This will cause an error in the Acquire case of the state machine because Undefined_PCI-6221/ai0:8
does not exist in your system.
▪ Verify that the application handles this error by displaying a dialog to the user and allowing the user to enter the AI Voltage
Channel again.
▪ Click the Exit button to stop the VI when finished.
7. Examine the error handling behavior of this application for an error that is not handled locally.
▪ Run the VI.
▪ Double-click the text in the AO Voltage Channel control and set it to an invalid channel, such as Undefined_PCI-6221/ao1.
▪ Click the Generate Stimulus button.
▪ This will cause an error in the Update Stimulus case of the state machine because Undefined_PCI-6221/ao1 does not exist in
your system.
▪ Because the VI block diagram does not have any local error handling in the Update Stimulus case of the Case structure, this VI
handles the error by exiting the While Loop, displaying an error dialog, and stopping the VI.
Preventing Incorrect Behavior
In the Analyze case in this application, the Analyze subVI calculates the RMS values of the acquired data and compares them to the
Threshold (rms) control value. However, because the calculated RMS values will always be zero or positive values, it does not make sense
for the Threshold (rms) control to pass in a negative value.
To prevent a potential user error or confusion, edit the Threshold (rms) control to have a minimum value of 0, to prevent the user from
entering a negative value.
1. On the front panel, right-click the Threshold (rms) control and select Properties.
2. In the Data Entry tab, uncheck the Use Default Limits checkbox, then set Minimum to 0 and select Coerce under the Response to
value outside limits drop-down list.
3. Test the new behavior.
▪ Try setting the Threshold (rms) control to a negative value, such as -5.
▪ Notice that the Threshold (rms) control coerces a negative value to 0.
Your Turn
1. Modify this VI to also handle an error of invalid AO Voltage Channel locally by displaying an instructive dialog of “AO Voltage
Channel is invalid. Please enter a valid analog output channel” to the user.
___________________________________________________________________________________________________________
___________________________________________________________________________________________________________
_________________

86
2. To view the solution, refer to [Your Turn Solution] Event-Driven State Machine - Handle Error Locally VI in the
C:\Solutions\LabVIEW Core 2\Exercise 5-2 directory.
___________________________________________________________________________________________________________
___________________________________________________________________________________________________________
_________________
On the Job

1. Would any of your applications benefit from handling a specific error locally?
___________________________________________________________________________________________________________
_______________________________________________

2. If so, what specific errors do you want to handle locally?


___________________________________________________________________________________________________________
_______________________________________________

3. Describe how you would handle each of those errors (Ignore error, retry action, fix source of error, etc.).
___________________________________________________________________________________________________________
___________________________________________________________________________________________________________
_________________
___________________________________________________________________________________________________________
___________________________________________________________________________________________________________
_________________
End of Exercise 5-2

87
Exercise 5-3: Create an Execution Log File
Goal:
▪ Explore how an example application logs timestamps, state information, and error information to
an execution log file.
Hardware Setup:

(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.
Instructions:
Explore Example Execution Log Files
1. In Windows Explorer, go to the C:\Exercises\LabVIEW Core 2\Execution Log directory.
2. Explore an example of an execution log where an error occurs.
▪ Double-click [Error Example] YYYYMMDD_HHMMSS_ExecutionLog.txt to view its
contents.
▪ By looking at this execution log, you can see the error information at the end of the file.
You can also see the timing, states, and state data that led to this error occurring in the
application.
▪ Close the file when finished

88
89
3. Explore an example of an execution log where no errors occur.
▪ Double-click [Success Example] YYYYMMDD_HHMMSS_ExecutionLog.txt to view its
contents.
▪ In this example execution log, you know that no errors occurred because the end of the
log file explicitly logs Success when the application exited.
▪ The information in the execution log allows you to have an idea of what happened during
this run of the application.
▪ Close the file when finished.

90
Explore Example Code That Creates Execution Log Files
1. Open C:\Exercises\LabVIEW Core 2\Execution Log\Execution Log.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine (Execution Log)
VI.Move all the control references into the “Acquire”: Value Change event case.
3. Explore the block diagram.

91
1. Open/Replace/Create File – Creates the execution log file.
2. Scroll through every state in the state machine. Notice that every state contains code that writes timestamp, the current state, and
state data to the execution log file.
3. If an error has occurred, this part of the VI outputs a string containing the error information. If no errors have occurred this part of the
VI outputs a string containing Success!.
4. Write to Text File – Writes this text to the execution log file.
5. Close File – Closes the file.

92
4. Generate an error log with no errors.
▪ Run the VI.
▪ Click a few buttons without causing an error.
▪ Click the Exit button to stop the VI.
▪ Open the new execution log created in the C:\Exercises\LabVIEW Core
2\ Execution Log directory. Notice that the last line of the execution log is Success!.
▪ Close the execution log file.
5. Generate an error log with an error.
▪ Run the VI.
▪ Double-click the text in the AI Voltage Channel control and set it to
Undefined_cDAQ1Mod8/ai0:8.
▪ Click the Acquire button. This will cause an error in the Acquire case of the state machine
because Undefined_cDAQ1Mod8/ai0:8 does not exist in your system. Click Continue on
the error dialog window. The VI now exits.
▪ Open the new execution log created in the C:\Exercises\LabVIEW Core
2\Execution Log directory. Notice that the last lines of the execution log contain the
error information. Notice that the Acquire state information before the error contains the
incorrect AI Voltage Channel value of Undefined_cDAQ1Mod8/ai0:8.
▪ Close the execution log file.
6. Close the VI and project when finished.
On the Job
1. Would any of your applications benefit from creating an execution log? If so, what information
should you write to the execution log?
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____________________________________________________________________________
_____

End of Exercise 5-3

93
Exercise 6-1: Preparing Files for Distribution

Goal
Review the Building Applications Checklist and prepare VIs to build a stand-alone application.

Scenario
Review the Building Applications Checklist to assist you in the build process before creating a stand-
alone application or installer.
Stand-alone applications in LabVIEW have the Window Appearance set to Top-level application
window to open the VI front panel when the application runs.
A VI that runs as a stand-alone application remains in memory when the application finishes running.
Therefore, it is necessary to call the Quit LabVIEW function to close the application when the application
finishes executing. Placing the Quit LabVIEW function on the block diagram can make editing the
application more difficult in the future because LabVIEW exits each time the application finishes
executing.

Design
▪ Modify the VI Properties to prepare to build a stand-alone application.
▪ Modify the application to call the Quit LabVIEW function when the code is executed in the run-
time system.
▪ Modify the application to specify a log path relative to the stand-alone application.
Before you build an application, you first prepare the code so that it executes reliably when compiled into
an application.

Review the Building Applications Checklist


1. Select Help» LabVIEW Help to open the LabVIEW Help.
2. Select Fundamentals» Building and Distributing Applications» Developing and Distributing
an Application.
3. Review the Preparing to Build the Application section.

Guided Instructions
Set the Top-Level Application Window
1. Open C:\Exercises\LabVIEW Core 2\Deployment\Event-Driven State
Machine.lvproj.
2. From the Project Explorer window, open the Event-Driven State Machine - Main VI.
3. Select File» VI Properties to display the VI Properties dialog box.
4. Select Window Appearance from the Category pull-down menu.
5. Uncheck the Same as VI name checkbox, and enter a name, such as Event-Driven
State Machine, in the Window title text box.
6. Select Top-level application window to give the front panel a professional appearance
when the VI opens as a stand-alone application.

94
7. Click the Customize button to view the various window settings that LabVIEW provides for
configuring the top-level application windows.
8. Click OK to close the Customize Window Appearance dialog box and click OK to close the
VI Properties dialog box.
9. Save the VI.

Call the Quit LabVIEW Function


1. Open and modify the block diagram to call the Quit LabVIEW function when the application
finishes. The Quit LabVIEW function exits LabVIEW and closes the application after it has
executed.

1. Conditional Disable Structure- Place a Conditional Disable Structure after the Simple
Error Handler VI from the Quick Drop window. Wire the error out of the Simple Error
Handler VI to the border of the structure. Right-click the border of the structure and select
Add Sub-diagram After. In the Configure Condition dialog, set the value of Symbol(s)
to Run_Time_Engine. Set Value(s) to True.
2. Quit LabVIEW Function- Place this function in the “RUN_TIME_ENGINE==True” sub-
diagram. You can leave the Default sub-diagram empty.

2. In the Project Explorer window, select File» Save All to save all the VIs.

Specify a File Path Relative to the Application


The Event-Driven State Machine - Main VI already contains code to specify a relative path to the
application.
Open the Generate Timestamped VI in the Log case of the loop. The Application Directory VI creates a
path relative to the stand-alone application when you call the VI from a stand-alone application.
Otherwise, the Application Directory VI returns the path to the folder containing the project file.

Test
1. Run the Event-Driven State Machine - Main VI to ensure that it is working.
2. Save the VI and the project.

End of Exercise 6-1

95
Exercise 6-2: Create and Debug a Stand-Alone Application

Goal
Create a build specification, build a stand-alone application (EXE) in LabVIEW, and debug the
application running on the local computer.

Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.

Scenario
Create a stand-alone application to run the Event-Driven State Machine - Main VI. After you prepare your
files, you create an Application (.exe) Build Specification, and run the application. You then use LabVIEW
to debug the running application.

Design
Use the Application (EXE) Build Specifications to create a stand-alone application for the event-driven
state machine application.
Connect with the running application by creating a debugging session in LabVIEW.

Review the Building Applications Checklist


1. Select Help» LabVIEW Help to open the LabVIEW Help.
2. Select Fundamentals» Building and Distributing Applications» Developing and Distributing
an Application.
3. Review the Configuring Specifications for a Built Application list of steps.

Guided Instructions
Creating an Application (EXE) Build Specification
1. Open C:\Exercises\LabVIEW Core 2\Creating an Executable\Event-Driven
State Machine.lvproj.
2. Right-click Build Specifications in the Project Explorer window and select New» Application
(EXE) from the shortcut menu.
3. (Optional) Place a checkmark in the Do not prompt again for this operation checkbox and click
the OK button if you receive a prompt about SSE2 optimization.
4. Modify the filename of the target and destination directory for the application in the Information
page.
▪ Select the Information page.
▪ Change the Target filename to Event-Driven State Machine.exe.
▪ Enter C:\Exercises\LabVIEW Core 2\Creating an Executable\Executable
in the Destination directory text box.

96
Tip You do not need to create the directory. LabVIEW creates any directories that you specify.
5. Specify the top-level VI for the application.
▪ Select the Source Files page.
▪ Select the Event-Driven State Machine - Main VI in the Project Files tree.
▪ Click the right arrow next to the Startup VIs list box to add the selected VI to the Startup
VIs list box.
6. Include code to allow debugging of the application.
▪ Select the Advanced page.
▪ Place a checkmark in the Enable debugging checkbox.
▪ Click OK.
7. In the Project Explorer window, select File» Save All.
8. In the Project Explorer window, right-click the My Application build specification and select Build
from the shortcut menu.
9. Click Done in the Build status window.

Running the Application


1. Close the Project Explorer window and exit LabVIEW.
2. Navigate to C:\Exercises\LabVIEW Core 2\Creating an Executable\Executable
in Windows Explorer.
3. Run Event-Driven State Machine.exe.
▪ Click the Acquire button.
▪ Click the Exit button when done.
4. Verify that the application closed when you stopped the application and the application created a
text file in the Executable folder.

Debugging the Application on the Same Computer


1. Launch LabVIEW.
2. Run the Event-Driven State Machine.exe.
3. Select Operate» Debug Application or Shared Library from the LabVIEW window.
4. Enter localhost in the Machine name or IP address text box.
5. Select Event-Driven State Machine.exe from the Application or shared library drop-down
menu.
▪ Click the Refresh button if Event-Driven State Machine.exe does not appear in the list.
6. Click the Connect button to create the debugging connection.
7. Start debugging the running application.
▪ Open the block diagram.
▪ Turn on execution highlighting.
▪ Try using probes, breakpoints, and single-stepping.
8. Click the Stop button in the debugging window to stop the application.

End of Exercise 6-2

97
Exercise 6-3: Create an Installer

Goal
Create an installer build specification and build the installer. As a challenge, remotely debug the
application created by the installer.

Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.

Scenario
Creating an installer simplifies deploying an application to multiple machines. After you have prepared
your files, you create an Application (.exe) Build Specification and then create an Installer Build
Specification.

Design
Use an Installer Build Specification to create an installer for the Application (.exe) Build Specification.
Warning: To successfully build an installer, make sure that your exercise source files are placed in the
appropriate location, that is — C:\Exercises\ LabVIEW Core 2\”name of the current
exercise”.

Guided Instructions
Create an Installer Build Specification
1. Open C:\Exercises\LabVIEW Core 2\Creating an Installer\Event-Driven
State Machine.lvproj.
2. Right-click Build Specifications in the Project Explorer window and select New» Installer from
the shortcut menu.
3. Modify the installer destination in the Product Information page.
▪ Select the Product Information page.
▪ Type C:\Exercises\LabVIEW Core 2\Creating an Installer\Installer as
the Installer destination.
4. Specify the Installer Build Specification.
▪ Click the Source Files page.
▪ Select My Application under the Build Specification folder.
▪ Expand Program Files and select Event-Driven State Machine in the Destination View
tree.

98
▪ Click the right arrow next to the Project Files View tree to place the event-driven state
machine application and all application support files under Program Files» Event-Driven
State Machine in the Destination View tree as shown in figure below.

5. Add the NI LabVIEW Run-Time Engine to the installer by modifying the Additional Installers
page.
▪ Select the Additional Installers page.
▪ Select the NI LabVIEW RunTime 2020 and NI-DAQmx Runtime 20.0 installers (or later
versions), if they are not already automatically selected.
▪ Click OK.
Note: Some additional installers you might select in the Additional Installers page, can
require you to have downloaded the product installation package before you can include
the product installer in your application installer. To download an installation package, go
to ni.com/info and enter the code downloads.
6. In the Project Explorer window, right-click the My Installer build specification and select Build
from the shortcut menu.
7. Click Done when LabVIEW finishes building the installer.
8. Save and close the Project Explorer window and close LabVIEW.

99
Test
1. Run the install.exe file in the C:\Exercises\LabVIEW Core 2\Creating an
Installer\Installer\Volume directory.
2. Follow the instructions on-screen to install the application. By default, the application is created
inside the C:\Program Files (x86)\Event-Driven State Machine directory.
3. From the Start menu, navigate to Start» Event-Driven State Machine or from the Windows
Explorer, navigate to C:\Program Files (x86)\Event-Driven State Machine
directory.
4. Right-click Event-Driven State Machine and select Run as administrator.
Note: You must run the application as an administrator because the application creates a
file in the <Program Files> folder. If you do not run the program as an administrator,
Error 8 occurs.
5. Click Yes in the dialog box asking for permission to make changes to the computer.
6. Test the application by clicking the Acquire button, Clean button, and the Exit button.

Challenge (Optional)
If you have Internet access during class, try to debug the application on a remote computer.
1. Verify that classroom has Internet access.
2. Decide whether to debug a classmate’s application or install your application on your classmate’s
computer.
3. If you decide to debug your own application on a remote computer you must distinguish your
application from the applications already on your classmate’s computer.
▪ In the installer build specification, rename your application with a unique name.
▪ Transfer your installer to the remote computer using a USB flash drive or the network.
▪ Install your application.
4. To use LabVIEW on your computer to debug a running application on a remote computer, you
must determine the IP address of the remote computer, also known as the destination computer.
Note: Consider your computer to be the development computer and your classmate’s
computer to be the destination computer.
▪ Open the Windows Start menu on the destination computer.
▪ Enter cmd in the search box and press the <Enter> key.
▪ Type ipconfig at the prompt in the Command window and press the <Enter> key.
▪ Note the IP address.
5. Run the application on the destination computer.
6. On the development computer, launch LabVIEW, if necessary.
7. Select Operate» Debug Application or Shared Library from the LabVIEW menu.
8. Enter the IP address of the destination computer in the Machine name or IP address text box.
9. Select the application from the Application or shared library drop-down menu.
▪ Click the Refresh button if the application you want does not appear in the list.
10. Click the Connect button to create the debugging connection.
11. Start debugging the running application.
▪ Open the block diagram.

100
▪ Turn on Execution Highlighting.
▪ Try using probes, breakpoints, and single-stepping.
12. Stop the application by clicking the Stop button in the debugging window.
Note: Refer to the LabVIEW Continuous Integration Tutorial (Jenkins/GitHub) on ni.com
for more details.

End of Exercise 6-3

101
Exercise 6-4: Creating a Package

Goal
Create a package for distributing the stand-alone application.

Hardware Setup
(Hardware) In the exercises where we work with Analog Input/Output channels, we use PCI-6221/USB-
6212 multifunction I/O device paired with the BNC-2120 shielded connector block. Analog Input 2 should
be connected to the Sine/Triangle BNC connector. Analog Input 3 should be connected to the TTL
Square Wave BNC connector. The Sine/Triangle waveform switch should be set to Sine.

Scenario
Creating a package allows deploying an application (i.e. source distribution, packed project library,
shared library, .NET assembly, and/or application) to clients through NI Package Manager or
SystemLink.

Design
Use a package build specification to create a package that includes the application for distribution.
Warning: To successfully build a package, make sure that your exercise source files are placed in the
appropriate location, that is — C:\Exercises\ LabVIEW Core 2\”name of the current
exercise”.

Guided Instructions
Creating a Package Build Specification
1. Open C:\Exercises\LabVIEW Core 2\Creating a Package\Event-Driven State
Machine.lvproj.
2. Right-click Build Specifications in the Project Explorer window
3. Select New» Package from the shortcut menu.
4. Modify the package information.
▪ Select the Information page.
▪ Set the package build specification name under the Build specification name section
and filename of the package under the Package name section of the page.
▪ Type C:\Exercises\LabVIEW Core 2\Creating a Package\Package as the
Package output directory to specify the package build directory.
5. Specify the install destination under the Destinations page.
▪ Select the Destinations page.
▪ Select Program Files» Event-Driven State Machine in the Destination tree. Modify the
Destination name, if needed.
6. Specify the source files to include in the package on the Source Files page.
▪ Click the Source Files page.
▪ Select the My Application build specification.
▪ Select Program Files» Event-Driven State Machine in the Destination View tree.

102
▪ Click the right arrow next to the Project Files View tree to place the Event-Driven State
Machine application and all application support files under Program Files» Event-Driven
State Machine in the Destination tree as shown in the figure below

7. Manage the shortcuts created during the package installation.


▪ Select the Shortcuts page
▪ Under Shortcuts tree, select the Event-Driven State Machine, and optionally rename it
under the Name box. You can select the type of the shortcut under the Directory drop-
down menu - Program Menu for Start menu shortcut, Public Desktop for Desktop
shortcut and Startup for the application to run automatically on startup. For this example,
select the Public Desktop option.
8. Configure the dependencies for a package.
▪ Select the Dependencies page.
▪ Select the LabVIEW Runtime (32-bit) and NI-DAQmx Runtime packages, if they are not
already automatically selected.
▪ You can specify the dependency relationship with the package you select in the Related
packages listbox by selecting the desired dependency relationship under the
Dependency relationship drop-down menu.

103
9. Under the Package Installer page you can specify whether or not to build your distribution into a
package installer file (.exe). You can distribute your application to clients using the package
installer. By default, the package installer includes all the package dependencies in the output so
that your clients can install the package installer without network access.
10. Under the Feed page you can add your package to a feed, so that your clients can subscribe to
the feed to receive update notifications and install the package from NI Package Manager or NI
SystemLink via network access.
11. Click OK.
12. In the Project Explorer window, right-click the My Package build specification and select Build
from the shortcut menu.
13. Click Done when LabVIEW finishes building the package.
14. Save and close the Project Explorer window and close LabVIEW.

Test
1. Navigate to C:\Exercises\LabVIEW Core 2\Creating a Package\Package in Windows
Explorer.
2. Double-click the event-driven-state-machine_1.0.0-0_windows_all.nipkg.
3. When the User Account Control dialog box appears, click Yes to grant the NI Package Manager
administrator rights.
4. Click Next to progress the installation.
5. Click Close to exit setup.
6. Notice that there is a new folder on the desktop called Event-Driven State Machine.
7. Inside it, you will find the Event-Driven State Machine.exe shortcut, which links to the
C:\Program Files (x86)\Event-Driven State Machine directory.
8. Right-click Event-Driven State Machine.exe and select Run as administrator.
9. Test the application by clicking the Acquire button, Clean button, and the Exit button.
Note: Your package is visible in the NI Package Manager. To find it, launch the NI
Package Manager, then under the INSTALLED tab remove the checkmark from the
Products only checkbox and search for your package. Here you can Remove or Repair
the installed package.

End of Exercise 6-4

104

You might also like