The VISA I/O API & .NET
by David Gladfelter

Example 1:

(a)

<DllImportAttribute("VISA32.DLL", EntryPoint:="#141", ExactSpelling:=True, CharSet:=CharSet.Ansi, SetLastError:=True, CallingConvention:=CallingConvention.Winapi)> _
Public Shared Function viOpenDefaultRM(ByRef sesn As Integer) As Integer
End Function

(b)

Public Declare Function viOpenDefaultRM2 Lib "VISA32.DLL" Alias "#141" (ByRef sesn As Integer) As Integer


Listing One

#include <stdio.h>
#include <stdlib.h>
#include <visa.h>
#include <string.h>

void programExit(char *s)
{
    printf("%s\n",s);
    exit(-1);
}
int main(int argc, char *argv[])
{
    ViSession defaultRM;
    ViStatus status;
    ViSession sess;
    ViUInt32 retCount;
    ViString idnQuery = "*IDN?\n";
    char idnResponse[100];

    // Open Resource Manager that knows how to find, parse, and open resources
    status = viOpenDefaultRM(&defaultRM);
    if ( status != VI_SUCCESS ) { programExit("viOpenDefaultRM() failed");}
    // Open a session to the device, located on the network with hostname 
    // "arbGen.agilent.com" and having a device name of "inst0"
    status = viOpen(defaultRM,"TCPIP::arbGen.agilent.com::inst0::INSTR",
                                        VI_EXCLUSIVE_LOCK,VI_NULL,&sess);
    if ( status != VI_SUCCESS ) { programExit("viOpen() failed");}
    // Perform a clear to put the device I/O in a known state.
    status = viClear(sess);
    if ( status != VI_SUCCESS ) { programExit("viClear() failed");}
    // Ask the instrument to identify itself
    status = viWrite(sess,idnQuery,(ViUInt32)strlen(idnQuery),&retCount);
    if ( status != VI_SUCCESS ) { programExit("viWrite() failed");}
    if ( retCount != strlen(idnQuery)) { programExit("viWrite() failed\n"); }
    // read back the identification string
    status = viRead(sess,idnResponse,sizeof(idnResponse),&retCount);
    if ( status != VI_SUCCESS ) { programExit("viRead() failed\n");}
    // null-terminate the response so it can be printed
    idnResponse[retCount] = 0;
    printf("%s = %s\n","TCPIP::arbGen.agilent.com::inst0::INSTR",idnResponse);
    // Close the I/O resources  
    viClose(sess);
    viClose(defaultRM);
    return 0;
}



Listing Two

Module Module1
    Sub Main()
        ' Run sample program with an instrument address of "GPIB::12::INSTR"
        RunExample("GPIB0::12::INSTR")
    End Sub
    Private Sub RunExample(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, holds 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
End Module


Listing Three

#Region "viGetAttribute Overloads"
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#133", _
                        ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                        SetLastError:=True, _
                        CallingConvention:=CallingConvention.Winapi)> _
    Public Function viGetAttribute(ByVal vi As Integer, _
                                    ByVal attrName As Integer, _
                                    ByRef attrValue As Byte) As Integer
    End Function
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#133", _
                        ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                        SetLastError:=True, _
                        CallingConvention:=CallingConvention.Winapi)> _
    Public Function viGetAttribute(ByVal vi As Integer, _
                                    ByVal attrName As Integer, _
                                    ByRef attrValue As Short) As Integer
    End Function
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#133", _
                        ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                        SetLastError:=True, _
                        CallingConvention:=CallingConvention.Winapi)> _
    Public Function viGetAttribute(ByVal vi As Integer, _
                                    ByVal attrName As Integer, _
                                    ByRef attrValue As Integer) As Integer
    End Function
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#133", _
                    ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                    SetLastError:=True, _
                    CallingConvention:=CallingConvention.Winapi)> _
    Public Function viGetAttribute(ByVal vi As Integer, _
                    ByVal attrName As Integer, _
                    ByVal attrValue As System.Text.StringBuilder) As Integer
    End Function
#End Region

#Region "viSetAttribute Overloads"
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#134", _
                    ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                    SetLastError:=True, _
                    CallingConvention:=CallingConvention.Winapi)> _
    Public Function viSetAttribute(ByVal vi As Integer, _
                                    ByVal attrName As Integer, _
                                    ByVal attrValue As Byte) As Integer
    End Function
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#134", _
                    ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                    SetLastError:=True, _
                    CallingConvention:=CallingConvention.Winapi)> _
    Public Function viSetAttribute(ByVal vi As Integer, _
                                    ByVal attrName As Integer, _
                                    ByVal attrValue As Short) As Integer
    End Function
    <DllImportAttribute("VISA32.DLL", EntryPoint:="#134", _
                        ExactSpelling:=True, CharSet:=CharSet.Ansi, _
                        SetLastError:=True, _
                        CallingConvention:=CallingConvention.Winapi)> _
    Public Function viSetAttribute(ByVal vi As Integer, _
                                    ByVal attrName As Integer, _
                                    ByVal attrValue As Integer) As Integer
    End Function
#End Region




4


