programming machine interfacing

53
Learn to control instruments with Visual Basic

Upload: warren-o-sanopao

Post on 24-Dec-2015

333 views

Category:

Documents


12 download

DESCRIPTION

GPIB Programming Using VB

TRANSCRIPT

Page 1: Programming Machine Interfacing

Learn to control instruments with Visual Basic

Page 2: 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.

Page 3: Programming Machine Interfacing
Page 4: Programming Machine Interfacing

IEEE 488.2 AND SCPIThe SCPI and IEEE 488.2 standards addressed the limitations and ambiguities of the original IEEE 488 standard. IEEE 488.2

makes it possible to design more compatible and productive test systems. SCPI simplifies the programming task by defining a single comprehensive command set for programmable instrumentation, regardless of type or manufacturer.

The ANSI/IEEE Standard 488-1975, now called IEEE 488.1, greatly simplified the interconnection of programmable instrumentation by clearly defining mechanical, electrical, and hardware protocol specifications. For the first time, instruments from different manufacturers were interconnected by a standard cable. Although this standard went a long way towards improving the productivity of test engineers, the standard did have a number of shortcomings. Specifically, IEEE 488.1 did not address data formats, status reporting, message exchange protocol, common configuration commands, or device-specific commands. As a result, each manufacturer implemented these items differently, leaving the test system developer with a formidable task.

IEEE 488.2 enhanced and strengthened IEEE 488.1 by standardizing data formats, status reporting, error handling, Controller functionality, and common commands to which all instruments must respond in a defined manner. By standardizing these issues, IEEE 488.2 systems are much more compatible and reliable. The IEEE 488.2 standard focuses mainly on the software protocol issues and thus maintains compatibility with the hardware-oriented IEEE 488.1 standard.

SCPI built on the IEEE 488.2 standard and defined device-specific commands that standardize programming instruments. SCPI systems are much easier to program and maintain. In many cases, you can interchange or upgrade instruments without having to change the test program. The combination of SCPI and IEEE 488.2 offers significant productivity gains, and finally, delivers as sound a software standard as IEEE 488.1 did a hardware standard.

Page 5: Programming Machine Interfacing

Why Automated Testing? 1. Test automation enables one to achieve detailed product testing with significant

reduction in test cycle time.2. The efficiency of automated testing incorporated into product lifecycle can generate

sustainable time and money savings.3. Better, faster testing.4. Rapid validation of software changes with each new release of application is possible.

5. Automated testing increases the significance and accuracy of testing and results in greater test coverage.

6. Automated testing offers a level of consistency, which is not achievable through the use of manual testing.

7. Automated testing eliminates the time constraints associated with manual testing. Scripts can be executed at any time without human intervention.

8. Automated test scripts are re-usable, and can be used across varying scenarios and environments.

9. Enhanced productivity.

Page 6: Programming Machine Interfacing

GPIB VS. RS-232GPIB RS-232

Parallel Serial

Fast, data transfer rate 1 MHz Slow, runs at 700 samples/sec

Max. Distance 2 m Up to 50 m

14 Devices can be connected to one GPIB Only one device can be connected to an RS232 cable

Expensive Inexpensive

Page 7: Programming Machine Interfacing

Get started - Using Visual Basic to connect to instruments

The virtual instrument software architecture (VISA) is a multi-vendor standard, ensuring a high degree of interoperability among instrumentation vendors. VISA supports a wide variety of operating systems and instrument interfaces. This guide describes how to use the Agilent VISA COM I/O Library in Visual Basic. There are three basic steps to communicate with an instrument in Visual Basic:

1. Include the appropriate VISA COM libraries.2. Connect to the instrument by finding the I/O address of the

instrument you are using.3. Use the connection to the instrument to do something and read

the result.

Page 8: Programming Machine Interfacing

VISA (Virtual Instrument Systems Architecture) — The VISA library is the industry standard for instrument control and is now maintained by the IVI Foundation. A program written to work with the VISA library will work with implementations of VISA from other vendors. It can be used from any program that can call arbitrary Windows DLLs, such as:

– Agilent VEE– C/C++– MathWorks MATLAB– Microsoft Visual Basic 6– Microsoft Visual Studio .Net (using Visual Basic, Managed C++ or C#)– National Instruments LABVIEW– National Instruments LabWindows/CVI

VISA provides complete access to GPIB, LAN, USB, RS-232, VXI message-based, and VXI register-based products.

Page 9: Programming Machine Interfacing

Step 1: Include the appropriate references

From Visual Basic, go to Project > References. This brings up a dialog box of available references as seen below.

Scroll down until you see the VISA COM 1.0 Type Library and Agilent VISA COM Resource Manager 1.0, check the box and then click the OK button. This adds the reference for the Agilent I/O Utilities library. Having these references gives you benefits such as typing aids for program lines involving VISA COM commands. The Agilent IO Libraries Suite 14 uses VISA COM 3.0. Only in VB 6.0 and the Excel VBA environment can you call the VISA COM 1.0 Type Library and automatically get access to the VISA COM 3.0 Type Library.

Place a command button on the form. Double-clicking on the command button in design mode will bring you to the Form Code window. Alternately, from the menu bar, you may click View > Code.

Page 10: Programming Machine Interfacing
Page 11: Programming Machine Interfacing

Step 2: Connect to the instrumentThe Agilent IO Libraries Suite provides a Global Resource Manager (GRM), which can create any VISA COM

resource installed on the system. Create an instance of the VisaComLib.ResourceManager object and use it to create an object that provides communication to an instrument.

Dim ioMgr As VisaComLib.ResourceManager Set ioMgr = New VisaComLib.ResourceManager

The easiest way to get a VisaCom object for a specific instrument is to use the Open method. The Open method is from the interface IMessage that is indirectly referred to from the FormattedIO488 class through the IO property. For example:

Dim instrument As VisaComLib.FormattedIO488 Set instrument = New VisaComLib.FormattedIO488 Set instrument.IO = ioMgr.Open("GPIB0::10") ' 10 is the instr. address

The above example sets the instrument to a VisaComLib.FormattedIO488 object that can communicate with the instrument on the GPIB0 bus at instrument address 10. Notice that instantiating the instrument is necessary before using the Open method from the IO property. Also notice the syntax used to specify the interface and bus address.

Page 12: Programming Machine Interfacing

Step 3: Talk to your instrument

The session object (VisaComLib.FormattedIO488) has several mechanisms for sending and receiving data from an instrument. Two of those mechanisms are WriteString and ReadString. The following example shows how to send the instrument identification query and display the instrument's response query in a message box.

Dim idn As String instrument.WriteString "*IDN?" idn = instrument.ReadString() MsgBox "The IDN String is: " & idn, vbOKOnly, "IDN Result"

Page 13: Programming Machine Interfacing

Handling Data with Instruments

This tutorial describes how to read data from an Agilent instrument or send data to an Agilent instrument. The most common type of data is a string representation of a number. However, this tutorial will show examples for both string and double representations of the data.

Note: Be sure that you connect to your instrument before testing your code. Instructions for connecting to an instrument can be found in the first three tutorials. All examples require Agilent VISA COM installed on your PC.

Page 14: Programming Machine Interfacing

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.

Page 15: Programming Machine Interfacing

Writing StringsIf 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 & "'"

Page 16: Programming Machine Interfacing

Reading NumbersConverting 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.

Page 17: Programming Machine Interfacing

Writing NumbersUse 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.

Page 18: Programming Machine Interfacing

Using a LAN connectionNewer Agilent instruments have LAN and USB ports in addition to a GPIB port.

Using a LAN connection does not require a GPIB card and cable and may give better data-throughput performance. However, it requires some LAN knowledge to set-up. There are several ways to connect to an instrument using LAN: treat the instrument as a LAN client, connect a GPIB instrument to a LAN to GPIB converter such as the Agilent E5810 LAN/GPIB Gateway, or use a crossover cable. Follow these instructions to set-up a LAN connection with the instrument.

Notes: Make sure you fully install the Agilent IO Libraries, version 14 or later, before trying to connect with LAN. You will know this is installed if there is an IO icon in your system tray (the letters “IO” with a multicolored circle around it). Clicking on this icon will bring up several useful tools such as Agilent Connection Expert and VISA Assistant. Click on the icon and select About Agilent IO Control to check what version you are running.

Page 19: Programming Machine Interfacing

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.

Page 20: Programming Machine Interfacing

You can also connect to your instrument by setting up the instrument with the Agilent IO Libraries interface. Click the IO icon in the system tray and select Agilent Connection Expert. On the top toolbar, click Add Instrument. Under Interface type, highlight LAN and click Ok.

You can find the IP address of the instrument connected to the network using one of two ways.

1. Click Find Instruments. Ensure that the boxes next to LAN and Lookup hostnames, 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.

Test your connection with the instrument by clicking Test Connection.

Page 21: Programming Machine Interfacing
Page 22: Programming Machine Interfacing

• The address of the instrument in this example is TCPIP0::141.121.206.3. If you are unsure of the address, refer to the Agilent Connection Expert window and find it’s VISA address. Connect to the instrument using that address string. For example:

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::141.121.206.3")

Page 23: Programming Machine Interfacing

Crossover LAN Cable:Setting up a direct connection to the instrument is similar to setting up a connection to the instrument over a network.

Use a crossover LAN cable to connect to an instrument without having to go through a server. Connect your instrument and your PC with a crossover cable. Wait 2 minutes for your computer to autoconfigure a new IP address.

Find the IP address of your instrument. This is different than the DHCP Ethernet address. DHCP must be turned off before you acquire the IP address of the instrument when using a crossover cable. Consult the instrument's user manual for further instructions. If you need to set the IP address of the instrument manually, type in an address with the same subnet as your computer. For example, if your computer's IP address is 169.254.33.40, set the instrument's IP address to something like 169.254.33.44.

You can test the connection in your DOS window. Type ping . Your computer will send out 4 request packets and should receive 4 replies from the instrument.

Click the IO icon in the system tray and select Agilent Connection Expert. On the top toolbar, click Add Instrument. Under Interface type, highlight LAN and click Ok.

You can find the IP address of the instrument connected to the network using one of two ways.

1. Click Find Instruments. Ensure that the boxes next to LAN and Lookup hostnames, 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.

Page 24: Programming Machine Interfacing

• 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 instrument’s 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.

Page 25: Programming Machine Interfacing
Page 26: Programming Machine Interfacing

As shown above, add an entry with the instrument's IP address above the local host entry similar to the following:

111.222.33.44 33220A

Save and close the file.

Page 27: Programming Machine Interfacing

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.

Page 28: Programming Machine Interfacing
Page 29: Programming Machine Interfacing

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.

Click Ok. Agilent Connection Expert should automatically update the Instrument I/O on this PC frame and list any instruments you currently have connected to the LAN/GPIB converter. The address of the instrument should be something like GPIB1::10::INSTR. Handle the instrument as if it were connected by a direct GPIB connection. If you are unsure of the address of the instrument, run Agilent Connection Expert for a list of all instruments connected to your computer and their address strings.

Page 30: Programming Machine Interfacing

Using a USB connection

In addition to LAN and GPIB ports, newer instruments are also equipped with USB ports. A USB connection is typically easy to setup and very inexpensive. A USB2.0 connection is also faster than a LAN or GPIB connection. However, USB does not work with Windows NT. You can connect your instrument using a standard USB cable or with a USB /GPIB converter. For instruments that do not offer a USB port, the Agilent 82537A USB/GPIB interface connects to the GPIB port of the instrument and to a USB port on the computer.

Page 31: Programming Machine Interfacing

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.

Page 32: Programming Machine Interfacing
Page 33: Programming Machine Interfacing

Click the IO icon in the system tray and select Agilent Connection Expert. The Agilent Connection Expert will automatically detect the instrument connected with a USB cable and list it under the Instrument I/O on this PC column. When connecting to your instrument in Visual Basic, use can use the VISA alias or the VISA address as the address of your instrument.

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("UsbDevice1") ' or Set instrument.IO =

ioMgr.Open("USB0::2391::1031::PROTO03::0::INSTR")

Page 34: Programming Machine Interfacing

USB/GPIB ConverterConnecting 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")

Page 35: Programming Machine Interfacing

Waits and delays - learn how to create a delay

Visual Basic and Visual Studio .NET do not include a Wait command. When working with instruments a Wait command can be particularly useful for adding a delay between a query and reply. The most common example is to add a delay after sending a reset command to an instrument. An instrument may buffer commands and any commands sent to an instrument that is resetting will likely be lost. There are several ways to introduce Wait functionality; sleep and delay are two common methods for creating a delay.

Page 36: Programming Machine Interfacing

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

Page 37: Programming Machine Interfacing

DelayFor 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

Page 38: Programming Machine Interfacing

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.

Page 39: Programming Machine Interfacing

CablingTo 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 "straight-through" 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.

Page 40: Programming Machine Interfacing

Check your instrument manual for the proper cable to use. If the manual is unavailable, try one of the two cables shown below. A typical instrument to PC cable pin diagram is shown on the right. The pin out on the left for a printer cable. This pin out will work with most Agilent and non-Agilent instruments. The diagram on the right is a simpler cable. This is the cable shipped with many Agilent instruments that support RS232.

Page 41: Programming Machine Interfacing
Page 42: Programming Machine Interfacing

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:

Page 43: Programming Machine Interfacing
Page 44: Programming Machine Interfacing

• 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

Page 45: Programming Machine Interfacing

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 = 10instrument.IO.TerminationCharacterEnabled = True

Page 46: Programming Machine Interfacing

Command DelaysFor 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.

Page 47: Programming Machine Interfacing

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.

Page 48: Programming Machine Interfacing

Synchronizing Instrument I/O with *OPC?, SRQ, and polling

If you have worked through the other section in this series, you have probably come to realize that, in addition to exchanging data, you need control when the exchange takes place. You may need to synchronize the instrument with your program execution or provide a means for the instrument to signal your program. This section describes three ways to synchronize your program with your instruments.

– The first method is to stop the executing of the Visual Basic code with a *OPC? Command. When the instrument has finished executing the commands that have been received it will become idle and respond to the *OPC? Command.

– The second method is to have the instrument interrupt the execution of the Visual Basic program with an SRQ. The GPIB bus includes a SRQ line, a hardware line that connects the instrument to the computer.

– The third method is to use visual basic to poll the instrument's SRQ status register. Polling the status register is useful when the instruments are connected via RS232, USB or LAN.

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.

Page 49: Programming Machine Interfacing

Getting started in Visual Studio .NET - Using VB.NET to control instruments

Microsoft has integrated robust support for COM components in the .NET environment. The COM interfaces translate well into .NET equivalents via automated wrapper-generator tools that Microsoft provides.

Page 50: Programming Machine Interfacing

Step 1: Open a new project

Start Microsoft Visual Studio .NET. Under the Projects tab of the Start Page, click New Project. In the Project Types window, highlight Visual Basic Projects. In the Templates window, highlight Windows Application. You may rename the project in the Name text field to whatever you like. Click Ok.

You should see a new Form appear. Along the left side of the window, there is a pop out menu labeled Toolbox. If the Toolbox menu is not there, click on View > Toolbox from the menu bar or press CTRL+ALT+X. Click the Toolbar menu and select Button. Draw a command button on the form.

Page 51: Programming Machine Interfacing

Step 2: Add the appropriate references

In the Solution Explorer window, right-click References and click Add Reference. Alternately, you may use the Project > Add Reference menu.

At the top of the Add Reference window, click the COM tab. Highlight VISA COM 3.0 Type Library. Click Select to select the VISA COM 3.0 libraries and click OK.

Page 52: Programming Machine Interfacing
Page 53: Programming Machine Interfacing

Step 3: Connect to the instrument• Double-click on the command button in Design window or press F7 to view the skeleton code or your project. Enter the

following code to connect to most IEEE 488.2 compliant instruments:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click Dim ioMgr As Ivi.Visa.Interop.ResourceManager Dim instrument As Ivi.Visa.Interop.FormattedIO488 Dim idn As String

ioMgr = New Ivi.Visa.Interop.ResourceManager instrument = New Ivi.Visa.Interop.FormattedIO488 ' use instrument specific address for Open() parameter – i.e. GPIB0::22 Set instrument.IO = ioMgr.Open("GPIB0::22") instrument.WriteString("*IDN?") idn = instrument.ReadString() MsgBox("The IDN String is: " & idn, vbOKOnly, "IDN Result")End Sub

To test your code, use the Debug > Start menu or press F5. Clicking the button should return the instrument address as a string in a message box.

For more information regarding available interfaces and classes, press CTRL+ALT+J or use the View > Other Windows > Object Browser menu.