VB .NET
Here is a complete program sample using VISA in VB .NET and in C#. The sample program is written for the Agilent PSA and ESA series spectrum analyzers. It stores the current screen image on the instrument's flash as C:PICTURE.GIF. It then transfers the image over GPIB or LAN and stores the image on your PC in the current directory as picture.gif. The file C:PICTURE.GIF is then deleted from the instrument's flash.
This sample demonstrates reading arrays, error handling, and basic session tasks.
VB .NET
Private Sub RunTutorial(ByVal instrAddress As String) ' Declare Variables used in the program Dim status As Integer 'VISA function status return code Dim defrm As Integer = 0 'Session to Default Resource Manager Dim vi As Integer = 0 'Session to instrument Dim x As Integer 'Loop Variable Dim ResultsArray(50000) As Byte 'results array, big enough to hold a GIF Dim length As Integer 'Number of bytes returned from instrument Dim headerlength As Integer 'length of header ' the file to write the picture Dim fs As System.IO.FileStream = Nothing 'Set the default number of bytes that will be contained in the 'ResultsArray to 50,000 (50kB) length = 50000 Try If System.IO.File.Exists("picture.gif") Then System.IO.File.Delete("picture.gif") End If ' Open the default resource manager session status = visa32.viOpenDefaultRM(defrm) ' Open the session. For GPIB, the address string looks like: ' GPIB0::18::INSTR ' For PSA, to use LAN, change the string to ' "TCPIP0::xxx.xxx.xxx.xxx::inst0::INSTR" where ' xxxxx is the IP address status = visa32.viOpen(defrm, "instrAddress", 0, 0, vi) CheckStatus(defrm, status) ' Set the I/O timeout to fifteen seconds status = visa32.viSetAttribute(vi, visa32.VI_ATTR_TMO_VALUE, 15000) CheckStatus(vi, status) 'Store the current screen image on flash as C:PICTURE.GIF status = visa32.viPrintf(vi, ":MMEM:STOR:SCR 'C:PICTURE.GIF'" & vbLf) CheckStatus(vi, status) 'Grab the screen image file from the instrument status = visa32.viPrintf(vi, ":MMEM:DATA? 'C:PICTURE.GIF'" & vbLf) CheckStatus(vi, status) ' We're reading this as raw binary, although it is a IEEE 488.2 ' binary block containing byte data. We could've used ' "%#b" format string, and the byte array would not contain the ' IEEE binary block header. status = visa32.viScanf(vi, "%#y", length, ResultsArray) CheckStatus(vi, status) 'Delete the tempory file on the flash named C:PICTURE.GIF status = visa32.viPrintf(vi, ":MMEM:DEL 'C:PICTURE.GIF'" & vbLf) CheckStatus(vi, status) 'Close the vi session and the resource manager session Call visa32.viClose(vi) vi = 0 Call visa32.viClose(defrm) defrm = 0 'Store the results in a text file fs = _ New System.IO.FileStream("picture.gif", _ IO.FileMode.OpenOrCreate) Dim zeroVal() As Char = {"0"} Dim zeroValByte() As Byte zeroValByte = System.Text.Encoding.ASCII.GetBytes(zeroVal) headerlength = ResultsArray(1) - zeroValByte(0) + 2 fs.Write(ResultsArray, headerlength, length - 2 - headerlength) Catch err As System.ApplicationException MsgBox("*** Error : " & err.Message, vbExclamation, _ "VISA Error Message") Exit Sub Catch err As System.SystemException MsgBox("*** Error : " & err.Message, vbExclamation, _ "System Error Message") Exit Sub Catch err As System.Exception Debug.Fail("Unexpected Error") MsgBox("*** Error : " & err.Message, vbExclamation, _ "Unexpected Error") Exit Sub Finally If Not fs Is Nothing Then fs.Close() If vi <> 0 Then Call visa32.viClose(vi) End If If defrm <> 0 Then Call visa32.viClose(defrm) End If End Try End Sub Private Sub CheckStatus(ByVal vi As Integer, ByVal status As Integer) If (status < visa32.VI_SUCCESS) Then Dim err As System.Text.StringBuilder = New System.Text.StringBuilder(256) visa32.viStatusDesc(vi, status, err) Throw New ApplicationException(err.ToString()) End If End Sub
C#
private void RunTutorial(string instrAddress) { // Declare Variables used in the program int status; // VISA function status return code int defrm = 0; // Session to Default Resource Manager int vi = 0; // Session to instrument // results array, big enough to hold a GIF byte[] ResultsArray = new byte[50000]; int length; // Number of bytes returned from instrument int headerlength; // length of header System.IO.FileStream fs = null; // the file to write the picture // Set the default number of bytes that will be contained in the // ResultsArray to 50,000 (50kB) length = 50000; try { if (System.IO.File.Exists("picture.gif")) System.IO.File.Delete("picture.gif"); // Open the default resource manager session status = visa32.viOpenDefaultRM(out defrm); // Open the session. For GPIB, the address string looks like: // GPIB0::18::INSTR // For PSA, to use LAN, change the string to // "TCPIP0::xxx.xxx.xxx.xxx::inst0::INSTR" where // xxxxx is the IP address status = visa32.viOpen(defrm, "instrAddress", 0, 0, out vi); CheckStatus(defrm, status); // Set the I/O timeout to fifteen seconds status = visa32.viSetAttribute(vi, visa32.VI_ATTR_TMO_VALUE, 15000); CheckStatus(vi, status); //Store the current screen image on flash as C:PICTURE.GIF status = visa32.viPrintf(vi, ":MMEM:STOR:SCR 'C:PICTURE.GIF'\n"); CheckStatus(vi, status); //Grab the screen image file from the instrument status = visa32.viPrintf(vi, ":MMEM:DATA? 'C:PICTURE.GIF'\n"); CheckStatus(vi, status); // We're reading this as raw binary, although it is a IEEE 488.2 // binary block containing byte data. We could've used // "%#b" format string, and the byte array would not contain the // IEEE binary block header. status = visa32.viScanf(vi, "%#y", ref length, ResultsArray); CheckStatus(vi, status); //Delete the tempory file on the flash named C:PICTURE.GIF status = visa32.viPrintf(vi, ":MMEM:DEL 'C:PICTURE.GIF'\n"); CheckStatus(vi, status); //Close the vi session and the resource manager session visa32.viClose(vi); vi = 0; visa32.viClose(defrm); defrm = 0; //Store the results in a text file fs = new System.IO.FileStream("picture.gif", System.IO.FileMode.OpenOrCreate); char[] zeroVal = {'0'}; byte[] zeroValByte; zeroValByte = System.Text.Encoding.ASCII.GetBytes(zeroVal); headerlength = ResultsArray[1] - zeroValByte[0] + 2; fs.Write(ResultsArray, headerlength, length - 2 - headerlength); } catch(System.ApplicationException err) { System.Windows.Forms.MessageBox.Show("*** Error : " + err.Message, "VISA Error Message", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } catch(System.SystemException err) { System.Windows.Forms.MessageBox.Show("*** Error : " + err.Message, "System Error Message", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } catch(System.Exception err) { System.Diagnostics.Debug.Fail("Unexpected Error"); System.Windows.Forms.MessageBox.Show("*** Error : " + err.Message, "Unexpected Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Exclamation); } finally { if ( fs != null ) fs.Close(); if (vi != 0) visa32.viClose(vi); if (defrm != 0) visa32.viClose(defrm); } } private void CheckStatus(int vi, int status) { if (status < visa32.VI_SUCCESS) { System.Text.StringBuilder err = new System.Text.StringBuilder(256); visa32.viStatusDesc(vi, status, err); throw new ApplicationException(err.ToString()); } }
Next: Deploying Your Project