0% found this document useful (0 votes)
42 views10 pages

LabVIEW TM Core 2 Course Manual-21-30

Uploaded by

陳彥勳
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views10 pages

LabVIEW TM Core 2 Course Manual-21-30

Uploaded by

陳彥勳
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

LabVIEW Core 2 Course Manual

Caveats and Recommendations


The following list describes some of the caveats and recommendations to consider when
incorporating events into LabVIEW applications.
• Avoid using an Event structure outside a loop.
LabVIEW can generate events even when no Event structure is waiting to handle them.
Because the Event structure handles only one event each time it executes, place the Event
structure in a While Loop that terminates when the VI is no longer interested in events to ensure
that an Event structure handles all events that occur.
• Remember to read the terminal of a latched Boolean control in its Value Change event case.
When you trigger an event on a Boolean control configured with a latching mechanical action,
the Boolean control does not reset to its default value until the block diagram reads the terminal
on the Boolean control. You must read the terminal inside the event case for the mechanical
action to work correctly.
• Avoid placing two Event structures in one loop.
National Instruments recommends that you place only one Event structure in a loop. When an
event occurs in this configuration, the Event structure handles the event, the loop iterates, and
the Event structure waits for the next event to occur. If you place two Event structures in a
single loop, the loop cannot iterate until both Event structures handle an event. If you have
enabled front panel locking for the Event structures, the user interface of the VI can become
unresponsive depending on how the user interacts with the front panel.

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.

© National Instruments | 1-13


LabVIEW Core 2 Course Manual

Self-Review: Quiz
1. Which of the following buffer data?
a. Queues
b. Events
c. Local Variables

2. Match the following:

Obtain Queue Destroys the queue reference

Get Queue Status Assigns the data type of the queue

Release Queue Adds an element to the back of the queue

Enqueue Element Determines the number of elements currently in the queue

3. Which of the following are valid data types for queues?


a. String
b. Numeric
c. Enum
d. Array of Booleans
e. Cluster of a String and a Numeric

4. The Event structure handles only one event each time it executes.
a. True
b. False

© National Instruments | 1-15


LabVIEW Core 2 Course Manual

Self-Review: Quiz Answers


1. Which of the following buffer data?
a. Queues
b. Events
c. Local Variables

2. Match the following:

Obtain Queue Assigns the data type of the queue

Get Queue Status Determines the number of elements currently in the queue

Release Queue Destroys the queue reference

Enqueue Element Adds an element to the back of the queue

3. Which of the following are valid data types for queues?


a. String
b. Numeric
c. Enum
d. Array of Booleans
e. Cluster of a String and a Numeric

4. The Event structure handles only one event each time it executes.
a. True
b. False

© National Instruments | 1-17


Lesson 1 Moving Beyond Dataflow

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

© National Instruments | 2-1


Lesson 2 Implementing Design Patterns

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.

B. Simple Design Patterns


You learned to design three different types of design patterns in the LabVIEW Core 1 course—the
simple architecture, the general architecture, and the state machine.

Simple VI Design Pattern


When performing calculations or making quick lab measurements, you do not need a complicated
architecture. Your program might consist of a single VI that takes a measurement, performs
calculations, and either displays the results or records them to disk. The simple VI design pattern
usually does not require a specific start or stop action from the user. The user just clicks the Run
button. Use this architecture for simple applications or for functional components within larger
applications. You can convert these simple VIs into subVIs that you use as building blocks for
larger applications.

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

General VI Design Pattern


A general VI design pattern has three main phases—startup, main application, and shutdown. Each
of the following phases may contain code that uses another type of design pattern.
• Startup—Initializes hardware, reads configuration information from files, or prompts the user
for data file locations.
• Main Application—Consists of at least one loop that repeats until the user decides to exit the
program or the program terminates for other reasons such as I/O completion.
• Shutdown—Closes files, writes configuration information to disk, or resets I/O to the default
state.

Figure 2-2 shows the general VI design pattern.


Figure 2-2. General VI Design Pattern Framework

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.

© National Instruments | 2-3


Lesson 2 Implementing Design Patterns

State Machine Design Pattern (Polling)


The state machine design pattern is a modification of the general design pattern. It usually has a
start up and shut down phase. However, the main application phase consists of a Case structure
embedded in the loop. This architecture allows you to run different code each time the loop
executes, depending upon some condition. Each case defines a state of the machine, hence the
name, state machine. Use this design pattern for VIs that are easily divided into several simpler
tasks, such as VIs that act as a user interface.

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

You might also like