Main Content

visadev

Create connection to device using VISA

Since R2021a

    Description

    A visadev object represents a connection to a device or instrument using the VISA interface. The following interface types are supported: TCP/IP (using VXI11 and HiSLIP), TCP/IP Socket, USB, GPIB, Serial, VXI, and PXI. Identify devices available to connect to using visadevlist. Then, connect to the device or instrument using visadev.

    Creation

    Description

    example

    v = visadev(resourceName) creates a connection to a device using its VISA resource name. Establish a connection using an installed VISA driver. If you have multiple VISA drivers installed, MATLAB® uses the preferred VISA set in your VISA vendor's configuration utility software.

    example

    v = visadev(resourceAlias) creates a connection to a device using its VISA alias, if it has one. If the configuration utility does not yet recognize the device, you cannot connect using the alias and must use the resource name.

    Input Arguments

    expand all

    VISA resource name, specified as a character vector or string scalar. You can identify the name of the resource you want to connect to using the information returned by visadevlist. This input sets the ResourceName property.

    Example: gpibdev = visadev("GPIB0::5::INSTR") connects to the GPIB device specified by the VISA resource name GPIB0::5::INSTR.

    Data Types: char | string

    VISA alias associated with a resource, specified as a character vector or string scalar. Identify the alias of the resource you want to connect to using the information returned by visadevlist. You can use an alias only if an alias is assigned using the VISA vendor’s configuration utility software. This input sets the Alias property.

    Example: serialdev = visadev("COM4") connects to the serial device specified by the VISA resource alias COM4.

    Data Types: char | string

    Properties

    See visadev Properties for a full list of properties.

    Object Functions

    readRead data from VISA resource
    readlineRead line of ASCII string data from VISA resource
    readbinblockRead one binblock of data from VISA resource
    writeWrite data to VISA resource
    writelineWrite line of ASCII data to VISA resource
    writebinblockWrite one binblock of data to VISA resource
    writereadWrite command to VISA resource and read response
    configureTerminatorSet terminator for ASCII string communication with VISA resource
    flushClear buffers for communication with VISA resource
    visastatusCheck status of VISA resource
    visatriggerSend trigger to GPIB or VXI instruments
    setDTRSet serial DTR pin
    setRTSSet serial RTS pin
    getpinstatusGet serial pin status

    Examples

    collapse all

    Search for and establish a connection to your VISA resource.

    Search for available VISA resources.

    resourceList = visadevlist
    resourceList =
    
      6×6 table
    
                            ResourceName                         Alias                  Vendor               Model       SerialNumber     Type 
             __________________________________________    _________________    ______________________    ___________    ____________    ______
    
        1    "USB0::0x0699::0x036A::CU010105::0::INSTR"    "NI_SCOPE_4CH"       "TEKTRONIX"               "TDS 2024B"    "CU010105"      usb   
        2    "TCPIP0::169.254.2.20::inst0::INSTR"          "Keysight_33210A"    "Agilent Technologies"    "33210A"       "MY57003523"    tcpip 
        3    "ASRL1::INSTR"                                "COM1"               ""                        ""             ""              serial
        4    "ASRL3::INSTR"                                "COM3"               ""                        ""             ""              serial
        5    "GPIB0::5::INSTR"                             "FGEN_2CH"           "Agilent Technologies"    "33522B"       "MY52800145"    gpib  
        6    "GPIB0::11::INSTR"                            "OSCOPE_2CH"         "TEKTRONIX"               "TDS 1002"     "0"             gpib  
    

    Create a connection to the first resource over the VISA-USB interface using the resource name.

    usbdev = visadev("USB0::0x0699::0x036A::CU010105::0::INSTR")
    usbdev = 
    
      USB with properties:
    
             ResourceName: "USB0::0x0699::0x036A::CU010105::0::INSTR"
                    Alias: "NI_SCOPE_4CH"
                   Vendor: "TEKTRONIX"
                    Model: "TDS 2024B"
    
      Show all properties, functions
    

    Alternatively, you can connect to a device using its alias.

    serialdev = visadev("COM1")
    serialdev = 
    
      Serial with properties:
    
             ResourceName: "ASRL1::INSTR"
                    Alias: "COM1"
                     Port: "ASRL1"
                 BaudRate: 9600
    
      Show all properties, functions
    

    When you use visadev in a script or at the MATLAB command line, the result is a connection represented by an object in the MATLAB workspace.

    analyzerResource = "TCPIP0::K-N9040B-91124.dhcp.mathworks.com::inst0::INSTR";
    V = visadev(analyzerResource);
    V = 
    
      TCPIP with properties:
    
             ResourceName: "TCPIP0::K-N9040B-91124.dhcp.mathworks.com::inst0::INSTR"
                    Alias: ""
                   Vendor: "Keysight Technologies"
                    Model: "N9040B"
                  LANName: "inst0"
        InstrumentAddress: "172.31.173.155"
                      Tag: ""
    

    When no references to the same connection exist in other variables, you can disconnect the device by clearing the workspace variable.

    clear(V)

    Use visadevfind to confirm that the connection is closed.

    visadevfind
    ans =
    
         []

    When you have a visadev connection that exists in the MATLAB workspace or is saved as a class property or app property, the visadev object might not be accessible in a different function or app callback. In this case, you can use visadevfind to find and delete the connection.

    V = visadevfind
    V = 
    
      TCPIP with properties:
    
             ResourceName: "TCPIP0::K-N9040B-91124.dhcp.mathworks.com::inst0::INSTR"
                    Alias: ""
                   Vendor: "Keysight Technologies"
                    Model: "N9040B"
                  LANName: "inst0"
        InstrumentAddress: "172.31.173.155"
                      Tag: "Analyzer"
    

    To close this connection, delete V.

    delete(V)

    This command deletes the visadev object and disconnects the device. If you subsequently want to reconnect to the device, you must create a new connection with visadev.

    After the deletion, calling visadevfind confirms that there are no existing connections.

    visadevfind
    ans =
    
         []

    Note that the variable V is still present in the workspace, but it is now an invalid handle.

    V
    V = 
    
      handle to deleted TCPIP

    The variable persists after deletion of the interface because visadev is a handle object. (For more information about this type of object, see Handle Object Behavior.) You can use clear to remove the invalid handle from the workspace.

    clear V

    Version History

    Introduced in R2021a

    expand all