Professional Documents
Culture Documents
Matthew Otis
Texas A&M University
Instructor’s Portion
Summary
This lab will show students why there is a need for instrument control, the
software architecture to control it, and how to do basic serial
communication. The main focus of this lab is to teach students how to
communicate with instruments and how to use NIVISA to communicate
with instruments using different communication protocols (Serial, GPIB,
etc.). The students will also gain LabVIEW experience by making several
VIs.
Uses
This experiment can be used throughout all engineering and science
disciplines as long as there is a need for serial communication with an
external device.
Equipment List
A computer with Windows, Macintosh, Linux, Sun, or HPUX
(individual system requirements can be found at:
http://www.ni.com/labview/lv_sysreq.htm).
LabVIEW 6i or better (ie LabVIEW 6.1 PDS part number 776678
03).
RS232 Serial Port on the computer being used.
RS232 Cable (part number 18223801).
NI Instrument Simulator (part number 77736901) or other Serial
Device.
VISA and Serial Communication 1
Paperclip or a piece of wire.
Setup
This lab is written under the presumption that there will be one NI
Instrument Simulator for each workstation. However you may be able to
work around this and have less than one NI Instrument Simulator per
station. The students could build the VIs on one computer, transport the
VI via disk, and then attempt to run it on another computer that is
connected to the NI Instrument Simulator.
While the lab procedure could be fairly easily adapted for use with another
instrument, it is designed for the NI Instrument Simulator. Most of the
basic procedures and concepts will remain the same regardless of the
instrument, however many of the specifics will vary with use of a different
instrument
Before the Day of Lab
1. Install LabVIEW (see the LabVIEW Release Notes for your version of
LabVIEW).
2. Ensure that the NI Instrument Simulator has power and that the power
light is lit when turned on.
3. Conduct a runthrough of the lab procedure the students will perform.
On the day of Lab
1. Make sure everything is powered on before commencing the lab.
References
Bishop, Robert H. (2001), LabVIEW Student Edition 6i, Prentice
Hall Inc. Upper Saddle River, NJ., Section 10
VISA and Serial Communication 2
Student’s Portion
Introduction
A typical laboratory setup will use multiple instruments to conduct an
experiment. Some common examples of instruments are oscilloscopes,
digital multimeters, and function generators, but an instrument can be
anything used to record, measure, or control. While the functionality of
each instrument is different, most instruments have two common
components:
User Interface The user interface is commonly made up of dials, buttons,
or knobs for the user to provide input to the instrument, and display
screens or digital readouts for the user to see the output from the
instrument.
Communication Protocol In this experiment we will look at one of the
most common and simplest of communication protocols known as serial
communication. Serial is very useful for smaller amounts of data, and is
conceptually quite easy to understand.
Varied protocols for communication such as serial, General Purpose
Interface Bus (GPIB), VXI, etc. are available to engineers and scientists.
However, they are all inherently different, rendering each protocol
incompatible with any other. Industry has been working to make a
standardized system that can operate all of these devices. This standard
has become known as Virtual Instrument Software Architecture (VISA).
We will use VISA within LabVIEW to communicate via serial. VISA can
serve to greatly simplify the making of many programs due to its
versatility across communication protocols.
Objective
Understand the benefits of VISA and when to use it.
Learn how serial communication works.
Learn when to use serial communication.
Learn how to use LabVIEW to communicate with serial instruments.
VISA and Serial Communication 3
Theory
Serial Communication
PC Serial Port
RS-232 Instrument
RS-232 Cable
Figure 1: An RS232 Instrument is connected to the PC Serial Port with an RS232
Cable.
Serial communication is a popular means of transmitting data between a
computer and a peripheral device such as a programmable instrument or
even another computer. Serial communication uses a transmitter to send
data, one bit at a time, over a single communication line to a receiver. You
can use this method when data transfer rates are low or you must transfer
data over long distances. Serial communication is popular because most
computers have one or more serial ports, so no extra hardware is needed
other than a cable to connect the instrument to the computer or two
computers together.
Serial communication requires that you specify the following four
parameters: the baud rate of the transmission, the number of data bits
encoding a character, the sense of the optional parity bit, and the number
of stop bits. Each transmitted character is packaged in a character frame
that consists of a single start bit followed by the data bits, the optional
parity bit, and the stop bit or bits. The Figure 5 shows a typical character
frame encoding the letter m.
Figure 2: A byte is sent as a package including a start bit, data bits, sometimes a parity
bit, and stop bits.
VISA and Serial Communication 4
Baud rate is a measure of how fast data are moving between instruments
that use serial communication. RS232 uses only two voltage states, called
MARK and SPACE. In such a twostate coding scheme, the baud rate is
identical to the maximum number of bits of information, including control
bits that are transmitted per second.
MARK is a negative voltage, and SPACE is positive. The previous
illustration shows how the idealized signal looks on an oscilloscope.
The following is the truth table for RS232:
Signal > +3 V = 0
Signal < –3 V = 1
The output signal level usually swings between +12 V and –12 V. The
dead area between +3 V and –3 V is designed to absorb line noise.
A start bit signals the beginning of each character frame. It is a transition
from negative (MARK) to positive (SPACE) voltage. Its duration in
seconds is the reciprocal of the baud rate. If the instrument is transmitting
at 9600 baud, the duration of the start bit and each subsequent bit is about
0.104 ms. The entire character frame of eleven bits would be transmitted
in about 1.146 ms.
Data bits are transmitted upside down and backwards. That is, inverted
logic is used, and the order of transmission is from least significant bit
(LSB) to most significant bit (MSB). To interpret the data bits in a
character frame (Figure 2), you must read from right to left and read 1 for
negative voltage and 0 for positive voltage. This yields 1101101 (binary)
or 6D (hex). An ASCII conversion table (Figure 3) shows that this is the
letter m.
VISA and Serial Communication 5
Figure 3: Table with Hex values for ASCII characters.
An optional parity bit follows the data bits in the character frame. The
parity bit, if present, also follows inverted logic, 1 for negative voltage and
0 for positive voltage. This bit is included as a simple means of error
handling. You specify ahead of time whether the parity of the transmission
is to be even or odd. If the parity is chosen to be odd, the transmitter then
sets the parity bit in such a way as to make an odd number of ones among
the data bits and the parity bit. This transmission uses odd parity. There
are five ones among the data bits, already an odd number, so the parity bit
is set to 0.
The last part of a character frame consists of 1, 1.5, or 2 stop bits. These
bits are always represented by a negative voltage. If no further characters
are transmitted, the line stays in the negative (MARK) condition.
The transmission of the next character frame, if any, is heralded by a start
bit of positive (SPACE) voltage.
Knowing the structure of a character frame and the meaning of baud rate
as it applies to serial communication, you can calculate the maximum
transmission rate, in characters per second, for a given communication
setting. This rate is just the baud rate divided by the bits per frame. In the
previous example, there are a total of eleven bits per character frame. If
the transmission rate is set at 9600 baud, you get 9600/11 = 872 characters
per second. Note that this is the maximum character transmission rate. The
hardware on one end or the other of the serial link might not be able to
reach these rates, for whatever reason.
VISA and Serial Communication 6
There are many different recommended standards of serial port
communication, including the following most common types.
RS232
This standard has been adopted for use in serial communications between
computers and printers, modems, and other equipment. The serial ports on
standard IBMcompatible personal computers follow RS232. Figure 4
shows what an RS-232 connector looks like. Figure 5 entails details about
what each pin does.
Figure 4: RS232 Serial Ports contain 9 different pins each of which has its own separate
function.
RS449, RS422, RS423
The RS449, RS422, and RS423 are additional EIA serial
communication standards related to RS232. RS449 was issued in 1975
VISA and Serial Communication 7
and was supposed to supersede RS232, but few manufacturers have
embraced the newer standard.
VISA
Figure 6: VISA is able to communicate with all of these different forms of
communication by calling on their individual lowlevel drivers. This allows for faster
software development time.
Virtual Instrument Software Architecture (VISA) is the lower layer of
functions in the LabVIEW instrument driver VIs that communicates with
the driver software.
VISA was developed and designed to have the following attributes:
• Maximize ease of use and performance.
• Maintain longterm compatibility with the installed base.
• Maintain multivendor open architectures.
• Maximize multiplatform capability.
• Maximize expandability and modularity in frameworks.
• Maximize software reuse.
• Standardize the use of system software elements.
• Treat instrument drivers as part of the instrument.
• Accommodate established standards.
• Maximize cooperative support of users.
VISA by itself does not provide instrumentation programming capability.
VISA and Serial Communication 8
It is a highlevel Application Programmer Interface (API) that calls in
lowlevel drivers. VISA can control VXI, GPIB, serial, or computerbased
instruments and makes the appropriate driver calls depending on the type
of instrument used. When debugging VISA problems, remember this
hierarchy. An apparent VISA problem could be an installation problem
with one of the drivers that VISA calls.
In LabVIEW, VISA is a single library of functions you use to
communicate with GPIB, serial, VXI, and computerbased instruments.
For example, some instruments give you a choice for the type of interface.
If the LabVIEW instrument driver were written with functions on the
Functions» Instrument I/O»GPIB palette, those instrument driver VIs
would not work for the instrument with the serial port interface. VISA
solves this problem by providing a single set of functions that work for
any type of interface.
VISA Programming Terminology
The functions you can use with a resource are operations. The resource
also has variables, or attributes, that contain information related to the
resource. The following terminology is similar to that used for instrument
driver VIs:
• Resource—Any instrument in the system, including serial and parallel
ports.
• Session—You must open a VISA session to a resource to communicate
with it, similar to a communication channel. When you open a session to a
resource, LabVIEW returns a VISA session number, which is a unique
refnum to that instrument. You must use the session number in all
subsequent VISA functions.
• Instrument Descriptor—Exact name of a resource. The descriptor
specifies the interface type (GPIB, VXI, ASRL), the address of the device
(logical address or primary address), and the VISA session type (INSTR
or Event).
The instrument descriptor is similar to a telephone number, the resource is
similar to the person with whom you want to speak, and the session is
similar to the telephone line. Each call uses its own line, and crossing
these lines results in an error.
VISA and Serial Communication 9
Figure 7: VISA Write writes data to a specified device.
The VISA Write function writes the write buffer string to the device
specified by the VISA resource name. dup VISA resource name returns
the same handle to that session. On UNIX platforms, data are written
synchronously. On all other platforms, it is written asynchronously.
Return count contains the number of bytes actually transferred across the
GPIB. The error in and error out clusters contain the error information.
Figure 8: VISA Read reads the specified number of bytes from specified device or
interface.
The VISA Read function (Figure 8) reads data from the device specified
by the VISA resource name. byte count indicates the number of bytes to
be read into the returned read buffer string. dup VISA resource name
returns the same handle to that session. On UNIX platforms, data are read
synchronously. On all other platforms, it is read asynchronously. return
count contains the number of bytes actually transferred across the GPIB.
The error in and error out clusters contain the error information.
How to use
Use the VIs and functions located on the Functions>Instrument
I/O>Serial palette for serial port communication.
The VISA Write and VISA Read functions work with any type of
instrument communication and are the same whether you are doing GPIB
or serial communication. However, because serial communication requires
you to configure extra parameters, you must start the serial port
communication with the VISA Configure Serial Port VI.
VISA and Serial Communication 10
The VISA Configure Serial Port VI initializes the port identified by VISA
resource name to the specified settings. timeout sets the timeout value
for the serial communication. baud rate, data bits, parity, and flow
control specify those specific serial port parameters. The error in and
error out clusters maintain the error conditions for this VI.
Pre-Lab Preparation
Read the theory and lab procedure portion of this experiment.
Bring with you:
Pen or Pencil.
A formated 3.5inch floppy disk.
This experiment.
Workstation Details
Computer with LabVIEW Software
A regular RS232 serial cable
NI Instrument Simulator or another Serial Device
Piece of wire or paperclip
Lab Procedure
1. In order to understand how serial is working, you must first understand
what serial is working with. A serial byte is sent as a packet of
information and that information includes start, data, and stop bits.
a. Search online, and try to find a table with the binary values for the
individual ASCII characters (or you can convert the Hex values for
ASCII characters from Figure 3 into the needed binary values).
b. On your data sheet draw out what your initials would look like if
they were sent in a serial transmission with one start bit, eight data
bits, no parity bit, and one stop bit
(i.e. N > Hex: 4E = Bin:01001110 & I > Hex: 49 = Bin:01001001
N >
VISA and Serial Communication 11
I >
).
2. For you to gain a basic understanding of the way serial communication
occurs you are going to construct a simple test called a loopback test.
A loopback test causes the signal received to simply be the same
signal that was sent.
You are going to send a simple signal out, and then receive the same
signal back and read it. A loopback test sends the outgoing signal
back into the line for the incoming signal by making an external
connection on the serial cable. This can be useful for seeing if a serial
connection or cable is in proper working order.
First we will build a VI that uses Virtual Instrument Software
Architecture (VISA) to communicate with the serial port.
a. Open LabVIEW, create a new VI and go to the block diagram
(CtrlE). Then right click on the block diagram and go to Functions >
Instrument I/O > Serial and place VISA Configure Serial Port,
VISA Write, VISA Read, VISA Close on the block diagram.
b. Place Time & Dialog > Simple Error Handler VI to indicate if
any errors occurred.
VISA and Serial Communication 12
Figure 9: Possible front panel for Serial LoopBack Test.vi
Figure 10: Block Diagram for Serial LoopBack Test.vi
c. VISA Resource Name, Write Buffer, Read Buffer, and Return
Count can all be created by using the wiring tool and right clicking on
their respective nodes and either creating an indicator or a control
depending upon what is needed.
d. Save the VI on your 3.5inch floppy disk as Serial LoopBack
Test.vi.
The structure of this VI does the following:
1. Configures the serial port and prepares for data transmission.
2. Writes the desired string, converts the string to the proper bit
structure required for sending including the proper start, parity and
stop bits.
3. Reads the same number of bytes as sent, decodes the data from
VISA Write from a binary string back into ASCII then to text as
seen in the indicator
4. Closes the VISA session
5. Reports if any errors have occurred and where they occurred.
VISA and Serial Communication 13
To do the actual loopback test we will need to connect the sending
and the receiving lines together. Refer to the above pinout in Figure 7
and the information from Figure 8 to see which lines to will need to be
connected. If you properly do the loopback test, you should receive
the same information you sent.
e. Now run the VI and use whatever string you wish in the write
buffer. Once the VI has run, make a screenshot of both the completed
front panel and block diagram.
3. Now that you know how to make a simple VI to communicate using
the serial port, you will use those skills and to construct a VI that can
communicate with an actual instrument.
Figure 11: These switch settings configure the instrument as a serial device with the
following settings:
Baud rate = 9600
Data bits = 8
Parity = no parity
Stop bits = 1
Flow control parameters = hardware handshaking
a. Turn the power off on the NI Instrument Simulator and configure the
switches according to Figure 11. Once the switches are set connect the
serial cable to the NI Instrument Simulator and turn the power on for
the NI Instrument Simulator. (To avoid errors, screw the cable in
place so it does not become unplugged.)
VISA and Serial Communication 14
Figure 12: Possible Front Panel for Instrument Simulator Write & Read.vi.
Figure 13: Block Diagram for Instrument Simulator Write & Read.vi.
b. Now resave the previous VI as Instrument Simulator Write & Read.vi.
You will modify the block diagram to look like that of Figure 13.
i. All of the VISA commands can be found in Functions >
Instrument I/O > Serial.
ii. Functions > String is where Line Feed Constant,
Concatenate Strings, and Scan From String are located
iii. VISA Resource Name, Write Buffer, and Read Buffer,
were all created by right clicking on their respective nodes
and either creating an indicator or a control depending upon
what is needed.
The basic structure of this VI is the same as the previous with a few
pointed differences:
VISA and Serial Communication 15
1. The NI Instrument Simulator requires a linefeed as the end of string
character.
2. The first VISA Read reads a string that tells the number of characters
that will be returned by the device for the response to the query.
3. Scan From String converts the number shown in the first read string
into an actual number so that the next VISA Read knows how many
characters to expect.
4. VISA Read then receives the response with the proper # of characters.
In order for the handshaking to properly work, you will need to configure
your VISA port such that it is using RTS/CTS protocol (this is the
protocol used by the NI Instrument Simulator). To ensure this protocol is
being used you will need to create the proper constant on VISA Configure
Serial Port. RTS and CTS refer to lines 7 and 8 respectively on a serial
port with RS232 configuration.
On the front panel put *IDN? in the write buffer (“”). This is a standard
command that asks an instrument to identify itself. The device should
answer with National Instruments GPIB and Serial Device Simulator Rev
B.1 (or the name of the device in use).
Save and Run the VI. Once the VI has run, make a screenshot of both the
completed front panel and block diagram.
3. The next VI is going to take data from the NI Instrument Simulator,
convert the returned string data into numerical data, and display the
numerical data on a graph so that the data is more easily recognized. The
same VI that we just used can be modified for this part of the lab.
VISA and Serial Communication 16
Figure 14: Possible front panel with data for Instrument Simulator with Graph.vi.
Figure 15: Block diagram for Instrument Simulator with Graph.vi.
a. Open Instrument Simulator Write & Read.vi and save as Instrument
Simulator with Graph.vi.
b. Spreadsheet String To Array can be found under Functions >
String.
i. On Spreadsheet String To Array you will want to create
constants on three of the nodes:
On the delimiter (Tab) put a , in the constant (Delimiter tells
the VI what character to look for so as to break the string into an
array).
On Array Type (2D Format) simply create a constant and do
not populate the array
(This tells the VI what type of array you wish to make from the
string data).
On the format string put %f in the constant.
c. Format Strings can be found in the LabVIEW Help, under Format
Specifier Syntax and Format Specifier Syntax Elements
d. Save the VI as Instrument Simulator with Graph.vi.
VISA and Serial Communication 17
This VI will do the same functions as the previous VI, except that the final
data will be converted from string data to numerical data, so that the data
can be displayed.
e. Type FORM:DATA ASC; SOUR:FUNC SIN; SENS:DATA? in the
write buffer (“”), and run the VI.
f. Once the VI has run, make a screenshot of both the front panel with
the waveform and block diagram.
g. Type FORM:DATA ASC; SOUR:FUNC SQU; SENS:DATA? In the
write buffer (“”), and run the VI.
h. Once the VI has run, make a screenshot of both the front panel with
the new waveform.
i. Look at the front of the NI Instrument Simulator to find some of the
other strings that will result in waveforms.
j. Finish filling in the information on your Data Sheet.
k. Close LabVIEW.
l. Have your instructor verify that you have completed the lab.
m. Logoff of your computer.
Lab Report
This lab report should be an informal report. The lab report will consist of
the normal contents of an informal report: title, introduction, results, and
discussion. The report must be typed. You should include the following in
your lab report:
Staple your data sheet to the back of your report.
All screenshots should be included in your report.
If there were any problems in the lab, discuss them. Why do you think
they occurred?
Write about how you think that this communication can be improved
upon, and what merits you think it currently has.
VISA and Serial Communication 18
Sheet for students to fill out during lab with the data they have taken
(blank tables, questions, and sections for the instructor to initial, etc.).
What do you expect in the lab report? Will the report be formal or
informal?
Data Sheet
1. Serial Transmission of ASCII characters.
a. What are your initials? __ __
b. Draw the serial representation of your initials:
First Initial =
Second Initial =
1. Creating a LoopBack Test
a. What VISA Resource Name do you have to use? ___________
i. Why do you think it is named as such?
ii. What other VISA Resource Names were listed?
b. Which pins did you connect with the wire?
VISA and Serial Communication 19
i. Will anything happen if you connect the wrong pins?
c. Why is VISA being used in this operation?
d. What string did you send?
2. Instrument Simulator Write & Read
a. Which of the LEDs on the NI Instrument Simulator upon power
the instrument on were lit?
b. Which LEDs changed while the VI was running?
c. What characters does the Line Feed Constant represent?
d. What do RTS and CTS stand for?
e. Why is handshaking necessary?
3. Instrument Simulator with Graph
a. If you wanted to change a string into an array, what VI would you
use?
b. What does a delimiter do?
VISA and Serial Communication 20
c. What does %f do?
i. Will any other format string(s) work and yield the same results,
if so which ones? (Hint: There are at least 2 others that will
work)
d. In part 4e, what are the first five data points returned?
i. What is the maximum value for any data point?
e. In part 4f, what are the first five data points returned?
i. What is the maximum value for any data point?
f. _______________________________________________ and
_______________________________________________ are two
of the character strings used to generate other waveforms with the
instrument simulator.
g. The NI Instrument Simulator only has a few sets of stored data
points that it then randomly sends back as a set. How many
possible different sine waves are sent back?
VISA and Serial Communication 21