Programming Machine Interfacing
Programming Machine Interfacing
Introduction
In 1965, Hewlett-Packard designed the Hewlett-Packard
Interface Bus ( HP-IB ) to connect their line of programmable
instruments to their computers. Because of its high transfer
rates (nominally 1 Mbytes/s), this interface bus quickly gained
popularity. It was later accepted as IEEE Standard 488-1975,
and has evolved to ANSI/IEEE Standard 488.1-1987. Today,
the name General Purpose Interface Bus (GPIB) is more
widely used than HP-IB. ANSI/IEEE 488.2-1987 strengthened
the original standard by defining precisely how controllers and
instruments communicate. Standard Commands for
Programmable Instruments (SCPI ) took the command
structures defined in IEEE 488.2 and created a single,
comprehensive programming command set that is used with
any SCPI instrument. Figure 1 summarizes GPIB history.
RS-232
Parallel
Serial
Max. Distance 2 m
Up to 50 m
Expensive
Inexpensive
VISA provides complete access to GPIB, LAN, USB, RS-232, VXI messagebased, and VXI register-based products.
Reading Strings
The following code will display a string in a text box as sent
by a DMM:
Dim strData As String
instrument.WriteString "Meas?"
strData = instrument.ReadString()
Text1.Text = strData
The command WriteString tells the instrument to see if
there is a measurement to be recorded. The command
ReadString writes the reply of the Meas? query as a string
into strData. The data is written to a text box that you
create.
Writing Strings
If you have a string to begin with, writing a string to an instrument is
straightforward. The following code writes a string to the instrument
display:
instrument.WriteString ":syst:beep;:disp:text 'LOVE AGILENT'
Note that in this case, the string must be in between single quotes in
order for the instrument to recognize the string. Alternately, you
may set the display text to the expression in a text box for more
flexibility. The ReadString and WriteString commands are used
with IEEE488 ASCII strings and they must follow the 488.2 ASCII
string rules. For example:
instrument.WriteString ":syst:beep;:disp:text " & "'" & Text2.Text & "'"
Reading Numbers
Converting the string representation into number format (i.e. double, single,
integer or long) is relatively simple. Change the type of the data variable to the
type of preference. For example:
Dim dblData As Double
Dim strReply As String
instrument.WriteString "Meas?"
strReply = instrument.ReadString()
dblData = Val(strReply)
Text3.Text = dblData
Notice that we use the Val method with the ReadString command. This is
needed in order to stay consistent with a computer's geographic setting.
Depending on your geographical location and computer language preferences,
different countries have different data syntax. The Val command translates a
string into a double consistent with the instrument use of a decimal radix.
Consult later tutorials for more information concerning this localization issue.
Writing Numbers
Use Str$ (string_name) to convert a double to a string.
instrument.WriteString ":syst:beep;:disp:text " & "'" & Str$(14.2) & "'
Similar to ReadString and WriteString, there are also ReadNumber and WriteNumber commands. The following code writes 0.09 to the null register of
a DMM, reads back that number and displays it in a textbox.
Dim dblNum As Double
Dim offset As Double
offset = 0.09
With instrument
.WriteString "CALC:FUNC null
.WriteString "CALC:STAT on"
.WriteString "CALC:NULL:OFFS ", False
.WriteNumber offset, VisaComLib.IEEEASCIIType.ASCIIType_BSTR
.WriteString "CALC:NULL:OFFS?"
dblNum = .ReadNumber()
End With
Text4.Text = dblNum
In this example, we wrote the double offset to the instrument using the WriteNumber command. Before writing the number to the DMM, the DMM should
be set up to ensure proper execution. The .WriteString "CALC:FUNC null" and .WriteString "CALC:STAT on" lines set up the DMM by enabling the math
function. Both of these lines are executed in memory.
The most interesting line in the code, .WriteString "CALC:NULL:OFFS ", False, writes a null value to the null register that reserves space to write the number
offset. The second argument in WriteString indicates whether or not to flush the first argument's register. By not flushing the register, we are able to use
WriteNumber to add offset as a string to the previous command.
Finally, the .WriteString "CALC:NULL:OFFS?" code checks to see if there is a value in the null register, and the ReadNumber command reads that value. The
code should display 0.09 in a text box. Consult your instrument's user guide for more information about operation commands.
LAN Client:
With a LAN compatible instrument such as the 33220A, connect the
instrument to the same network as the computer with a standard LAN
cable.
Depending on the instrument, you will need the IP address or the Host
Name of the instrument. Most Agilent instruments configure its own
unique IP address while older instruments allow you to set the IP address
of your instrument as long as it corresponds with the same subnet of your
computer. Consult your instruments user manual to get the DHCP Ethernet
address or Host Name. DHCP should be turned on when connecting to
a LAN instrument over a network.
Newer instruments such as the 33220A are web-enabled and have a
webpage containing information catering to your Agilent instruments. To
access the webpage, open up a web browser. In the URL text field type the
IP address of your instrument. A webpage should pop up containing
information about your instrument. Note the IP address of the instrument.
Test your connection with the instrument by clicking Test Connection. The address of the
instrument is the VISA address. Use the VISA address to call it using Visual Basic.
Dim ioMgr As VisaComLib.ResourceManager
Dim instrument As VisaComLib.FormattedIO488
Set ioMgr = New VisaComLib.ResourceManager
Set instrument = New VisaComLib.FormattedIO488
Set instrument.IO = ioMgr.Open("TCPIP0::169.254.2.20")
Note: If you had to disconnect your PC from the internet to connect your PC to your instrument and
are having problems viewing the web-enabled instrument page, follow these steps to access the
instruments webpage. Open a web browser and click Tools > Internet Options on the menu bar.
Select the Connections tab and click LAN Settings. Check the Automatically Detect Settings
box and uncheck all other boxes. Press Ok twice. Type in the IP address of your instrument in the
Address text field and your web browser will take you to your instrument's web enabled page.
Optional: To make searching for connections faster, add the instrument's IP address to your hosts
list. In Windows NT or 2000, open C:winntystem32drivers tchosts. In Windows XP, open
C:WINDOWSystem32drivers tchosts.
LAN/GPIB Converter:
Connecting to an instrument using a LAN/GPIB
converter such as the Agilent E5810
LAN/GPIB Gateway with a GPIB cable is easy
with the latest edition of the Agilent IO
Libraries Suite 14. First, find the IP address of
the LAN/GPIB converter. An Agilent E5810
LAN/GPIB gateway will automatically
reconfigure itself while older models need to
be configured. Consult your LAN/GPIB
converter's user manual for instructions to do
so.
Click the IO icon in the system tray and select Agilent Connection Expert. On
the top toolbar, click Add Interface. Highlight Remote GPIB and click Add.
You can connect to the LAN/GPIB converter using one of two ways.
1. Click Find Interfaces. Ensure that the box next to Lookup hostnames is
checked, and click Find Now. After it is finished searching for instruments,
highlight the appropriate hostname. Click Ok.
2. Click the bullet next to IP address. Enter the IP address of the instrument.
USB Cable
Connect the cable from your PC to the
instrument. The Found New
Hardware Wizard should
automatically open on your
computer. Select all defaults to install
the appropriate drivers for the USB
cable. Click Finish.
USB/GPIB Converter
Connecting to an instrument using an Agilent 82357A USB to GPIB converter
is relatively straightforward. Connect your GPIB end of the UBS/GPIB
converter to your instrument. Plug the USB end of the cable into your
computer. Windows will automatically detect the connection and begin
installing the appropriate drivers. Accept all defaults. Click Finish when it is
done installing.
The USB to GPIB connection is established as a GPIB connection. Like always,
you can check your connections in Agilent Connection Expert. In Visual
Basic, the address of the instrument is something like GPIB2::10::INSTR.
Dim ioMgr As VisaComLib.ResourceManager
Dim instrument As VisaComLib.FormattedIO488
Set ioMgr = New VisaComLib.ResourceManager
Set instrument = New VisaComLib.FormattedIO488
Set instrument.IO = ioMgr.Open("GPIB2::10::INSTR")
Sleep
For simple pauses that are several hundred milliseconds or
longer you can use the Sleep API. Call the function with
Sleep 1000 for a 1 second delay after declaring the
function. Access this function anywhere inside the code
after it is declared. For example:
Public Declare Sub Sleep Lib "kernel32" Alias "Sleep"
(ByVal dwMilliseconds As Long)
Private Sub Command1_Click()
Debug.Print "Do you like to sleep?"
Sleep 1000 ' 1 sec sleep
Debug.Print "Yes."
End Sub
Delay
For short delays of fractional seconds, or for a more accurate timer, use
the timeGetTime Win API. timeGetTime returns the number of
milliseconds since you started Windows. Use this function to create a
delay function. The delay function uses the timeGetTime API as a
reference to count the delay. Paste the following code into a module.
Then when you want a delay of 50 milliseconds call the delay routine
like this: delay 50.
Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Public lngStartTime As Long
Public Sub delay(msdelay As Long)
lngStartTime = timeGetTime()
Do Until (timeGetTime() - lngStartTime) > msdelay
Loop
End Sub
Communicating with
Instruments using RS-232
When it comes to interfaces RS-232 is
pretty inexpensive to incorporate and
over the years many instruments
have been designed with an RS-232
interface. To use the RS-232 interface
make sure it is enabled (selected)
and determine the proper cables to
connect the computer to the
instrument. Finally, set and verify the
RS-232 communication parameters.
Cabling
To connect the instrument to a computer, you must have the proper interface cable.
Most computers are DTE (Data Terminal Equipment) devices. Most instruments are
also DTE devices. To connect these, you must use a DTE-to-DTE interface cable.
These cables are also called null-modem, modem-eliminator, or crossover cables.
The interface cable must also have the proper connector on each end and the
internal wiring must be correct. Connectors are typically a "D" style with 9 pins or
25 pins (DB-25 connector) with a "male" or "female" pin configuration. A male
connector has pins inside the connector shell and a female connector has holes
inside the connector shell. Most Agilent instruments use a 9-pin configuration. The
Agilent 34398A serial cable with a 9-pin female connectors will work with these
instruments.
If you cannot find the correct cable for your configuration, you may use a wiring
adapter. If you are using a DTE-to-DTE cable, make sure the adapter is a "straightthrough" type. Typical adapters include gender changers, null-modem adapters,
and "D" style adapters with 9 pins or 25 pins. The Agilent 34398A Cable Kit
contains cables and an adapter to help you connect serial interfaces.
Configuration
Both the PC and the instrument must be set to
use the same configuration for serial
communications. In general, the instrument
will be the limiting factor for parameter
setting. Newer instruments often allow you to
set parameters, typically through the front
panel. Older instruments often have a fixed
set of parameters. Refer to the instrument's
documentation to determine settings for the
following parameters. Here is a table of
typical parameter values:
You can write Visual Basic code that can set RS232 parameters for you. First, connect
to the instrument similarly to connecting to an instrument using GPIB, LAN or USB
interfaces.
Dim mgr As AgilentRMLib.SRMCls
Dim instrument As VisaComLib.FormattedIO488
Set mgr = New AgilentRMLib.SRMCls
Set instrument = New VisaComLib.FormattedIO488
Set instrument.IO = mgr.Open("ASRL1::INSTR)
Notice how the interface family for RS232 is ASRL. Using RS232 requires you to use the
Serial Interface, ISerial, built in the VISA COM libraries. Use this interface to set the
RS232 parameters.
Dim sfc As VisaComLib.ISerial
Set sfc = instrument.IO
sfc.BaudRate = 9600
sfc.FlowControl = ASRL_FLOW_DTR_DSR
Termination Character
For some instruments a termination character is
required for reading and writing data. A
termination character is generally a non-printing
character that signals the end of the data. For
RS-232 most instruments require a line feed
character (ASCII 10) or a carriage return
character (ASCII 13). Check your instrument
documentation to be certain.
instrument.IO.TerminationCharacter = 10
instrument.IO.TerminationCharacterEnabled = True
Command Delays
For RS-232 communications, the timing of instrument commands can become an
important consideration. Consider a sequence of instrument instructions like this:
instrument.WriteString "Command1?"
instrument.WriteString "Command2?",value
This kind of sequence can sometimes present a problem, particularly for RS232. The
difficulty lies in the WriteString command. Command1 could take longer to complete on
the instrument than the PC processor takes to send Command2. This in turn can send the
command before the instrument is ready.
On GPIB this isn't a problem because there is a specific bus handshake to keep things
straight. On RS232 this is more difficult (although handshakes do exist). RS-232
handshakes are more oriented toward getting the bytes across the bus than the
command level as a whole.
It is often necessary to insert a delay between the WriteString and the ReadString
commands. It is better to delay after every command since RS232 is slow and waiting.
Refer to the Delays and waits in VB article for more information about wait methods.
Troubleshooting
Here are a few things to check if you are having problems
communicating over the RS-232 interface.
On the instrument, verify that the RS-232 interface is selected
(GPIB is the default interface on many instruments). Then,
verify that the instrument and your computer are configured for
the same baud rate, parity, and number of data bits.
Verify that you have connected the correct interface cable and
adapters. Even if the cable has the proper connectors for your
system, the internal wiring may not be correct.
Verify that you have connected the interface cable to the
correct serial port on your computer (COM1, COM2, etc).
Ensure that every command has the proper time to execute.
Delay statements may be needed.
Note: This section illustrates the use of instruments that support the SCPI and
IEEE-488.2 Common Commands. For older, non-SCPI instruments, consult the
instrument documentation.