Instrument Control (GPIB, Serial, VISA, IVI)

cancel
Showing results for 
Search instead for 
Did you mean: 

VBA USB control example

You will have to excuse me but I really new at this,

 

With this example uses GPIB

 

I want to do the same thing with a USB interface instrument:

 

 

How would the below program change?

 

Public RM As VisaComLib.ResourceManager 'Resource manager
Public PS As VisaComLib.FormattedIO488 'IO Object

Private Sub CommandButton1_Click()
Set RM = New VisaComLib.ResourceManager 'Resource Manager Object created
Set PS = New VisaComLib.FormattedIO488 'FormattedIO488 Object created
Set PS.IO = RM.Open("GPIB0::12::INSTR") 'IO Object created (enter VISA address of ENA)

PS.IO.Timeout = 10000 'Timeout time in msec

PS.WriteString ("*IDN?")
Sheet1.Cells(6, 2) = PS.ReadString

PS.IO.Close
Set PS = Nothing
Set RM = Nothing

End Sub

 

 

0 Kudos
Message 1 of 2
(1,189 Views)

You can also use it by simply changing the address field of your code.
If you want to do something advanced, please do as below.

 

Sub communication_test1()
    Dim RM As VisaComLib.ResourceManager
    Dim VISACOMOBJ As VisaComLib.FormattedIO488
    Dim addr As String, list() As String
    Dim timeout As Long: timeout = 5000 '5000msec

    addr = "GPIB0::15::INSTR" 'GPIB Resource Name
    'OR
    addr = "USB0::0x****::0x****::********::INSTR" 'USBTMC Resource Name

    Set RM = New VisaComLib.ResourceManager
    Set VISACOMOBJ = New VisaComLib.FormattedIO488
    Set VISACOMOBJ.IO = RM.Open(addr, NO_LOCK, timeout)
    With VISACOMOBJ
        .IO.timeout = timeout
        .WriteString "*IDN?" & vbLf, True
        list = .ReadList(ASCIIType_BSTR, ",")
        .IO.Close
    End With

    MsgBox Join(list, vbLf)

End Sub

Sub communication_test2()
    Dim RM As VisaComLib.ResourceManager
    Dim VISACOMOBJ As VisaComLib.FormattedIO488
    Dim igpibDev As VisaComLib.igpib 'for GPIB
    Dim iusbDev As VisaComLib.IUsb ' for USB
    Dim ilanvxi11Dev As VisaComLib.ITcpipInstr 'for VXI-11(LAN)
    Dim addr As String, list() As String
    Dim timeout As Long: timeout = 5000 '5000msec
    
    addr = "GPIB0::15::INSTR" 'GPIB Resource Name
    'OR
    addr = "USB0::0x****::0x****::********::INSTR" 'USBTMC Resource Name
    'OR
    addr = "TCPIP0::*.*.*.*::inst0::INSTR" 'VXI-11 Resource Name

    Set RM = New VisaComLib.ResourceManager
    Set VISACOMOBJ = New VisaComLib.FormattedIO488
    Set VISACOMOBJ.IO = RM.Open(addr, NO_LOCK, timeout)

    Select Case VISACOMOBJ.IO.HardwareInterfaceType
        Case VisaComLib.HardwareInterfaceType.INTF_GPIB
            Set igpibDev = VISACOMOBJ.IO
            With igpibDev
                .timeout = timeout
                .ControlREN GPIB_REN_ADDRESS_AND_LLO
                MsgBox "The measuring instrument has been locked out." & vbLf & "The instrument should be in a state where it does not accept any button operations other than the power supply." & vbLf & "Try pressing any buttons."
                VISACOMOBJ.WriteString "*IDN?" & vbLf, True
                list = VISACOMOBJ.ReadList(ASCIIType_BSTR, ",")
                .ControlREN GPIB_REN_GTL_AND_DEASSERT
                MsgBox "The lockout has been released."
            End With
        Case VisaComLib.HardwareInterfaceType.INTF_USB
            Set iusbDev = VISACOMOBJ.IO
            With iusbDev
                .timeout = timeout
                .ControlREN GPIB_REN_ADDRESS_AND_LLO
                MsgBox "The measuring instrument has been locked out." & vbLf & "The instrument should be in a state where it does not accept any button operations other than the power supply." & vbLf & "Try pressing any buttons."
                VISACOMOBJ.WriteString "*IDN?" & vbLf, True
                list = VISACOMOBJ.ReadList(ASCIIType_BSTR, ",")
                .ControlREN GPIB_REN_GTL_AND_DEASSERT
                MsgBox "The lockout has been released."
            End With
        Case VisaComLib.HardwareInterfaceType.INTF_TCPIP
            'Note that INTF_TCPIP is not always VXI-11, but sometimes HiSLIP or Socket (identified by address)
            Set ilanvxi11Dev = VISACOMOBJ.IO
            ilanvxi11Dev.timeout = timeout
            VISACOMOBJ.WriteString "*IDN?" & vbLf, True
            list = VISACOMOBJ.ReadList(ASCIIType_BSTR, ",")
    End Select

    VISACOMOBJ.IO.Close
    MsgBox Join(list, vbLf)
End Sub
0 Kudos
Message 2 of 2
(1,142 Views)