Introduction To LabVIEW - 2
Introduction To LabVIEW - 2
Exercise: Breakpoints
Set some Breakpoint around in your code and check out how it works. Use the Breakpoint
Manager tool.
Example:
Use the Step into/over/out functionality together with your Breakpoints and learn how you
can use them and see what the difference between them is.
Topics:
• Arrays
• Array Functions
• Cluster
7.1 Arrays
Arrays are very powerful to use in LabVIEW. In all your applications you would probably use
both One-Dimensional Arrays and Two-Dimensional Arrays.
7.1.1 Auto-Indexing
LabVIEW uses a powerful mechanism called “Auto-indexing”.
For Example you may use a For loop to create Array data like this:
Or you may use an Array like this in order to automatically specify number of iterations:
50
51 Working with Data
The most useful Array functions are:
Array Size
Index Array
Search 1D Array
Initialize Array
Build Array
Array Subset
Array Constant
All these functions are basic (but very useful) array functions you will probably be using in all
your applications and VIs.
In this example we see how we can use these Array functions and what they do.
The resulting Front Panel is as follows:
7.2 Cluster
Clusters group data elements of mixed types, such as a bundle of wires, as in a telephone
cable, where each wire in the cable represents a different element of the cluster. A cluster is
similar to a record or a struct in text-based programming languages. Bundling several data
elements into clusters eliminates wire clutter on the block diagram and reduces the number
of connector pane terminals that subVIs need. The connector pane has, at most, 28
terminals. If a front panel contains more than 28 controls and indicators that you want to
use programmatically, group some of them into a cluster and assign the cluster to a terminal
on the connector pane. Like an array, a cluster is either a control or an indicator. A cluster
cannot contain a mixture of controls and indicators.
Although cluster and array elements are both ordered, you must unbundle all cluster
elements at once rather than index one element at a time. You also can use the “Unbundle
By Name” function to access specific cluster elements.
In order to write to a cluster from the code, you may use the “Bundle” function or the
“Bundle By Name” function. See example below:
Example: Clusters
In order to get access to the different elements in the cluster, you need to “Unbundle” by
using the “Unbundle” function or the “Unbundle By Name”. See example below:
Exercises
Exercise: Arrays
Create some simple VIs where you use these array functions to manipulate array data:
Array Size
Index Array
Search 1D Array
Initialize Array
Build Array
Array Subset
Exercise: Arrays
Create a SubVI that find the “peaks” in the input array regarding to an input peak level.
Exercise: Clusters
Create a Cluster and get the different values from the controls in the Cluster. See Front Panel
below:
On the Front panel we have the following String controls and indicators available from the
Control palette:
On the Block Diagram we have the following String functions available from the Functions
palette:
Some of the most important String functions are:
Concatenate Strings
58
59 Working with Strings
This function concatenates several strings into on string:
Search and Replace String
Use this when you want to replace or remove a certain text in a string.
Match Pattern
This is the most useful function when it comes to string manipulation.
Example:
Exercises
Here are some exercises using some of the String functions that are available in LabVIEW.
Create a SubVI which removes leading zeros in a string. Create a Test VI that uses the SubVI.
Create a SubVI which removes all spaces from the end of the string. Create a Test VI that
uses the SubVI.
Create a SubVI which adds 2 strings into one. Create a Test VI that uses the SubVI.
9 Error Handling
This chapter explains the basic concepts of handle errors in your code.
Topics:
• Finding Errors
• Error Wiring
• Error Handling
You should always wire the Error cluster between all SubVIs, nodes, etc. that support this,
see example below.
The Error cluster is located in the Controls palette here:
62
63 Error Handling
The Error cluster contains of the following parts:
These are:
In general you should always show the error to the user. See LabVIEW Help for more details
of how to use these SubVIs.
Exercises
Exercise: Error Handling
Check out the different Error Vis in LabVIEW. Use them in some of your previous Vis.
Topics:
• Project Explorer
• Building .exe (executable) applications
• Deployment: Create an Installer
The project Explorer is necessary when you want to deploy your code into, e.g., an
executable (.exe) application, build a setup, etc.
66
67 Working with Projects
The Project Explorer is also very useful when you integrate a source control tool, such as
Team Foundation Server, Visual Source Safe, etc. Then you may easily check files in and out
of the source code system.
In order to create a new Project in LabVIEW, simply select “Empty Project” from the Getting
Started window when you open LabVIEW.
10.2 Deployment
When your application is finished, you may want to distribute or deploy your application and
share it with others.
The Project Explorer gives you several choices when it comes to distribute and deploy your
application. Some of the options are:
• Create an executable application (.exe) – this means that the target doesn’t need to
have LabVIEW installed on their computer. All the target need is LabVIEW Run-Rime,
which is a small installation package.
• You may create your own installer, so all the target need is to run a setup.exe in
order to use your application
• Other possibilities is to create a Web Service or a Shared Library (DLL) of your
application
All these options are available from the Project Explorer, just right-click on your “Build
Specifications” node.
There are lots of properties and setting you may use in order to create your application, go
through all the Categories in the Properties window.
When you have finished all the steps, just select “Build” in order to create your application.
When you make changes in your application, it is easy to rebuild your application:
Exercises
Exercise: Project Explorer
Create a new Project and put on of your existing application into the project
Exercise: Deployment
11 Design Techniques
This chapter explains some useful techniques to use when creating your application.
Topics:
In the example below we cannot be sure that the DAQmx Write.vi executes before the
DAQmx Stop Task.vi executes. LabVIEW will in this case randomly execute one of these first.
If the Stop VI happens to execute first then the Write VI will failed because task has been
stopped.
In the example below we wire the Error cluster through all the VIs, and there will be no
doubt that the Write VI will execute before the Stop VI.
71
72 Design Techniques
This approach will also take care of the error handling in your program, which is very
important in real-world applications.
A shift register transfers any data type and automatically changes to the data type of the
first object wired to the shift register. The data you wire to the terminals of each shift
register must be the same type.
To initialize a shift register, wire any value from outside the loop to the left terminal. If you
do not initialize the shift register, the loop uses the value written to the shift register when
the loop last executed or the default value for the data type if the loop has never executed.
Use a loop with an uninitialized shift register to run a VI repeatedly so that each time the VI
runs, the initial output of the shift register is the last value from the previous execution. Use
an uninitialized shift register to preserve state information between subsequent executions
of a VI. After the loop executes, the last value stored in the shift register remains at the right
terminal. If you wire the right terminal outside the loop, the wire transfers the last value
stored in the shift register. You can add more than one shift register to a loop. If you have
multiple operations within a loop, use multiple shift registers to store the data values from
those different processes in the structure.
Sometimes, you may want to change the order of the sequence, repeat one item in the
sequence more often than the other items, stop a sequence immediately, or have items in
the sequence that may execute only when certain conditions are met. Although your
program may not have any such requirements, there is always the possibility that the
program must be modified in the future. Therefore, a state programming architecture is a
good choice, even if a sequential programming structure is sufficient. The following list
describes more complex programming requirements that justify the use of a state
programming architecture for an application.
The State Machine approach in LabVIEW uses a Case structure inside a While loop to handle
the different states in the program, and the transitions between them. The Shift Register is
used to save data from and between the different states.
State Machine with multiple transitions depending on the State:
if they are not using the same shared resource. An example of a shared resource is a file, or
an instrument.
Using multiple While loops is sometimes useful in applications that need to handle User
interactions in parallel with, e.g., DAQ operations, etc.
Below we see an example of how this structure could look like. The upper loop could handle
interaction with the user, while the lower loop could handle DAQ operations, such as
reading and writing to some I/O equipment.
In order to pass data between the loops, you may e.g. use local variables. The loop may have
different time cycles. The I/O may require faster cycles than the User interaction loop.
11.5 Templates
You should create your own templates for such VI you use a lot. It is easy to create your own
templates for scratch, just create a VI as you normally do and then save it as a template with
the ending “.vit”. You may also convert a VI you already made just by changing the extension
to “.vit”.
You should copy your templates to the LabVIEW template folder which is default located in
“C:\Program Files\National Instruments\LabVIEW X.X\Templates\”.
The Templates will then be available from the Getting Started Window or File→New… in
LabVIEW.
I may case I have created a sub folder called “My Templates” where I place all my templates.
Exercises
Here are some exercises about shift-registers, State Machines and parallel programming.
Exercise: Shift-register
Create a VI (see example below) where you have the following states:
• Initialize
• Write
• Read
• Close
In the VI you will use a shift-register as a temporary storage. In the Write state you Write
Data to the storage (shift-register) while you in the Read state will read the Data from the
Storage.
Write a Test program that test the SubVI, e.g.,
This example shows how you can use a shift-register as a temporary storage, which is very
useful in many situations.
Create a VI that consists of 2 parallel loops. Use local variables and other mechanisms in
order to share data between the 2 loops.
Exercise: Templates
Strip some of yours previous Vis and save them as reusable Templates.
12 User Interface
This chapter explains the basic concepts of creating user-friendly Graphical User Interfaces
(GUI) in LabVIEW.
Topics:
• Decorations
• Tab Control
• Splitter
• Sub Panel
• Etc.
The information (Controls and Indicators) on the Front Panel is not structured. You should
group elements that naturally belong together and use different colors with care.
When creating Graphical User Interfaces (GUI) you should use the controls from the System
palette and not from the Modern or Classic palettes. Modern Controls may be used in Sub
Vis with no visible User Interface (for the user).
80
81 User Interface
The appearance of the controls in the System palette is standard MS Windows look and
feeling and this look is familiar for most users. These controls also change appearance due to
changes in the appearance in the operation system.
12.1 VI Properties
In order to make the appearance of the Window that hosts your application, you should
always make some changes in the “VI Properties”.
You find the “VI Properties” by right-click on the icon in the upper right corner of your VI.
The first thing you should change is the “Window title”. Here you may type appropriate
name of your application or SubVI.
The next you should do is to the “Customize” button in order to customize the Window
appearance.
Below we see an application with a simple and neat User Interface and with a Customized
Windows appearance.
Below we see a professional application created in LabVIEW that implement common GUI
objects such as a Toolbar, a Tree view, a List view, etc..
Exercises
Exercise: User Interface
Create a Dialog Box where you use some of the Controls from the System palette. Make the
necessary settings in VI Properties in order to hide menus, buttons, create a Title, etc.
Create a Test VI from where you open this Dialog Box, enter some data in the Dialog Box,
and then retrieve these data in the calling VI.
Example:
13 Plotting Data
This chapter explains the basic concepts when plotting data in LabVIEW.
LabVIEW offers powerful functionality for plotting data. In the Graph palette we have lots of
useful controls for plotting and visualization of data.
The most useful are:
• Waveform Chart
• Waveform Graph
• XY Graph
Example:
85
86 Plotting Data
The example below show the basic difference between a “Chart” and a “Graph”.
You use the “Graph” if you want to plot a set of data, e.g., an array with data, plot data from
a file, etc. Use the “Chart” if you want to plot one data point at a time, e.g., inside a loop,
etc.
13.1 Customizing
The different Chart components in LabVIEW offer a great deal of customizing.
You may click on the “Plot Legend” in order to set colors, different line
styles, etc.
If you right-click on the Graph/Chart, you may set properties such as auto-scaling, etc.
You may also select which items that should be visible or not.
The “Graph Palette” lets you zoom in and out on the Graph, etc.
Exercises
Exercise: Graph
Create a VI that reads data from a file and plot the data in a Graph component.
Exercise: Chart
Create a VI where you use Data Binding in order to retrieve data from an OPC demo.
Exercise: Customizing
Customize the Graph and the Chart in the examples above in order to set colors, line
thickness, etc.
Build Array
This example using the Build Array function inside a For loop in order build an array with 10
elements.
Index Array
91
92 Tips & Tricks
It is always useful to find a specific value in an array:
The Index Array is extendible, so you can drag it out to find more than one elements:
Array Size
Find the size of an arbitrary array:
Select
Depending on the input data, go to the Alarm case or the Write Data case.
Concatenate Strings
This function concatenate several strings into on string:
Search and Replace String
Use this when you want to replace or remove a certain text in a string.
Match Pattern
This is the most useful function when it comes to string manipulation.
Example:
Fract/Exp String to Number
This function converts a string into a number:
Example: Number To Fractional String
Example:
Short-Cut Description
Ctrl + B Deletes all broken wires in a VI
Ctrl + . Stops the Running VI
Ctrl + E Toggle between the Front Panel and Block Diagram
Tab Cycles through the most common Tools (Automatic Tool Selection
should be disabled!)
Ctrl + Mouse Scrolls through subdiagrams in Case, Event or Sequence structures
wheel
Ctrl + H Displays the Context Help window
Ctrl + Mouse Opens the Block Diagram directly
Double-click on a
SubVI
Ctrl + Arrows Move faster. You first have to select a SubVI, a Function, Object, etc
(→←←↓ )
Ctrl + W Close the SubVI
Double-click on a Selects the hole wire
wire
15 Example Application
In this example, we will go through an example application. The application uses most of the
LabVIEW features you have learned in this Tutorial.
The application is called “Glossary”. It is a simple application that learns kid’s words in
English. Since the application is for kids, the user interface is create with a “childish” look
and feel.
This is an example of how to create a user-friendly application in LabVIEW that uses most of
the functionality in LabVIEW, such as State-machine principles, reading and writing to files,
dialog boxes, graphics and sound effects. Basic functions for string and array manipulation,
and of course while loops, case structures, subVIs, etc. The example also uses the Project
Explorer to collect all the files in one place and to create an executable file of the solution.
Finally it uses the Project Explorer to create a setup package you can use to install the
application easily on other computers.
97
98 Example Application
I created this application for my kids in the Primary school who had problems with learning
their homework in English.
If you click on the little book symbol in your application, this window will pop up:
This makes it easy to keep an overview of all your files in your project. You may also use the
Project Explorer to create an executable file of your application. In addition you may also
create an installation package so you can easily install the application on other computers.
As part of the installation the LabVIEW Run-time engine will be installed.
Block Diagram:
The application uses the state machine principle, which makes it easy to create large
applications:
16 Additional Exercises
This chapter lists lots of additional exercises you could try out in order to improve your
LabVIEW skills.
vCard is a file format standard for electronic business cards. vCards are often attached to e-
mail messages, but can be exchanged in other ways, such as on the World Wide Web. They
can contain name and address information, phone numbers, URLs, logos, photographs, and
even audio clips.
Example:
BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:(404) 555-1212
ADR;WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:100 Waters Edge=0D=0ABaytown, LA 30314=0D=0AUSA
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:42 Plantation St.=0D=0ABaytown, LA 30314=0D=0AUSA
EMAIL;PREF;INTERNET:forrestgump@example.com
REV:20080424T195243Z
END:VCARD
When the user click Open, then a dialog box like this should appear:
101
102 Additional Exercises
Requirements:
Extend the application in the previous example. You should now be able to both write and
read vCard files. The application could look something like this:
When the user clicks New, the dialog box in the previous exercise appears. The user may
enter a new vCard. If the user clicks Edit, a dialog box with the selected contact should
appear.
Exercise: ActiveX
Create a simple Web Browser using ActiveX and the Internet Explorer ActiveX control
(Microsoft Web Browser)
Use the ActiveX function palette:
The application could look something like this:
Exercise: Themes
In e.g., ASP.NET we have something called Themes. Themes are used to change the
appearance of your whole application regarding to color, font, pictures, etc.
LabVIEW do not offer such a functionality , but try to create your own Theme Configurator,
so you can change the appearance of your VI instantly.
Create a similar Theme Configurator so you may easily change the appearance of your Vis.
17 What’s Next?
17.1 My Blog
For more information about LabVIEW, visit my Blog:
https://www.halvorsen.blog
LabVIEW resources:
https://www.halvorsen.blog/documents/programming/labview/
17.2 Tutorials
This Tutorial is a part of a series with other Tutorials I have made, such as:
• Introduction to LabVIEW
• Data Acquisition in LabVIEW
• Control and Simulation in LabVIEW
• LabVIEW MathScript
• Linear Algebra in LabVIEW
• Datalogging and Supervisory Control in LabVIEW
• Model Predictive Control in LabVIEW
• Wireless Data Acquisition in LabVIEW
• etc.
These Training Kits are available for download (.pdf files, source code, additional resources,
etc.) from my blog:
https://www.halvorsen.blog
www.ni.com
106
107 What’s Next?
17.4 Examples
In the NI example Finder (Help → Find Examples…) you find lots of useful examples that you
can play with or use as a start when creating your own applications.
Quick Reference
108
Quick Reference
Introduction to LabVIEW
Hans-Petter Halvorsen
Copyright © 2017
E-Mail: hans.p.halvorsen@usn.no
Web: https://www.halvorsen.blog
https://www.halvorsen.blog