'' """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" '' Copyright (c) 1999- 2003 Agilent Technologies Inc. All rights reserved. '' '' You have a royalty-free right to use, modify, reproduce and distribute '' the Sample Application Files (and/or any modified version) in any way '' you find useful, provided that you agree that Agilent Technologies has '' no warranty, obligations or liability for any Sample Application Files. '' '' Agilent Technologies provides programming examples for illustration only, '' This sample program assumes that you are familiar with the programming '' language being demonstrated and the tools used to create and debug '' procedures. Agilent Technologies support engineers can help explain the '' functionality of Agilent Technologies software components and associated '' commands, but they will not modify these samples to provide added '' functionality or construct procedures to meet your specific needs. '' """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" '' To develop VISA applications in Microsoft Visual Basic, you first need '' to add the Visual Basic (VB) declaration file in your VB project as a '' Module. This file contains the VISA function definitions and constant '' declarations needed to make VISA calls from Visual Basic. '' To add this module to your project in VB 6, from the menu, select '' Project->Add Module, select the 'Existing' tab, and browse to the '' directory containing the VB Declaration file, select visa32.bas, and '' press 'Open'. '' '' The name and location of the VB declaration file depends on which '' operating system you are using. Assuming the 'standard' VISA directory '' of C:\Program Files\VISA or the 'standard' VXIpnp directory of '' C:\VXIpnp, the visa32.bas file can be located in one of the following: '' '' \winnt\agvisa\include\visa32.bas - Windows NT/2000/XP '' \winnt\include\visa32.bas - Windows NT/2000/XP '' \win95\include\visa32.bas - Windows 95/98/Me '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' bintrace.bas ' The following example program is written for the PSA and ESA Series ' Spectrum Analyzers. It queries the IDN string from the instrument ' and then reads the trace data in Spectrum Analysis mode in binary ' format (Real,32 or Real,64 or Int,32). The data is then stored to a ' file "bintrace.txt". ' Binary transfers are faster than the default ASCII transfer mode, ' because less data is sent over the bus. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Option Explicit Private Sub Main() ' Declare Variables used in the program Dim status As Long 'VISA function status return code Dim defrm As Long 'Session to Default Resource Manager Dim vi As Long 'Session to instrument Dim strRes As String * 100 'Fixed length string to hold *IDN? Results Dim x As Integer 'Loop Variable Dim output As String 'output string variable Dim ArrayPtr(1) As Long 'Array of Pointers Dim ResultsArray(8192) As Single 'trace element array of Real,32 values 'For Real,64 data use Double. For Int,32 data use Long Dim length As Long 'Number of trace elements return from instrument Dim fnum As Integer 'File Number to used to open file to store data Dim isOpen As Boolean 'Boolean flag used to keep track of open file 'Set the default number of trace elements to the ResultsArray size 'Note: PSA and ESA currently support up to 8192 trace points length = 8192 'Set the array of pointers to the addresses of the variables ArrayPtr(0) = VarPtr(length) ArrayPtr(1) = VarPtr(ResultsArray(0)) On Error GoTo Error_Handler ' Open the default resource manager session status = viOpenDefaultRM(defrm) ' Open the session. Note: For PSA, to use LAN, change the string to ' "TCPIP0::xxx.xxx.xxx.xxx::inst0::INTSR" where xxxxx is the IP address status = viOpen(defrm, "GPIB0::18::INSTR", 0, 0, vi) If (status < 0) Then GoTo VisaErrorHandler ' Set the I/O timeout to five seconds status = viSetAttribute(vi, VI_ATTR_TMO_VALUE, 5000) If (status < 0) Then GoTo VisaErrorHandler 'Ask for the devices's *IDN string. status = viVPrintf(vi, "*IDN?" + Chr$(10), 0) If (status < 0) Then GoTo VisaErrorHandler 'Read back the IDN string from the instrument status = viVScanf(vi, "%t", strRes) If (status < 0) Then GoTo VisaErrorHandler 'Print the IDN string results in a message box MsgBox (strRes) 'Change the instrument mode to Spectrum Analysis status = viVPrintf(vi, ":INST:NSEL 1" + Chr$(10), 0) If (status < 0) Then GoTo VisaErrorHandler ' Set instrument trace data format to 32-bit Real ' Note: For higher precision use 64-bit data, ":FORM REAL,64" ' For faster data transfer for ESA, use ":FORM INT,32" status = viVPrintf(vi, ":FORM REAL,32" + Chr$(10), 0) If (status < 0) Then GoTo VisaErrorHandler 'Set Analyzer to single sweep mode status = viVPrintf(vi, ":INIT:CONT 0" + Chr$(10), 0) If (status < 0) Then GoTo VisaErrorHandler 'Trigger a sweep and wait for sweep to complete status = viVPrintf(vi, ":INIT:IMM;*WAI" + Chr$(10), 0) If (status < 0) Then GoTo VisaErrorHandler 'Query the trace data from the instrument 'Note: Change the "%#zb" to "%#Zb" for Real,64 data ' For Int,32 leave the modifier as "%#zb" status = viVQueryf(vi, ":TRAC:DATA? TRACE1" + Chr$(10), _ "%#zb", ArrayPtr(0)) 'Close the vi session and the resource manager session Call viClose(vi) Call viClose(defrm) 'Print number of elements returned MsgBox ("Number of trace elements returned = " & length) 'Create a string from the ResultsArray to output to a file For x = 0 To length - 1 output = output & ResultsArray(x) & vbCrLf Next x 'Print Results to the Screen MsgBox (output) 'Store the results in a text file fnum = FreeFile() 'Get the next free file number Open "bintrace.txt" For Output As #fnum isOpen = True Print #fnum, output ' Intentionally flow into Error Handler to close file Error_Handler: ' Raise the error (if any), but first close the file If isOpen Then Close #fnum If Err Then Err.Raise Err.Number, , Err.Description Exit Sub VisaErrorHandler: Dim strVisaErr As String * 200 Call viStatusDesc(defrm, status, strVisaErr) MsgBox "*** Error : " & strVisaErr, vbExclamation, "VISA Error Message" Exit Sub End Sub