⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module1.vb

📁 进行数据的通信
💻 VB
字号:
'  This sample application is comprised of three basic parts:
'
'  1. Initialization
'  2. Main Body
'  3. Cleanup
'
'  The Initialization portion consists of initializing the bus and the
'  GPIB interface board so that the GPIB board is Controller-In-Charge
'  (CIC). Next it finds all the listeners and then clears all the
'  devices on the bus.
'
'  In the Main Body, this application queries a device for its
'  identification code by issuing the '*IDN?' command. Many
'  instruments respond to this command with an identification string.
'  Note, 488.2 compliant devices are required to respond to this
'  command.
'
'  The last step, Cleanup, takes the board offline.

Imports NationalInstruments.NI4882  'must be included to reference the LangInt assembly
Imports VB = Microsoft.VisualBasic  'included for string manipulation
Module Module1
    Const BDINDEX = 0                   ' Board Index
    Dim li As LangInt                   'declare li of type LangInt that has all GPIB functions
    Dim c As GpibConstants              'declare c of type GpibConstants that has all GPIB constants

    Sub Main()
        Dim k As Integer                'loop counter
        Dim num_listeners As Integer    'number of listeners on GPIB bus
        Dim DisplayStr As String
        Dim ReadBuffer As String         'contains the string returned from instrument
        Dim ARRAYSIZE = 100             ' size of ReadBuffer
        Dim result(30) As Short
        Dim instruments(31) As Short    ' array of primary addresses
        li = New LangInt()                  'declare an instance of li
        c = New GpibConstants()             'declare an instance of c

        '  ====================================================================
        '
        '  INITIALIZATION SECTION
        '
        ' ====================================================================

        '  Your board needs to be the Controller-In-Charge in order to find all
        '  listeners on the GPIB.  To accomplish this, the subroutine SendIFC
        '  is called.  If the error bit EERR is set in ibsta, call GPIBCleanup with
        '  an error message.

        li.SendIFC(BDINDEX)
        If (li.ibsta And c.ERR) Then Call GPIBCleanup("Error sending IFC.")

        '  Create an array containing all valid GPIB primary addresses, except
        '  for the primary address 0. Your GPIB interface board is at address 0
        '  by default.  This array (instruments) will be given to the subroutine
        '  FindLstn to find all listeners.  The constant NOADDR signifies the end
        '  of the array.

        For k = 0 To 29
            instruments(k) = k + 1
        Next k
        instruments(30) = c.NOADDR

        '  Print message to tell user that the program is searching for all active
        '  listeners.  Find all of the listeners on the bus.  Store the listen
        '  addresses in the array result.  If the error bit ERR is set in ibsta,
        '  call GPIBCleanup with an error message.

        Console.WriteLine("Finding all listeners on the bus...")
        li.FindLstn(BDINDEX, instruments, result, 31)
        If (li.ibsta And c.ERR) Then Call GPIBCleanup("Error finding all listeners.")

        '  ibcntl contains the actual number of addresses stored in the result
        '  array. Assign the value of ibcntl to the variable num_listeners.
        '  Print the number of listeners found.

        num_listeners = li.ibcntl

        DisplayStr = "Number of instruments found = " + Str$(num_listeners)
        Console.WriteLine(DisplayStr)

        '  Print a blank line.

        Console.WriteLine("")

        '  The result array contains the addresses of all listening devices
        '  found by FindLstn. Use the constant NOADDR
        '  to signify the end of the array.

        result(num_listeners) = c.NOADDR

        '  DevClearList will send the GPIB Selected Device Clear (SDC) command
        '  message to all the devices on the bus. If the error bit EERR is set in
        '  ibsta, call GPIBCleanup with an error message.

        li.DevClearList(BDINDEX, result)
        If (li.ibsta And c.ERR) Then Call GPIBCleanup("Error in clearing the devices. ")


        '  ====================================================================
        '
        '  MAIN BODY SECTION
        '
        '  In this application, the Main Body communicates with the instruments
        '  by writing a command to them and reading each response. This would be
        '  the right place to put other instrument communication.
        '
        ' ====================================================================

        '  Send the identification query to each listen address in the result
        '  array using SendList.  The constant NLend
        '  instructs the function SendList to append a linefeed character with
        '  EOI asserted to the end of the message.  If the error bit EERR is
        '  set in ibsta, call GPIBCleanup with an error message.

        li.SendList(BDINDEX, result, "*IDN?", 5, c.NLend)
        If (li.ibsta And c.ERR) Then Call GPIBCleanup("Error sending '*IDN?'. ")

        '  Read each device's identification code, one at a time.
        '
        '  Establish a FOR loop to read each one of the devices identification
        '  codes. The variable LOOP will serve as a counter for the FOR loop
        '  and as the index to the array RESULT.

        For k = 0 To (num_listeners - 1)

            '  Read the name identification response returned from each
            '  device. Store the response in the string ReadBuffer.  The
            '  constant STOPend instructs the
            '  function Receive to terminate the read when END is detected.
            '  If the error bit ERR is set in ibsta, call GPIBCleanup with an
            '  error message.

            'ReadBuffer = Space$(&H32)
            li.Receive(BDINDEX, result(k), ReadBuffer, ARRAYSIZE, c.STOPend)
            If (li.ibsta And c.ERR) Then Call GPIBCleanup("Error in receiving response to '*IDN?'.")


            '  The device returns a Line Feed character with the
            '  identification string. You could use the LEFT() function
            '  which returns a specified number of characters from the left
            '  side of a string to remove the Line Feed character. The code
            '  fragment below illustrates how to use the LEFT() function
            '  along with the GPIB global count variable, IBCNTL, to display
            '  the contents of ReadBuffer.
            '  Note, that you need one less character than the total number
            '  contained in IBCNTL.

            DisplayStr = Left$(ReadBuffer, li.ibcntl - 1)

            '   Display the list of readings.

            Console.WriteLine(DisplayStr)

        Next k         '  End of FOR loop

        ' ========================================================================
        '
        ' CLEANUP SECTION
        '
        ' ========================================================================

        '   Take the board offline

        li.ilonl(BDINDEX, 0)
        If (li.ibsta And c.ERR) Then Call GPIBCleanup("Error putting board offline.")


    End Sub
    Private Sub GPIBCleanup(ByRef msg As String)
        Dim ErrorMnemonic() As String = {"EDVR", "ECIC", "ENOL", "EADR", "EARG", "ESAC", "EABO", "ENEB", "EDMA", "", "EOIP", "ECAP", "EFSO", "", "EBUS", "ESTB", "ESRQ", "", "", "", "ETAB"}

        ' After each GPIB call, the application checks whether the call
        ' succeeded. If an NI-488.2 call fails, the GPIB driver sets the
        ' corresponding bit in the global status variable. If the call
        ' failed, this procedure prints an error message, takes the device
        ' offline and exits.
        MsgBox(msg & Chr(13) & "ibsta: 0x" & Hex(li.ibsta) & Chr(13) & "ERR: " & li.iberr & " <" & ErrorMnemonic(li.iberr) & ">", MsgBoxStyle.Critical, "Error")
        li.ibonl(BDINDEX, 0)                      'close BDINDEX
        End                                 'end program
    End Sub

End Module

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -