Visual Basic 6.0 Programming Example

Enter the Visual Basic 6.0 programming environment and follow the steps below:

1. Build a standard application program project (Standard EXE).

2. Open Project→Add File… and add the visa32.bas which contains all VISA functions ans constant statements to the project.

Then add the Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) statement into the visa32.bas, or create a new module to declare the Sleep function.

3. Add the Text, Edit and Button controls as shown in the figure below.

4. Encapsulate the write and read operations of VISA.

1) Encapsulate the write operation of VISA to facilitate the operation.

'-----------------------------------------------------------

'Function Name：InstrWrite

'Function： Send command to the instrument

'Input： rsrcName,instrument(resource) name

strCmd,Command

'-----------------------------------------------------------

Public Sub InstrWrite(rsrcName As String, strCmd As String)

Dim status As Long

Dim dfltRM As Long

Dim sesn As Long

Dim rSize As Long

'Initialize the system

status = viOpenDefaultRM(dfltRM)

'Failed to initialize the system

If (status < VI_SUCCESS) Then

MsgBox " No VISA resource was opened！"

Exit Sub

End If

‘Open the VISA instrument

status = viOpen(dfltRM, rsrcName, VI_NULL, VI_NULL, sesn)

'Failed to open the instrument

If (status < VI_SUCCESS) Then

MsgBox "Failed to open the instrument！"

Exit Sub

End If

'Write command to the instrument

status = viWrite(sesn, strCmd, Len(strCmd), rSize)

'Failed to write to the instrument

If (status < VI_SUCCESS) Then

MsgBox " Faild to write to the instrument！"

Exit Sub

End If

'Close the system

status = viClose(sesn)

status = viClose(dfltRM)

End Sub

2) Encapsulate the read operation of VISA to facilitate the operation.

'-----------------------------------------------------------

'Function Name：InstrRead

'Function： Read the return value from the instrument

'Input： rsrcName,Resource name

'Return：The string gotten from the instrument

'-----------------------------------------------------------

Public Function InstrRead(rsrcName As String) As String

Dim status As Long

Dim dfltRM As Long

Dim sesn As Long

Dim strTemp0 As String * 256

Dim strTemp1 As String

Dim rSize As Long

'Begin by initializing the system

status = viOpenDefaultRM(dfltRM)

'Initial failed

If (status < VI_SUCCESS) Then

MsgBox " Failed to open the instrument! "

Exit Function

End If

'Open the instrument

status = viOpen(dfltRM, rsrcName, VI_NULL, VI_NULL, sesn)

'Open instrument failed

If (status < VI_SUCCESS) Then

MsgBox " Failed to open the instrument! "

Exit Function

End If

' Read from the instrument

stasus = viRead(sesn, strTemp0, 256, rSize)

' Read failed

If (status < VI_SUCCESS) Then

MsgBox " Failed to read from the instrument! "

Exit Function

End If

'Close the system

status = viClose(sesn)

status = viClose(dfltRM)

' Remove the space at the end of the string

strTemp1 = Left(strTemp0, rSize)

InstrRead = strTemp1

End Function

5. Add the control event codes.

1) Connect the instrument

' Connect to the instrument

Private Sub CmdConnect_Click()

Const MAX_CNT = 200

Dim status As Long

Dim dfltRM As Long

Dim sesn As Long

Dim fList As Long

Dim buffer As String * MAX_CNT, Desc As String * 256

Dim nList As Long, retCount As Long

Dim rsrcName(19) As String * VI_FIND_BUFLEN, instrDesc As String * VI_FIND_BUFLEN

Dim i, j As Long

Dim strRet As String

Dim bFindDG As Boolean

‘Initialize the system

status = viOpenDefaultRM(dfltRM)

' Initialize failed

If (status < VI_SUCCESS) Then

MsgBox " No VISA resource was opened ！"

Exit Sub

End If

' Find instrument resource

Call viFindRsrc(dfltRM, "USB?*INSTR", fList, nList, rsrcName(0))

' Get the list of the instrument(resource)

strRet = ""

bFindDG = False

For i = 0 To nList - 1

' Get the instrument name

InstrWrite rsrcName(i), "*IDN?"

Sleep 200

strRet = InstrRead(rsrcName(i))

' Continue to switch the resource until find a DSA instrument

strRet = UCase(strRet)

j = InStr(strRet, "DG")

If (j >= 0) Then

bFindDG = True

Exit For

End If

Call viFindNext(fList + i - 1, rsrcName(i))

Next i

'Dispaly

If (bFindDG = True) Then

TxtInsAddr.Text = rsrcName(i)

Else

TxtInsAddr.Text = ""

End If

End Sub

2) Write operation

'Write the command to the instrument

Private Sub CmdWrite_Click()

If (TxtInsAddr.Text = "") Then

MsgBox ("Please write the instrument address！")

End If

InstrWrite TxtInsAddr.Text, TxtCommand.Text

End Sub

3) Read operation

'Read the return value from the instrument

Private Sub CmdRead_Click()

Dim strTemp As String

strTemp = InstrRead(TxtInsAddr.Text)

TxtReturn.Text = strTemp

End Sub

6. Execution result

1) Click "Connect" to search the signal generator;

2) Input "*IDN?" in the "Command" edit box;

3) Click "Write" to write the command into the signal generator;

4) Click "Read" to read the return value.

The execution result is as shown in the figure below: