Other topics about Controlling Peripherals
The following figure shows the flow of controlling the instrument with VISA. When developing a VISA program in the Visual Basic language, a special programing notice (in the readme text filie listed below) must be reviewed.
For details on the use of the VISA library and the programing notice for using the VISA library with the E5071C macro (E5071C VBA), refer to the following files contained in IO library CD-ROM.
visa.hlp (on-line help for the VISA library)
vbreadme.txt (notes on using the VISA library with VB)
To use the VISA library in the E5071C macro (E5071C VBA), you need to import two definition files into your project with the Visual Basic editor to define the VISA functions and perform other tasks. The definition files are stored on the sample programs disk under the following filenames (for information on importing modules, refer to Saving a Module (Exporting).
visa32.bas
Flow of instrument control with VISA
The VISA system startup session is processed in Line 90 in the sample program ctrl_ext.vba. VISA's viOpenDefaultRM function initializes and starts up the VISA system. The viOpenDefaultRM function must be executed before other VISA functions are called, and the parameter of this function is startup information (Defrm in ctrl_ext.vba).
viOpenDefaultRM(param)
Parameter |
(param) |
Description |
Startup information (output) |
Data type |
Long integer type |
The connection session is handled in Line 130. VISA's viOpen function makes connection with the specified instrument. The viOpen function returns a value so that the VISA functions can apply it to the specified instrument. The parameters of this function are startup information (Defrm), the address information of the specified instrument ("GPIB0::17::INSTR" in ctrl_ext.vba), access mode (0 in ctrl_ext.vba), timeout (0 in ctrl_ext.vba), and connection information (Equip in ctrl_ext.vba).
viOpen(param1, param2, param3, param4, param5)
Parameter |
(param1) |
Description |
Startup information (input) |
Data type |
Long integer type |
Parameter |
(param2) |
Description |
Address information of the specified instrument (input) |
Data type |
Character string type |
Syntax |
"GPIB0::gpib address::INSTR" "USB0::manufacturer ID::model code::serial number::0::INSTR" "TCPIP0::IP address::inst0::INSTR" |
Parameter |
(param3) |
Description |
Access mode (Enter 0) |
Parameter |
(param4) |
Description |
Timeout (Enter 0) |
Parameter |
(param5) |
Description |
Connection information (output) |
Data type |
Long integer type |
The communication session is conducted in Line 170. VISA's viVPrintf function sends a program message (GPIB command) to the specified instrument. The parameters of this function are connection information (Equip), the program message (*IDN?), and the variable to be formatted (0 in ctrl_ext.vba).
To input/output GPIB commands, the viVPrintf function and the viVScanf function are mainly used, but other VISA functions are also available. For more information, refer to visa.hlp (online help for the VISA library).
viVPrintf(param1, param2, param3)
Parameter |
(param1) |
Description |
Connection information (input) |
Data type |
Long integer type |
Parameter |
(param2) |
Description |
Program message (input) When sending a program message of the GPIB command, a message terminator is required at the end of the message (Chr$(10) in ctrl_ext.vba) |
Data type |
Character string type |
Parameter |
(param3) |
Description |
A variable to be formatted. If not applicable, enter 0. |
Data type |
Specified data type |
The receiving session is controlled in Line 210. VISA's viVScanf function receives the result from the specified instrument and stores it in the output variable. The parameters of this function are connection information (Equip in ctrl_ext.vba), the format parameter for the output variable (%t in ctrl_ext.vba), and the output variable (Prod in ctrl_ext.vba).
viVScanf(param1, param2, param3)
Parameter |
(param1) |
Description |
Connection information (input) |
Data type |
Long integer type |
Parameter |
(param2) |
Description |
Format parameter for the output variable |
Data type |
Character string type |
Parameter |
(param3) |
Description |
Output variable (output) |
Data type |
Character string type |
The disconnection session is handled in Line 280. VISA's viClose function disconnects communication and terminates the VISA system. The parameter of this function is startup information (Defrm in ctrl_ext.vba).
viClose(param)
Parameter |
(param) |
Description |
Startup information (input) |
Data type |
Long integer type |
The ctrl_ext.vba is a sample program to control instruments connected through USB/GPIB interface cable using the E5071C as the system controller. This VBA program consists of the following modules.
Object name |
Module type |
Content |
mdlVisa |
Standard module |
Reads out the product information of external instrument. |
VISA32.bas |
Standard module |
Definition files to use VISA library |
When you control peripherals from E5071C VBA, use the GPIB comĀmands provided for the instrument to communicate over VISA. On the other hand, when you control the E5071C itself from E5071C VBA, use the COM objects provided for the E5071C to communicate.
Initializes and starts up the VISA system and outputs the startup information to the Defrm variable. During this process, if an error occurs, the program goes to the error handling routine (Lines 320 to 360).
Establishes the connection to the external instrument (GPIB address: 17) connected via GPIB and outputs the connection information to the Equip variable. During this process, if an error occurs, the program goes to the error handling routine (Lines 320 to 360).
Queries the product information of the external instrument connected via USB/GPIB interface cable using VISA. During this process, if an error occurs, the program goes to the error handling routine (Lines 320 to 360).
Retrieves the product information through VISA and outputs it into the Prod variable. Displays the read-out result in the message box. During this process, if an error occurs, the program goes to the error handling routine (Lines 320 to 360).
Breaks the communication and terminates the VISA system.
If an error occurs in a VISA function, displays the detail of the error and terminates the program.
Read out the product information (ctrl_ext.vba)
10| Sub Main()
20|
30| Dim status As Long 'VISA function status return code
40| Dim Defrm As Long 'Session to Default Resource Manager
50| Dim Equip As Long 'Session to instrument
60| Dim Prod As String * 100 'String to receive the result
70|
80| ' Initializes the VISA system.
90| status = viOpenDefaultRM(Defrm)
100| If (status <> VI_SUCCESS) Then GoTo VisaErrorHandler
110|
120| ' Opens the session to the specified instrument.
130| status = viOpen(Defrm, "GPIB0::17::INSTR", 0, 0, Equip)
140| If (status <> VI_SUCCESS) Then GoTo VisaErrorHandler
150|
160| ' Asks for the instrument's product information.
170| status = viVPrintf(Equip, "*IDN?" & Chr$(10), 0)
180| If (status <> VI_SUCCESS) Then GoTo VisaErrorHandler
190|
200| ' Reads the result.
210| status = viVScanf(Equip, "%t", Prod)
220| If (status <> VI_SUCCESS) Then GoTo VisaErrorHandler
230|
240| ' Displays the result.
250| MsgBox Prod
260|
270| ' Closes the resource manager session (which closes everything)
280| Call viClose(Defrm)
290|
300| GoTo Prog_end
310|
320| VisaErrorHandler:
330| Dim VisaErr As String * 200
340| Call viStatusDesc(Defrm, status, VisaErr)
350| MsgBox "Error : " & VisaErr, vbExclamation
360| Exit Sub
370|
380| Prog_end:
390|
400| End Sub