You are on page 1of 16

Chapter 5

Using G to Control an Instrument


Here we get to the heart of what this course is aboutusing G to control an instrument. There are
five activities. At the end of the chapter you are given student drivers (sub-VIs) as a kind of
reward for your effort. You are welcome to use these drivers to write your own working
programs for assignments or the project requirement of the course.

Overview
LabVIEW and the G programming language were
released by National Instruments (NI) in 1987. Up to
that time programmers commonly controlled an
instrument from a computer by making toolbox calls
through RS-232, GPIB (and sometimes parallel) interfaces. NI still supports this kind of access for developers who are using non-G compilers. In addition, in its
first release, LabVIEW offerred special VIs for the
specific access of RS-232 and GPIB ports. But since
Release 4.0, and especially since the advent of VXI
devices and the call for greater standardization, NI
has promoted a more general structure for interface
access. 1 This structure is called VISA. It is based on a

standard initiated by NI and adopted by a number of


instrument manufacturers.2 Though NI still supports
the specialized VIs to a limited extent, the company
recommends adoption of VISA to ensure future compatibility. For these reasons, we discuss only VISA in
this chapter.3 In addition, we discuss only VISA as
implemented in LabVIEW version 6.1.
We discuss here the LabVIEW VISA VIs that enable
access of VXI, GPIB and RS-232 in a common way. For
pedagogical reasons, we describe them in activities in
the order in which they might be used by a typical
programmer in the building of a working program.

What VISA is
VISA is an acronym for Virtual Instrument Standard
Architecture. As described by NI, VISA is a standard
I/O Application Programming Interface (API) for instrumentation programming. VISA by itself does not
provide instrumentation programming capability.
VISA is a high level API that calls into lower level
drivers (Figure 5-1). Common VISA VIs can control
VXI, GPIB, or RS-232 instruments making the appropriate driver calls depending on the type of instrument
being used. Because VISA is an industry standard for
developing instrument drivers, most instrument
drivers currently written by NI use VISA and therefore support most major computer platforms if the
system level drivers exist for that platform.4
VISA uses the same high-level operations to communicate with instruments regardless of the interface
type. This means that the VISA command to write an
ASCII string to a message-based instrument is the
same whether the instrument is of VXI, GPIB or RS-

232 type. This makes for interface independence. New


types of interfaces emerging in the marketplace like
universal serial bus (USB) and IEEE-1394 (discussed
in Appendix B) have been described by the originating organizations in terms of the VISA paradigm.

VISA

VXI

GPIB

Serial

NI-VXI

NI-488.2

OS Calls

Figure 5-1. At present the VISA structure supports three


device types: VXI, GPIB and Serial. This list is anticipated
to grow to include USB and Firewire.

5-1

Using G to Control an Instrument

In subsequent updates of LabVIEW, NI is expected to


extend the existing VISA VIs to include USB and
IEEE-1394 interfaces.

Portability
Another advantage of VISA is that a program written
with VISA function calls on one platform is more
easily ported to another. To make this possible VISA
strictly defines its own data types. Issues like the size,

in bytes, of an integer varying from one platform to


another do not affect a VISA program. The VISA
function calls and their associated parameters are
uniform across the four major platforms. Software can
be ported to other platforms supporting LabVIEW
and then recompiled. Since the laboratory in which
this course is held contains IBM PC compatible and
Macintosh computers, you can witness this portability
for yourself.

A VISA Program in Outline


A program using VISA VIs is shown in block diagram
form in Figure 5-2. This program (reading left to right)
finds the resources residing on the host computer,
opens a session to the instrument, writes (or sends) a
command to the instrument, reads the response from
the instrument and then closes the session to the
instrument.
The core of the program consists of the five VIs:
VISA Find Resource, VISA Open, VISA Write, VISA Read and
VISA Close.
The first task of a program once it boots should be
to search out and identify the resources residing on
the host computer. By resource is meant the number
and identity of RS-232 port(s), the number and
primary addresses of GPIB port(s), and the existence
of connected VXI devices. This gives the programmer
the option of listing the resources in order of priority,
and then of choosing the type of instrument to access.
Having decided on the instrument(s) to access, the
next step is to open a session to the instrument(s). A
session is a link or a channel to an instrument. This
concept is used in LabVIEW because more than one
session to more than one device can be open at any

one time. A command or a query string can be sent to


the instrument using VISA Write. The subsequent
response from the instrument can be read using VISA
Read. Finally, the session can be closed using VISA
Close. Normally, in a large program you would open a
session to an instrument and leave it open while the
program executes. Then, when the user chooses to exit
the program, you would have your program close the
session.
Figure 5-2 illustrates the concept of data flow. Each
VI is connected to the one following it by two wires
called the VISA resource name wire and the Error wire.
The VISA resource name wire passes the same resource
information down the line from one VI to the next, so
the Write, Read and Close operations pertain to the
same session. The error wire ensures that if an error
occurs at one stage, then the error information is
passed to the VI following preventing that VI from
executing. This enables the programmer to determine
where the error occurred and to correct it. As you
examine each VI in turn in this chapter you will build
a VI that can serve as a template for any program you
subsequently write using VISA.

resource name wire


VISA
Find
Resources

VISA
Open

VISA
Write

VISA
Read

VISA
Close

Error wire
Figure 5-2. A block diagram of a program to control an instrument using VISA VIs. For the sake of simplicity, the necessary
controls and indicators for each block have been omitted.

5-2

Using G to Control an Instrument

Activity #1. VISA Find Resource


The first task of a program should be to determine if the host computer actually possesses the
interface to be used, which LabVIEW calls a resource. In the process of identifying the resources of
the host computer, you can also establish if all of the necessary resources for your program to run
are actually present, and if they are not you can have your program generate an error message for
the user. Here you will investigate the VISA Find Resource VI that provides the first step.

Help
The help for VISA Find Resource is reproduced in
Figure 5-3.

Figure 5-3. The VISA Find Resource VI.

The first thing to know about VISA Find Resource is that


though its use is recommended as good programming
practice, it is not absolutely necessary for a program.
If you are targeting your program for a specific
resource like a specific RS-232 port you may choose to
omit this VI and then generate an error message if the
subsequent VISA Open fails if the port is not found or is
already in use. Though an option, this is is not the
most elegant one.
If you choose to use VISA Find Resource you must run
it before you make an attempt to open a session. This
VI requires a search string in the control expression
(). Expression tells the VI what types of resources to
look for. Sample search expressions are listed in Table
5-1. If you leave expression () unwired, then the VI
will assume a null string by default and will not work.

Interface Type[board index]::Address::VISA Class


Contrary to VXI and GPIB resource names, RS-232
resource names do not require an address parameter.
For example, ASRL1::INSTR is the name of the COM1
serial port on a PC (or the modem port on a Mac). The
COM2 port on a PC (printer port on a Mac) would
have the name ASRL2::INSTR.
You can often pick from find list the resource to open
by index since the entries are returned in a canonical
orderVXI first, then GPIB and then RS-232 (if all are
present).

VISA Class
VISA Class in the expression above is a grouping that
contains some or all of the VISA operations. There is a
VXI class, a GPIB class, a serial class and an INSTR
class. The INSTR class includes all VISA operations
for an instrument. If the VISA class is omitted from
the expression, INSTR class is assumed by default.
The drivers for student use in this course were written
to assume INSTR class in order to make them most
general.

Building MyFindResources.vi
You will now start building a simple standalone VI.
The first step is to place and wire VISA Find Resource.
Do the following:

Boot LabVIEW so that it brings up a fresh Panel.


Switch to the Diagram.

Table 5-1. The most common search expressions input to


VISA Find Resource.

Select Instrument I/O >> VISA >> VISA

Instrument Resources
GPIB
RS-232
All

Expression
GPIB[0-9]*::?*INSTR
ASRL[0-9]*::?*INSTR
?*INSTR

VISA Find Resource returns the number of resources


found in the indicator return count, and their names in
the indicator array find list. These names have the
format:

Advanced >> Find Resource and drop the VI


in a convenient spot on the Diagram.
Create the controls and indicators required of the
VI and tidy up the Diagram so it resembles Figure
5-4.
Switch to the Panel and tidy up the placement of
the controls and indicators. Duplicate or improve
on the panel shown in Figure 5-5.
You must now enter a search string into the
control expression (). Type ?*INSTR into this
space. This will instruct VISA Find Resource to find

5-3

Using G to Control an Instrument

resources of all types.


Before running the VI turn on all the instruments
at your workstation in the lab that are connected
to your computer via RS-232 or GPIB interfaces.
Wait a few moments for the instruments to
complete their initialization.

Figure 5-4. MyFindResources.vi at this stage of construction.

When you are ready, run the VI. Was an error


message generated? Note the number in the
Indicator return count and the resource names in
the Indicator find list. The output might resemble
what is shown in Figure 5-5.5

Figure 5-5. The panel of MyFindResources.vi after being


run. Three serial resource names are shown in find list.

When you are ready to quit, save your VI in the location and under the name of your choice (MyFindResources.vi will do nicely). If you are doing this as
part of Lab #3 save your VI on the floppy disk. You
will be adding to this VI in the following activities.

Activity #2. VISA Open


Once the programmer has identified the resources on the host computer and selected the
instrument to control, the next step is to open a session to the instrument. Here you will learn how
to open an RS-232 session to a device and how to set the parameters the instrument requires. In
later sections of this chapter you will see how to do this in a general way (ala VISA) to include
GPIB as well as RS-232.

Help
The help for VISA Open in LabVIEW 6.1 is reproduced
in Figure 5-6.6

Figure 5-6. The VISA Open VI.

VISA Open takes a maximum of five controls. The


control timeout (emanating from the top of the icon in

5-4

Figure 5-6) can be left unwired.7 You can wire the


VISA Resource name from the VISA Find Resource VI
discussed in the previous activity. As stated earlier,
you dont need VISA Find Resource to obtain a resource
name; you could wire one of the names listed in Table
5-1 directly to VISA Open via a control or a constant if
you so wished. You can leave the duplicate session (F)
and access mode inputs unwired. You can wire the
control error in (no error) from VISA Find Resource if you
are using it.
As to the outputs of VISA Open, we will have you
wire them to the VI following in the next activity.
Before going on it is useful to pause for a look at the
RS-232 parameters and in particular at the values of
the parameters that VISA Open assumes by default. If
you know what the default values are you can use
them perhaps and save coding them.

Using G to Control an Instrument

For this purpose VISAOpenDefaults.vi has been prepared


for your use. You can find it in PSCB01S >> LabVIEW
Demos >> Chapter 05 >> Demos.

VISAOpenDefaults.vi
This section is optional and you might wish to skip it
on a first reading of this chapter. To find the default
values VISA Open assumes you can run VISAOpen
Defaults.vi (Figure 5-7). Some of the RS-232 parameters,
such as baud rate, etc., are described in Appendix B.
There are a number of others:
Serial Settings
These settings apply to RS-232 communication only:
1

End Mode for Reads. This determines whether a


termination character will be recognized when
received. The default value is TermChar, meaning
that an RS-232 read will stop when a termination
character is received. Default: TermChar.
End Mode for Writes. This determines whether a
termination character will be sent automatically at
the end of a write string. Default: None. Setting
this True can be useful since it saves you typing
the termination character(s) at the end of each
string you write to the instrument.

Message Based Settings


1 Send End Enable. This property specifies whether
to send an END indicator on the last byte of each
write operation. Default: TRUE
2 Suppress End Enable. This property specifies
whether an END indicator will be ignored on read
operations. Default: FALSE.
3 Termination Character. This property specifies a
character that, when read, causes a read operation
to terminate. The termination character must also
be enabled (see 4 next). Default: ASCII 10 <LF>.
4 Termination Character Enable. This property
specifies whether a read operation should terminate when the termination character is received.
Default: FALSE.
Figure 5-7 shows the output of VISAOpenDefaults.vi
from an actual run. Figure 5-8 shows a fragment of the
Diagram of this VI. The parameters are read via a
Property Node. (Recall that you used a property node
in Chapter 4 to set certain properties of a waveform
Graph.)

Figure 5-7. VISA Open defaults shown on the panel of


VISAOpenDefaults.vi.

Adding to MyFindResources.vi
You are now ready to add a VISA Open and a Property
Node to MyFindResources.vi. Do the following:

Load MyFindResources.vi and switch to its


Diagram.

Select a VISA Open and place it on the Diagram just

to the right of VISA Find Resource.


Wire the Diagram so it resembles Figure 5-9. You
need to pass VISA Open the first element in find list,
so wire Index Array to find list, index the first
element (Index 0) and wire it to the control

5-5

Using G to Control an Instrument

resource name of VISA Open.

Figure 5-8. A fragment of the Diagram of VISAOpen


Defaults.vi showing the property node. The wires leading
off to the right are connected to the indicators in Figure 5-7.

read mode by default (the wiring terminal is on


the right end of the object). To change it to Write
mode z-click (right-click) on TermChar and
select Change to Write mode (Figure 5-11).
You should now see the wiring terminal on the
left end of the object.

Figure 5-10. Selecting Termination character from the Property Node pulldown menu.

Select Instrument I/O >> VISA >> Property


Node and place the node just to the right of VISA
Open. Wire it as shown in Figure 5-9.

Figure 5-11. Changing the mode of TermChar to Write.

Figure 5-9. The Diagram of MyFindResources.vi at this


stage of construction.

You are now going to complete the Property Node.

You first need to set a setting on the property


node, so place the mouse pointer over the word
Property, hold the (left) mouse button down
and select Message Based Settings >>
termination character (Figure 5-10). If you
do this correctly the word Property will change
to TermChar. At this stage TermChar is in

5-6

You now need to choose a termination character,


so create a constant on the left end of TermChar.
In the constant space thus created type the ASCII
value of the termination character you want. If
you are working on a Windows PC type 10 for
a <LF>, if on a Mac type 13 for a <CR>.
To define a second attribute you need to add an
element to the list of properties so z-click (rightclick) on TermChar and select Add Element.
Then select Termination Character Enable.
Finish the wiring as shown in Figure 5-12.

Using G to Control an Instrument

TxD line is low? You will know it is low if the


LED on the TxD line is glowing red.
We suggest at this stage that you save your work.

If you are doing this as part of Lab #3 then save your


VI as MyVISAProgram.vi on the floppy. Your program is
only partially complete. You will continue with its
construction in the next activity.
Figure 5-12. The Diagram of MyVISAProgram.vi at this
stage of construction.

To continue

Run the VI. Was an error generated? Can you tell


by examining the LEDs on the RS-232 LED
indicator box (described in Appendix B) if the

Quit LabVIEW. Does the LED on the TxD line go out?


Does the LED on the RxD line return to a steady low?
This means that though you have Opened the RS-232
port by running your VI, and have not closed the port
as part of your program, LabVIEW has closed the port
anyway when it quit. This is normal LabVIEW
operation.

Activity #3. VISA Write, Read and Close


The business of controlling an instrument involves writing to it and reading from it in
consecutive steps. A VISA Read nearly always follows a VISA Write. It makes sense therefore to
consider them together in the same activity. We consider VISA Close at the same time, since it
closes the port without affecting the instrument itself.

Help
You write a message to your instrument with VISA
Write. Its help window is shown in Figure 5-13. The
picture of a watch in the upper right hand corner of
the icon indicates that VISA Write is asynchronous by
default. You should change this to synchronous
operation. To do this place the mouse pointer over the
icon, command click and select synchronous from the
drop down menu.

Figure 5-13. The VISA Write help window.

VISA Write
VISA Write takes a maximum of three controls: VISA
resource name, write buffer (), and error in (no error). VISA

resource name and error in can be wired from a VISA


Open placed just before it. Write buffer contains the
string you wish to write, which we will get to in a
moment. The indicators dup VISA resource name and
error out can be wired to the following VI, a VISA Read
for example. You can leave the indicator return count
unwired; it returns the number of characters written,
which is sometimes useful if you need this number
and dont know it already.
VISA Read
VISA Read (Figure 5-14) takes three controls: VISA
resource name, byte count (0), and error in (no error). The
controls VISA resource name and error in can be wired
from VISA Write. The control byte count specifies the
number of bytes to wait for (which we shall describe
in more detail in a moment). The indicators dup VISA
resource name and error out can be wired to a following
VI, for example, to a VISA Close. The indicator return
count returns the number of bytes in the string
returned from the instrument. The indicator read buffer
contains the actual return string, the information you
are after.

5-7

Using G to Control an Instrument

Figure 5-14. The VISA Read help window. This VI should


also be changed to synchronous operation.

VISA Close
Any session you open to a VISA resource uses system
resources within your computer (for example, the port
itself and memory set aside for buffers), so at the end
of the VISA session you should free up these
resources. If you fail to do so, the session remains
open. To close a session use VISA Close (Figure 5-15).

Completing Write Controls


You now have to think about what command you
want to write to your instrument. This will depend on
the instrument you are using and what you want the
instrument to do. Let us take the simplest example,
the Radio Shack Manual/Auto Range DMM, described in Lab #1 and in Appendix A. We know this DMM
will send a response string when it detects a voltage
transition on its RxD line. One way of producing a
voltage transition is to send it a carriage return <CR>.
Thus wire the <CR> as shown in Figure 5-17.

Figure 5-17. MyVISAProgram.vi with write/read controls/


indicators added.

Figure 5-15. The VISA Close help window.

VISA Close takes two controls, VISA resource name and


error in (no error). These you can wire from a previous
VISA Read. You are now ready for the wiring.

Adding to MyVISAProgram.vi
Add a VISA Write, VISA Read and VISA Close to
MyVISAProgram.vi as follows:

To make room for the VISA VIs on your Diagram,

disconnect the indicator error out (Figure 5-12) and


move it out of the way.
Place a VISA Write, VISA Read and VISA Close to the
right of the Property Node and wire them
together as is shown in Figure 5-16.

Figure 5-16. MyVISAProgram.vi at this stage of construction.

5-8

Completing Read Controls/Indicators


You need to tell VISA Read to read 14 bytes from the
read buffer (the length of the string returned from the
DMM). So wire a byte count constant of 14 shown in
Figure 5-17. Then go ahead and create the indicators
read buffer and return count.
Revisiting the Property Node
At this stage you will have to return to the Property
Node to ensure that the values of the RS-232 parameters are what the instrument requires. If you review
the Communication section for this DMM in Appendix A you will see that the Property Node needs to
specify a baud rate of 1200, 7 data bits and 2 stop bits
as illustrated in Figure 5-18.

Figure 5-18.The completed property node for the Radio Shack


Manual/Auto Range DMM.

Using G to Control an Instrument

When ready, ensure the DMM is ON and then run the


VI. The result might resemble Figure 5-19. If you get
an error message, alert your instructor. Possible
sources of an error are incorrect cabling or a faulty
DMM.

If you are doing this as part of Lab #3 then save your


results as MyVISAProgram+.vi on your floppy.
In the previous section we mentioned errors and the
need to connect each VI to the one following it with
an error wire. In the next activity we examine the
subject of errors in greater detail.

Figure 5-19. The panel of MyVISAProgram.vi in its final


state and after having been run.

5-9

Using G to Control an Instrument

Activity #4. VISA Error Handling


Many VIs in G can be linked with an Error wireand this is certainly the case for VISA VIs. This
means that if one of the VIs happens to generate an error, then the error information is passed to
the VI following preventing that VI from executing. In this way, error information gets passed to
the last VI in a linked chain and subsequently displayed or otherwise dealt with. From the
information contained in the error cluster the programmer can establish what type of error
occurred and where in the program it occurred. In this activity we will deliberately have you
generate an error so you can identify it.

The Error Cluster


The information conveyed on a VISA error wire is in
the form of a datatype called a cluster (Figure 5-20).
You have already seen a cluster in Chapter 4 in
conjunction with a Waveform graph. A cluster is a
datatype comprising a collection (cluster) of data that
may be of different typeboolean, numeric, string
and so forthbut all controls or all indicators and not
a mixture of both. The error control/indicator
contains three pieces of information: status, code and
source. We shall describe what each means in a
moment.

Figure 5-20. An error out cluster consists of three indicators: a boolean, a code numeric and a source string. An
error in cluster contains a status control, code control and
source control.

Let us deliberately generate an error with


MyVISAProgram+.vi so you can see what the indicator
error out looks like when the error occurs. Do the
following:

Load MyVISAProgram+.vi but dont run it just yet.


Connect the Radio Shack DMM to your computer

5-10

in the usual way but leave it OFF.

Run the VI. After about 10 seconds the VI will


stop and the indicator error out will have entries in
it.

Interpreting the Error Cluster


If you know how to interpret an error cluster you will
be prepared to correct an error when it occurs. In the
first place the error cluster you are looking at now
should show the status boolean in the ON state,
meaning (surprise!) that an error occurred. The
numeric code shows (if you look it up in the LabVIEW
Users Manual), that something has timed out. The
source string tells you that the timeout occurred in the
VISA Read.
This means that no error was generated in the VISA
Open, meaning that the port on your computer opened
OK but the instrument (the RS DMM) failed to send
data in the allotted time of 10 secondsno surprise
since the instrument was off.
If you are doing this as part of Lab #3 enter the
boolean indicator, code and source string information
you see on the Results Page.
We shall return to the subject of errors later.

Taking Stock
At this stage you have a working VI. Looking back
you can see that your VI consists of a number of parts
or building blocks. In the next activity we shall have
you examine some building blocks or student
drivers that have already been prepared for you.
These drivers should make it even easier for you to
write G programs to control the other instruments
described in this course.

Using G to Control an Instrument

Activity #5. Using a Student Driver


We present here the idea of a student driver. A student driver is a modular prefabricated VI
designed to be used by a student to perform a specific function for a specific instrument. It is
designed for maximum flexibility in the sense of being useable on different computers and with
different interfaces. We describe the first driver, for the RS DMM, in some detail. The others we
list for you to investigate on your own. We are confident you will find them very easy to use.

The Idea of a Driver


Our idea of a driver is not original. National
Instruments has supported the writing of instrument
drivers by software engineers for many years. In a
course such as this one, that involves so many
different instruments and runs for some 12 weeks, we
have found it adviseable to adapt the idea and
simplify it for student use.
Our student drivers are in some respects extensions
of the VISA VIs. There is an RSOpen.vi for the Radio
Shack DMM, a TekOpen.vi for the Tek oscilloscope and
so on. There are corresponding Close VIs and VIs for

other purposes. Generally speaking, student drivers


are designed to be used in conjunction with other
student drivers and not on their own.

RSOpen.vi
The first VI we present is RSOpen.vi (Figure 5-21). This
VI performs a VISA Open specifically for an RS DMM.
It contains two sub-VIs, FindResourcesDemo.vi and
RSPrep.vi. It begins by finding the resources on the
host computer using FindResourcesDemo.vi. This VI
returns the resource name requested or, if one is not
requested, a default one.

Figure 5-21. The Diagram of RSOpen.vi.

If you wish you can open the front panels of the subVIs. If you do this you will see that RSPrep.vi uses an
attribute node to set a number of values for the
instruments RS-232 interface (the RS DMM does not
work with GPIB). The termination (EOL) character is
set for the platform used: <CR> <LF> for a Windows
PC, <CR> for a Mac, <LF> for a UNIX box.

Depending on the platform the RTS line may be


Unasserted.
Basically, the idea of a student driver is that it is
simple enough to be dropped onto a Diagram and
wired much like the VISA VIs that form its internal
structure (Figure 5-2). Example Problem 5-1 shows
how to do this for the RS DMM. You will do this as a

5-11

Using G to Control an Instrument

part of Lab #3.

VI after having been run is shown in Figure 5-24. It


can be seen that the VI works as expected.

Example Problem 5-1


Writing a Program for the RS DMM Using Student
Drivers
Using the student drivers listed in Table 5-1 write a G
program to get and display the 14-byte string from a
Radio Shack Manual/Auto Range DMM.
Solution:
The first step is to drop an RSOpen.vi, RSGet.vi and
RSClose.vi onto a Diagram and wire them together
(Figure 5-22).

Figure 5-23. The completed RSExample.vi.

Figure 5-22. This shows the first step in using student


drivers to build a VI for communicating with an RS
DMM. If defaults are acceptable the controls of RSOpen.vi
can be left unwired.

If you have no special requirements, RSOpen.vi (like


the other xxxOpen.vis in Table 5-1) can be left with no
controls wired at all. It is designed to automatically
perform a VISA Open specifically for the RS DMM, to
select as the default the first COM port it finds and to
set the RS-232 parameters for the instrument. The RS
DMM does not work with a GPIB interface. As can be
seen from Figure 5-21, you could specify the resource
to look for and the port to choose. There is no use in
selecting a GPIB port because the instrument does not
work with GPIB, but you might want to select the
COM port if, for example, you are using more than
one DMM, one on each port. If Port Index (0) is left
unwired, then the first COM port found will be
chosen by default. This is OK here.
As a last step we pop up on RSGet.vi to create the
indicator Output String, and pop up on RSClose.vi to
produce the indicator error out. The Diagram of the
finished VI is shown in Figure 5-23. The Panel of the

5-12

Figure 5-24. The Panel of RSExample.vi after having been


run.

The process of writing this little VI can be readily


extended to the other instruments listed in Table 5-1,
as we shall attempt to show in Lab #3.

Using G to Control an Instrument

Table 5-1. A selection of student drivers available at the time of writing. All xxxOpen VIs are similarly written and use
FindResourcesDemo.vi. These drivers are designed to be used in conjunction with other drivers and not on their own.
Driver Name

RSClose.vi
RSDecoder.vi
RSGet.vi
RSOpen.vi
RSPrep.vi
Data+.vi
TekClose.vi
TekDecoder.vi

Instrument
Radio
Shack
Auto
Range
Digital
Multimeter
Tektronix
TDS210
Digital
Oscilloscope

TekHardCopy.vi
TekIMM.vi
TekOpen.vi
TekPrep.vi
TelClose.vi
TelOpen.vi
TelWrite.vi
HPMClose.vi
HPMOpen.vi

HPPClose.vi
HPPOpen.vi
HPPPrep.vi
HPPSweepInit.vi
OHClose.vi
OHOpen.vi

Telulex
SG-100/A
Signal
Generator
Agilent (HP)
34401A
Digital
Multimeter
Agilent (HP)
3640A
Programmable
Power
Supply
Ohaus Scout II
electronic

Description
Performs a VISA Close.
Decodes the 14-byte string returned and displays the mode, numeric and unit parts
of the string in indicators.
Gets a 14-byte string. Outputs an optional time stamp for the Read operation.
Performs a VISA Open. Works only for RS-232.
Sets the values of the RS-232 parameters for the DMM.
This VI queries the instrument for the waveform data and the formatting data. It
outputs two bundles, one for each channel. Uses TekDecoder.vi.
Performs a VISA Close.
This takes waveform data in the form of a binary string and converts it to an
output array of SGLs in absolute units.
Returns a hardcopy display of the oscilloscope screen in the form of a string.
Called by TekBMPDisplay.vi.
Illustrates how to use the immediate measurement mode, the mode in which the
oscilloscope performs a measurement such as Mean.
Performs a VISA Open. Works with GPIB or RS-232. For either resource OPC?
query operation is enabled. For more information on device specific commands see
Appendix A.
Sends some initial, desireable commands to the oscilloscope.
Performs a VISA Close.
Performs a VISA Open. Works only for RS-232 since the instrument works only
with RS-232.
Writes a control string to the instrument and then purges the serial buffer of
unwanted \r\n> character groups returned by the instrument.
Performs a SYSTEM:LOCAL and a VISA Close.
Performs a VISA Open. Works with GPIB or RS-232. If RS-232 is used the serial
parameters are set and SYSTEM:REMOTE is performed. Also performs *RST and
*CLS for RS-232 and GPIB.
Performs a SYSTEM:LOCAL and a VISA Close.
Performs a VISA Open. Works with GPIB or RS-232. If RS-232 is used the serial
parameters are set. Performs *RST and *CLS for RS-232 and GPIB.
Sets up parameters for HPPSweep.vi.
Initializes HPPSweep.vi.
Performs a VISA Close.
Performs a VISA Open. Sets the RS-232 parameters and the weight units.

5-13

Using G to Control an Instrument

Table 5-2. A selection of Demo VIs available for student use. These VIs can be used with any of the instrument VIs listed in
Table 5-1.
VI Name

Description

FindResourcesDemo.vi
TwoResourcesDemo.vi

Finds the VISA resources on the host computer. This VI is used by most xxxOpen VIs.
A VI to access two resources simultaneously. The resources may be RS-232 and GPIB or both.

Table 5-3. A selection of Example Programs (VIs) available for student use. All VIs use the xxxOpen and xxxClose VI
appropriate for the instrument. If the control Resource Desired on the instruments xxxOpen VI is left unwired, a default
resource is chosen. Some of these VIs will be demonstrated in Lab #3.
VI Name

TwoResources.vi
MyRSExample.vi
RSCOMDemo.vi
RSTest.vi

Instrument
General
Radio Shack
Manual/
Auto Range
Digital
Multimeter

RSDualPort.vi
GetTekID&Time.vi
GetTekCurve&Time.vi
TekBMPDisplay.vii

Tektronix
TDS210
Digital
Oscilloscope

TekGetCurve.vi
TekGetID.vi
TekMeas1Pt.vi
TekMultiPt.vi
TekParseCurve.vi
TekTest.vi
TelGetID.vi
TelStep.vi
TelSweep.vi
HPMGetID.vi
HPMMeasVolt.vi
HPPGetID.vi
HPPSweep.vi

Telulex
SG-100/A
Signal
Generator
Agilent 34401A
Multimeter
Agilent
3640A

OHContinuous.vi
OHPCommand.vi

Ohaus
ScoutII

5-14

Description
Enables one or two RS-232 or GPIB resources to be accessed concurrently.
Reads and displays the output.
Reads and displays output when the DMM is in COM mode (when it is
sending continuously).
Performs an Open, Get and Close in slow motion steps to enable the RS-232
interface to be tested with an LED indicator box.
Gets and displays a 14-byte string from two DMMs, one on each of two COM
ports.
Gets and displays the ID and the elapsed time.
Gets and displays the waveform data and the elapsed time.
Gets and shows on a Panel in BMP format the screen display of the
oscilloscope.
Gets one channel of waveform information in ASCII format and displays it in a
string indicator.
Gets and displays an instrument ID.
Illustrates how to get a single measurement of voltage.
Shows how to turn the oscilloscope into a continuous reading voltmeter.
Illustrates how to parse the CSV file returned.
Tests the GPIB or RS-232 interface (like RSTest.vi).
Gets and displays an instrument ID.

Illustrates how to sweep the output frequency over a frequency range.


Gets and displays an instrument ID.
Measures and displays one voltage measurement.
(VI) Gets and displays an instrument ID.
(VI) Sweeps the output voltage over a selected range for (possibly) two LEDs,
one after the other.
(VI) shows how to get data from the balance when it is printing continuously.
(VI) shows how to get data from the balance by sending the P command.

Using G to Control an Instrument

Practice Problems
In solving the following problems you may use the student drivers listed in Tables 5-1 and 5-2.
The programs are to be written in G. Some of these problems are also described in Appendix E.
1.

Write a program to obtain and display the ID


from a Tek TDS210 digital oscilloscope.

2.

Write a program to obtain and display the setup


parameters from a Telulex signal generator. Information for this task can be found in Appendix A.

3.

Set up a signal generator to produce a signal on


channel 1 of the Tek TDS210 digital oscilloscope.
Write a program to to transfer this data to your
computer (ASCII format) and display it in a
waveform graph.

4.

The occasion may arise when you wish to control


two instruments from the same computer concurrently, say the Telulex SG-100/A signal generator
and the Tek TDS210 digital oscilloscope. Both
instruments may be controlled via RS-232 but not
through the same port. Write a program to control
the signal gernerator via COM1 (modem) port

and the oscilloscope via COM2 (printer) port.


Send the command to the signal generator to
produce a 500 kHz sine wave and the command
to the oscilloscope to return the signal data from
the oscilloscope.
5.

There may be times when you need to control an


RS-232-only instrument and a GPIB-only
instrument from the same computer concurrently.
Again, these instruments might be the Telulex SG100/A signal generator and the Tek TDS210 digital oscilloscope as described in problem 4. Write a
program to generate a 1 MHz signal on the
Telulex via the RS-232 port and return the signal
data from the Tek oscilloscope via GPIB.

6.

5-15

Using G to Control an Instrument

EndNotes for Chapter 5


1

VXI (pronounced vixie) stands for Virtual Extensions for Instrumentation. A VXI device is usually a rack-mounted box
lacking front panel controls that is designed to be used in research or industry. Control is entirely by computer. National
Instruments is a major supplier of VXI devices. Though you might eventually be called upon to use a VXI device in the course
of your science research, we will not using VXI devices in this course.
2
The official descriptions of Universal Serial Bus and IEEE-1394 were written by the various consortia with frequent
reference to the VISA paradigm. See for example, Appendix B and the websites
3
If you come across a text on LabVIEW written prior to 1999 you will find tutorials on how to use the special serial and
GPIB VIs. Though we realize they have pedagogical value in learning about serial and GPIB devices they are obsolete and we
shall not be discussing them here. There is no guarantee National Instruments will support these special VIs indefinitely.
Indeed, NI accepts contributions of RS-232/GPIB/VXI VIs for posting on its website only if they are written with VISA VIs.
4
Platforms supported are Macintosh (on which LabVIEW was originally developed), Windows 3.x, Windows 95/98/NT,
Solaris 1, Solaris 2, and HP-UX
5
To produce Figure 5-5 MyFindResources.vi was run by the auther on his Mac G3 powerbook which is known to have two
serial ports, only one of which has an actual connector (like many Windows PC laptops). Should you run this VI on a generic
Windows PC you should see several resource names in find list. VISA FindResource behaves subtly differently for serial and
GPIB, but in a way consistent with the functioning of the interface. It will return resource names for serial resources whether
or not serial instruments are connected to the host computer and whether or not those instruments are on. However, a GPIB
resource name will be returned if and only if the GPIB card is installed and a GPIB instrument is connected and powered up.
If the device is off the resource name will not be returned. On a Mac G3 powerbook only two resource names should have
been returned. Three are shown in Figure 5-5. (Two only are returned by LabVIEW version 5.1.) At the time of writing the
reason for the difference was not known.
6
The VISA Open VI in LabVIEW version 6.1 is subtly different from version 5.1. Indeed, in version 6.1, VISA Open and VISA
Close VIs are not actually necessary. For good programming practice, however, both should be used especially if more than
one interface is being addressed at a time.
7
A timeout is the amount of time a VI will attempt to perform its intended function. If unable to do so the VI returns an
error message. This behavior is definitely better than hanging the computer indefinitely. In LabVIEW if a timeout control is
left unwired, G will usually use a default value.

5-16