Vi Module 5
Vi Module 5
MODULE –V
File Input/ Output, String handling, Instrument Control
Syllabus: File Input/ Output: File formats, file write &read, generating filenames automatically,
FILE FORMATS
ASCII Text-Format Files
ASCII text-format files store data in the ASCII format. This is the simplest format and since most
software packages can retrieve data from an ASCII text file, naturally it becomes the most
commonly used format, especially in instrumentation applications.
Binary-Format Files
These files store data in binary form and look like an image of the data stored in the computer’s
memory. These files cannot be read by word processors. Also, it is impossible to retrieve data
from these files without detailed knowledge of the file’s format. The main advantage of these files
is that they are much smaller than text files and reading and writing operations can be done with
little data conversion. Binary file format is useful in applications requiring writing and reading
of large chunks of data due to its size advantage.
LabVIEW measurement files are special files which are either text-based files (.lvm) or binary
files (.tdm or .tdms). Generally, these files are written and read using Express VIs. Here data is
stored as a sequence of records of a single arbitrary data type specified during creation of the file.
The data is indexed in terms of these records. Each record in a measurement file must have the
same data types associated with it.The three basic file input and output (I/O) operations to store
and retrieve data from files are,
As most data is stored (and read) in a spreadsheet format (line-wise tab delimited ASCII)
readymade VIs to handle these are available. The function Write to Spreadsheet File (Fig. 8.2) first
converts a two-dimensional or a one-dimensional array of numbers (by default real numbers of
format x.3f) to a text string separated by tab characters for elements in the same row.
Using the format input one can specify how to convert the numbers to characters. The default is
%.3f, meaning that the decimal numbers will have a precision of three digits after the decimal
point. Various formats are possible using the format specifier syntax for strings. The output, new
file path can be wired to all subsequent writes to the same file as a file handle. Once writing of
Data stored in a spreadsheet file can be read using the Read from Spreadsheet File VI (Fig. 8.3).
When placed on the diagram the instance (double, integer or string) of this VI can be selected using
a pull down menu. One can specify the number of rows to be read from the file. If this input is left
open, all rows of the file are read.
The Write to Binary File function (Fig. 8.6) writes binary data to a new file, appends data to an
existing file, or replaces the contents of a file. The input data can be of any type. The byte order
(big-endian, little-endian, etc).can also be specified for integers, default being big-endian.
The Read from Binary File function (Fig. 8.7) reads binary data from a file and returns it in data.
How the data is read depends on the format of the specified file. The data type input sets the type
of data the function uses to read from the binary file.
STRING HANDLING
STRING FUNCTIONS
Concatenate Strings: The Concatenate Strings function (Fig. 9.2) is useful in concatenating input
strings into a single output string. In addition to simple strings one-dimensional arrays of strings
can also be wired as inputs; the output will be a single string containing a concatenation of all the
strings in the order they are connected, top to bottom, left to right. This function can be used to
concatenate strings to be written as text in files.
Array to Spreadsheet String: This function (Fig. 9.8) converts an array of any
Dimension to a table in string form, containing tabs separating column elements, an EOL character
separating rows, and for arrays of three or more dimensions, headers separating pages.
PARSING OF STRINGS
One of the important uses of string functions is to extract numbers and other data from the given
string. Parsing using a single string function was illustrated in Fig. 9.20. By judiciously choosing
a combination of functions such as String Length, String Subset, Match Pattern and Scan from
String, almost any string parsing is possible. These functions are widely used for parsing,
especially in instrument I/O.
A parsing example is illustrated in Fig. 9.21, where the date/time string obtained from the Format
Date/Time String function is parsed to get day, month, year, weekday and time information. Note
the use of date/time format to get the date as dd/mm/yyyy and time as hh:mm:ss. All parameters
of the date/time format start with a ‘%’ symbol followed by a character to specify the format. Any
other string is ignored by LabVIEW and literally reproduced at the date/string output. The example
illustrates the use of characters such as ‘/’, ‘ ‘, and ‘,’ as separators between the various outputs.
INSTRUMENT CONTROL
Computer-based measurement and automation is popular because our PC provides the platform
we need to make our measurement and automation systems dependable and efficient. Its extensive
processing capabilities empower us to create flexible solutions based on industry standards. With
this flexibility, we can adjust our application specifications more easily than with traditional tools.
National Instruments software, including LabVIEW and Measurement Studio, delivers PC based
data analysis, connectivity, and presentation power to new levels in measurement and automation
applications. National Instruments hardware and software connect the computer to your
application. By providing an extensive hardware selection, including data acquisition and signal
conditioning devices, instrument control interfaces (such as GPIB, Serial, VXI and PXI), image
acquisition, motion control and industrial communications interfaces, National Instruments offers
the widest range of solutions for practically any measurement as shown in Figure 10.1.
When you use PCs to control instruments, you need to understand properties of the instrument,
such as the communication protocols to use. You must consider the following issues with PC
control of instrumentation:
GPIB COMMUNICATION
The ANSI/IEEE Standard 488.1-1987, also known as General Purpose Interface Bus (GPIB),
describes a standard interface for communication between instruments and controllers from
variousvendors. GPIB, instruments offer test and manufacturing engineers the widest selection of
vendorsand instruments for general-purpose to specialized vertical market test applications as
shown in
Figure 10.3 GPIB instruments are often used as stand-alone benchtop instruments where
measurements are taken by hand. You can automate these measurements by using a PC to control
the GPIB instruments.
Termination informs listeners that all data has been transferred. You can terminate a GPIB data
● The GPIB includes an end-or-Identify (EOI) hardware line that can be asserted with the last data
byte. This is the preferred method.
● Place a specific end-of-string (EOS) character at the end of the data string itself. Some
instruments use this method instead of or in addition to the EOI line assertion
● The listener counts the bytes transferred by handshaking and stops reading when the listener
reaches a byte count limit. This method is often used as a default termination method because the
transfer stops on the logical OR of EOI, EOS (if used) in conjunction with the byte count. Thus,
you typically set the byte count to equal or exceed the expected number of bytes to be read.
To achieve the high data transfer rate that the GPIB was designed for, you must limit the number
of devices on the bus and the physical distance between devices. You can obtain faster data rates
with HS488 devices and controllers. HS488 is an extension to GPIB that most NI controllers
support.
HARDWARE SPECIFICATIONS
The GPIB is a digital, 24-conductor parallel bus. As shown in Figure 10.4, it consists of eight data
lines (DIO 1-8), five bus management lines (EOI, IFC, SRQ, ATN, REN), three handshake lines
(DAV, NRFD, NDAC), and eight ground lines. The GPIB uses an eight-bit parallel, byte-serial,
asynchronous data transfer scheme. This means that whole bytes are sequentially handshaked
across the bus at a speed that the slowest participant in the transfer determines. Because the unit
of data on the GPIB is a byte (eight bits), the messages transferred are frequently encoded as ASCII
character strings.
Additional electrical specifications allow data to be transferred across the GPIB at the maximum
rate of 1 MB/sec because the GPIB is a transmission line system. These specifications are:
● A maximum separation of 4 m between any two devices and an average separation of 2 m over
the entire bus.
of the devices powered on. If you exceed any of these limits, you can use additional hardware to
extend the bus cable lengths or expand the number of devices allowed.
SOFTWARE ARCHITECTURE
The software architecture for the instrument control using LabVIEW is similar to the architecture
for DAQ. Instrument interfaces such as GPIB include a set of drivers. Use MAX to configure the
interface. VISA, Virtual Instrument Software Architecture, is a common API to communicate with
the interface drivers and is the preferred method used when programming for instrument control
in LabVIEW.
Underneath and between the different hardware components are the software components that are
the heart of your instrumentation system. Figure 10.5 shows the software architecture for Windows
2000/XP from the perspective of the computer. The instrument is connected to the computer
through a built-in connector such as RS-232 or an installed interface board such as GPIB or
VXI/MXI. Driver-level software for instrument is in the form of a Dynamic Link Library (DLL).
In the example shown in the figure, LabVIEW is the high-level application software layer.
LabVIEW includes built-in functions for GPIB, serial, VXI, and computer-based instruments that
load and use the installed driver software.
As with all Windows device drivers, any DLLs you install for interface boards will also interact
with the Windows Registry so that resources such as base addresses, interrupt levels and DMA
channels can be assigned. Configuration and diagnostic software tools are also available with
National Instruments hardware.
Configure the objects listed in MAX by right-clicking each item and selecting an option from the shortcut
menu. The Measurement and Automation Explorer (MAX) allows you to configure the GPIB card and
search for instruments, set the VISA alias name, run NI-Spy and configure IVI instrument drivers. If a
GPIB+ card is installed, you can run the GPIB analyzer software from MAX.
The Instrument I/O Assistant is a LabVIEW Express VI which you can use to communicate with
message-based instruments and convert the response from raw data to an ASCII representation.
You can communicate with an instrument that uses a serial, Ethernet, or GPIB interface.
The Instrument I/O Assistant shown in Figure 10.7 is located on the Functions»Input and
Functions»All Functions»Instrument I/O palettes is a LabVIEW Express VI that allows you to
easily test communication with your instrument and develop a sequence of query, parse and write
steps. These steps can be saved as an Express VI for instant use or can be converted to a LabVIEW
subVI. Use the Instrument I/O Assistant when an instrument driver is not available.
To launch the Instrument I/O Assistant, place the Instrument I/O Assistant Express VI on the block
diagram in LabVIEW. The Instrument I/O Assistant Express VI is available in the Instrument I/O
category of the Functions palette. The Instrument I/O Assistant configuration dialog box appears.
If it does not appear, double-click the Instrument I/O Assistant icon. Complete the following steps
to configure the Instrument I/O Assistant. Step 1: Select an instrument. Instruments that have been
configured in MAX appear in the Select an instrument pull-down menu. Step 2: Choose a Code
generation type. VISA code generation allows for more flexibility and modularity than GPIB code
generation. Step 3: Select from the following communication steps using the Add Step button:
● Query and Parse—Sends a query to the instrument, such as *IDN? and parses the returned string.
This step combines the Write command and Read and Parse command.
● Read and Parse—Reads and parses data from the instrument. Step 4: After adding the desired
number of steps, click the Run button to test the sequence of communication that you have
configured for the Express VI. Step 5: Click the OK button to exit the Instrument I/O Assistant
configuration dialog box.
Once you have placed the I/O Assistant on the block diagram, the wizard opens. The wizard starts
in the Select Instrument step, where you can choose a GPIB or serial instrument. You also can
check the interface properties from this window. After selecting the instrument, you can add
sequences to Query and Parse, Write, or Read and Parse. In addition, after you have set up a
communication sequence with one instrument, you can set up additional instruments in the same
Express VI. In Figure 10.8, a GPIB instrument was set up, then a query (*IDN?) was sent to the
instrument. The response was automatically parsed, resulting in a string output.
VISA
Virtual Instrument Software Architecture (VISA) is the lower layer of functions in the LabVIEW
instrument driver VIs that communicates with the driver software. VISA by itself does not provide
instrumentation programming capability. VISA is a high-level API that calls low-level drivers. As
shown in Figure 10.9 VISA can control VXI, GPIB, serial, or computer-based instruments and
makes the appropriate driver calls depending on the type of instrument used.
In LabVIEW, VISA is a single library of functions you use to communicate with GPIB, serial,
VXI and computer-based instruments. You do not need to use separate I/O palettes to program an
instrument. For example, some instruments give you a choice for the type of interface. If the
LabVIEW instrument driver was written with functions on the Functions»All
Functions»InstrumentI/O»GPIB palette, those instrument driver VIs would not work for the
instrument with the serial port interface.
VISA or Virtual Instrument Software Architecture is a protocol built upon 488.2 driver and
functions to meet the industry needs for having a way to easily interface with multiple I/Os and
have all manufacturers of instruments and instrument drivers follow a protocol. VISA created by
the VXIplug&play Alliance which is composed of the top 35 instrument manufacturers such as
HP. National Instruments is a leading member of the alliance.
The most important objects in the VISA language are known as resources. The functions you can
use with an object are known as operations. In addition to the operations that you can use an object,
the object has variables, known as attributes, associated with it that contains information related
to the object. Three terms need definitions and 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.
The most commonly used VISA communication functions are the VISA Write and VISA Read
functions.
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. The VISA Configure Serial Port VI opens
communication with COM2 and sets it to 9,600 baud, eight data bits, odd parity, one stop bit and
XON/XOFF software handshaking. Then, the VISA Write function sends the command. The VISA
Read function reads back up to 200 bytes into the read buffer, and the Simple Error Handler VI
checks the error condition. The VIs and functions located on the Functions»All
Functions»Instrument I/O»Serial palette are also used for parallel port communication. You
specify the VISA resource name as being one of the LPT ports.
INSTRUMENT DRIVERS
LabVIEW provides more than 1200 LabVIEW instrument drivers from more than 50 vendors.
You can use these instrument drivers to build complete systems quickly. Instrument drivers
drastically reduce software development costs because developers do not need to spend time
programming their instruments.
The structure of an instrument driver is shown in Figure 10.10. The high-level functions are built
from the lower-level functions. For the most control over the instrument, you would use the lower-
level functions. However, the high-level functions like the Getting Started VI you used in the
previous lesson are easy to use and have soft front panels that resemble the instrument.
As shown in Figure 10.11 an instrument driver VI initializes the DMM with its VISA Alias, uses an
Application Example VI to configure and read data from the meter, and closes the instrument, and then the
error status is checked.
Configure—A collection of VIs that configure the instrument for the operations you want to
perform. An example is a function to set up the trigger rate.
Action/Status—Contains both action and status VIs. Action VIs cause the instrument to initiate
or terminate test and measurement operations. Status VIs obtain the current status of the instrument
or the status of pending operations. An example of an action function is Acquire Single Shot. An
example of a status function is Query Transfer Pending.
Data—VIs that transfer data to or from the instrument such as reading a measured waveform from
the instrument or downloading a waveform to the instrument.
Utility—VIs that perform a wide variety of useful functions such as reset, self-test, error query
and revision query.
Close—Terminates the communication channel to the instrument and de-allocates the resources
set aside for that instrument.
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 your instrument to the computer (or two
computers together) as shown in Figure 10.12
You must specify four parameters for serial communication: the baud rate of the transmission, the
number of data bits that encode a character, the sense of the optional parity bit, and the number of
stop bits. A character frame packages each transmitted character as a single start bit followed by
the data bits. The baud rate is a measure of how fast data moves between instruments that use serial
communication. The baud rate informs how many bits are transferred per second on the serial
cable.
To interpret the data bits in a character frame as shown in Figure 10.13, you must read from right
to left and read 1 for a negative voltage and 0 for a positive voltage. An optional parity bit follows
the data bits in the character frame. The parity bit, if present, also follows inverted logic. This bit
is included as a means of error checking. You specify ahead of time for the parity of the
transmission to be even or odd. If you choose for the parity to be odd, the parity bit is set in such
a way so the number of 1s add up to make an odd number among the data bits and the parity bit.
RS-232 uses only two voltage states called MARK and SPACE. In such a two-state 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 following is
the truth table for RS-232:
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.
Use the I/O Assistant just like with GPIB communication as shown in Figure 10.14.
You can calculate the maximum transmission rate in characters per second for a given
communication setting by dividing the baud rate by the bits per character frame.
There are several recommended standards (RS) for serial communication. Each varies in hardware
and software specifications. One must be familiar with their instrument and what connector is used
before you can begin controlling that device with their computer. Figure 10.15 shows serial
hardware connection.
There are three main Serial I/O types that are most common recommended standards of serial port
communication.
● RS-232 (ANSI/EIA-232 Standard) is used for many purposes such as connecting a mouse,
printer, or modem. It is also used with industrial instrumentation. Because of improvements in line
drivers and cables, applications often increase the performance of RS-232 beyond the distance and
speed in the standards list.
● RS-422 (AIA RS-422A Standard) uses a differential electrical signal as opposed to the
unbalanced (single-ended) signals referenced to ground with RS-232. Differential transmission,
which uses two lines each to transmit and receive signals, results in greater noise immunity and
longer transmission distances as compared to RS-232. RS-422 is the Connector found on most
Macs. This is a differential communication method. Connector has 8 pins.
There are devices made to communicate with serial or GPIB instruments through the Ethernet,
USB, or IEEE 1394 (FireWire) ports which bypasses the need for a serial port or GPIB board on
your computer. When using these devices, program them just as you would if they were using the
serial port or a GPIB board. USB and ethernet interfaces transform USB ports or ethernet ports
into asynchronous serial ports for communication with serial instruments. You can install and use
these interfaces as standard serial ports from your existing applications. USB, ethernet, and IEEE
1394 controllers transform any computer with these ports into a full-function, Plug and Play,
IEEE 488.2 Controller that can control up to 14 programmable GPIB instruments.