LabVIEW TM Core 2 Course Manual-21-30
LabVIEW TM Core 2 Course Manual-21-30
Refer to the Caveats and Recommendations when Using Events in LabVIEW topic of the LabVIEW
Help for more caveats and recommendations when you use events in LabVIEW.
Self-Review: Quiz
1. Which of the following buffer data?
a. Queues
b. Events
c. Local Variables
4. The Event structure handles only one event each time it executes.
a. True
b. False
Get Queue Status Determines the number of elements currently in the queue
4. The Event structure handles only one event each time it executes.
a. True
b. False
Notes
1-18 | ni.com
Implementing Design Patterns
2
You can develop better programs in LabVIEW and in other programming languages if you follow
consistent programming techniques. Design patterns represent techniques that have proved
themselves useful time and time again. To facilitate development, LabVIEW provides templates
for several common design patterns. This lesson discusses two different categories of
programming design patterns—single loop and multiple loops.
Single loop design patterns include the simple VI, the general VI, and the state machine.
Multiple loop design patterns include the parallel loop VI, the master/slave, and the
producer/consumer.
Understanding the appropriate use of each design pattern helps you create more efficient
LabVIEW VIs.
Topics
A. Design Patterns
B. Simple Design Patterns
C. Multiple Loop Design Patterns
D. Error Handlers
E. Generating Error Codes and Messages
F. Timing a Design Pattern
G. Functional Global Variable Design Pattern
A. Design Patterns
Application design patterns represent LabVIEW code implementations and techniques that are
solutions to specific problems in software design. Design patterns typically evolve through the
efforts of many developers and are fine-tuned for simplicity, maintainability, and readability.
Design patterns represent the techniques that have proved themselves useful over time.
Furthermore, as a pattern gains acceptance, it becomes easier to recognize—this recognition alone
helps you to read and make changes to your code.
Figure 2-1 displays the block diagram of the Determine Warnings VI that was the course project in
the LabVIEW Core 1 course. This VI performs a single task—it determines what warning to output
dependent on a set of inputs. You can use this VI as a subVI whenever you must determine the
warning level.
Notice that the VI in Figure 2-1 contains no start or stop actions from the user. In this VI all block
diagram objects are connected through data flow. You can determine the overall order of operations
by following the flow of data. For example, the Not Equal function cannot execute until the Greater
Than or Equal function, the Less Than or Equal function, and both Select functions have executed.
Figure 2-1. Simple VI Architecture
2-2 | ni.com
LabVIEW Core 2 Course Manual
In Figure 2-2, the error cluster wires control the execution order of the three sections. The While
Loop does not execute until the Start Up VI finishes running and returns the error cluster data.
Consequently, the Shut Down VI cannot run until the main application in the While Loop finishes
and the error cluster data leaves the loop.
Tip Most loops require a Wait function, especially if that loop monitors user input
on the front panel. Without the Wait function, the loop might run continuously and
use all of the computer system resources. The Wait function forces the loop to run
asynchronously even if you specify 0 milliseconds as the wait period. If the operations
inside the main loop react to user inputs, you can increase the wait period to a level
acceptable for reaction times. A wait of 100 to 200 ms is usually good because most users
cannot detect that amount of delay between clicking a button on the front panel and the
subsequent event execution.
For simple applications, the main application loop is obvious and contains code that uses the simple
VI design pattern. When the application incudes complicated user interfaces or multiple tasks such
as user actions, I/O triggers, and so on, the main application phase gets more complicated.
A state machine in LabVIEW consists of a While Loop, a Case structure, and a shift register. Each
state of the state machine is a separate case in the Case structure. You place VIs and other code that
the state should execute within the appropriate case. A shift register stores the state that should
execute upon the next iteration of the loop. The block diagram of a state machine VI with five states
appears in Figure 2-3. Figure 2-4 shows the other cases, or states, of the state machine.
Figure 2-3. State Machine with Startup State
2-4 | ni.com