Function Reference


_viExecCommand

Send a Command/Query to an Instrument/Device through the VISA interface (GPIB / TCP)

#include <Visa.au3>
_viExecCommand ( $hSession, $sCommand [, $iTimeoutMS = -1 [, $sMode = @LF]] )

Parameters

$hSession A VISA descriptor (STRING) OR a VISA session handle (INTEGER)
This it can be a string or an integer (a handle):
* STRING -&gr; A VISA DESCRIPTOR is a string which specifies the resource with which to establish a communication session.
An example descriptor is "GPIB::20::0".
This function supports all valid VISA descriptors, including GPIB, TCP, VXI and Serial Interface instruments (ASRL).
A detailed explanation of VISA descriptors is shown in the Remarks section of this function.
As a SHORTCUT you can also use a STRING containing the address number (e.g. "20") of a GPIB instrument instead of typing the full descriptor (in that case, "GPIB::20::0")
* INTEGER -&gr; A VISA session handle is an integer value returned by _viOpen().
It is recommended that instead you use _viOpen() and VISA session handles instead of descriptors if you plan to communicate repeteadly with an Instrument or Device, as otherwise each time that you contact the instrument you would incur the overhead of opening and closing the communication link.
Once you are done using the instrument you must remember to close the link with _viClose().
$sCommand Command/Query to execute (e.g. "*IDN?" or "SOURCE:POWER -20 dBM")
A query MUST contain a QUESTION MARK (?)
When the command is a QUERY the function will automatically wait for the instrument's answer (or until the operation times out)
$iTimeoutMS [optional] The operation timeout in MILISECONDS.
This is mostly important for QUERIES only.
If it is not specified the last set timeout will be used. If it was never set before the default timeout (which depends on the VISA implementation) will be used.
Timeouts can also be set separatelly with the _viSetTimeout() function.
Depending on the bus type (GPIB, TCP, etc) the timeout might not be set to the exact value that you request. Instead the closest valid timeout bigger than the one that you requested will be used.
$sMode [optional] Control the mode in which the VISA viPrintf is called when $sCommand is not a query.
Default is @LF, which means "attach @LF mode".
Some instruments and in particular many GPIB cards do not honor the terminator character attribute in those cases an @LF terminator needs to be added.
As this is the most common case, by default the mode is set to @LF, which appends @LF to the SCPI command you can also set this mode to @CR and @CRLF if your card uses those terminators.
If you do not want to use a terminator, set this parameter to an empty string ("")
Also, some cards support the execution of a "sprintf" on the SCPI string prior to sending it through the VISA interface.
For those who do, it is possible, by setting this parameter to "str" to "protect" the VISa interface from accidentally applying an escape sequence when a "/" is found within the VISA command string.
This is normally NOT necessary and should only be set if your GPIB card or instrument require it.

Return Value

Returns a value which depends on whether the command is a QUERY or not and in whether the operation was successful or not.

* Command, NON QUERY:
Success: 0.
Failure: -1 if the VISA DLL could not be open
or a NON ZERO value representing the VISA error code (see the VISA programmer's guide)

Success: the answer of the instrument to the QUERY
Failure: -1 if the VISA DLL could not be open
-3 if the VISA DLL returned an unexpected number of results
or a NON ZERO value representing the VISA error code (see the VISA programmer's guide)

Remarks

* The VISA queries only return the 1st line of the device answer.
This is not a problem in most cases, as most devices will always answer with a single line.

* The following is a description of the MOST COMMON VISA DESCRIPTORS
Note that there are some more types. For more info please refer to a VISA programmer's guide (available at www.ni.com).
Optional segments are shown in square brackets ([]).
Required segments that must be filled in are denoted by angle brackets (<&gr;).

Interface    Syntax
------------------------------------------------------------
GPIB INSTR    GPIB[board]::primary address
    [::secondary address] [::INSTR]
GPIB INTFC    GPIB[board]::INTFC
TCPIP SOCKET    TCPIP[board]::host address::port::SOCKET
Serial INSTR    ASRL[board][::INSTR]
PXI INSTR    PXI[board]::device[::function][::INSTR]
VXI INSTR    VXI[board]::VXI logical address[::INSTR]
GPIB-VXI INSTR    GPIB-VXI[board]::VXI logical address[::INSTR]
TCPIP INSTR    TCPIP[board]::host address[::LAN device name]
    [::INSTR]

The GPIB keyword is used for GPIB instruments.
The TCPIP keyword is used for TCP/IP communication.
The ASRL keyword is used for serial instruments.
The PXI keyword is used for PXI instruments.
The VXI keyword is used for VXI instruments via either embedded or MXIbus controllers.
The GPIB-VXI keyword is used for VXI instruments via a GPIB-VXI controller.

The default values for optional parameters are shown below.

Optional Segment    Default Value
---------------------------------------
board    0
secondary address    none
LAN device name    inst0


Example Resource Strings:
--------------------------------------------------------------
GPIB::1::0::INSTR    A GPIB device at primary address 1 and
    secondary address 0 in GPIB interface 0.

GPIB2::INTFC    Interface or raw resource for GPIB
    interface 2.

TCPIP0::1.2.3.4::999::SOCKET    Raw TCP/IP access to port 999
    at the specified IP address.

ASRL1::INSTR    A serial device attached to interface
    ASRL1.    VXI::MEMACC Board-level register
    access to the VXI interface.

PXI::15::INSTR    PXI device number 15 on bus 0.

VXI0::1::INSTR    A VXI device at logical address 1 in VXI
    interface VXI0.

GPIB-VXI::9::INSTR    A VXI device at logical address 9 in a
    GPIB-VXI controlled system.

Related

_viClose, _viFindGpib, _viGpibBusReset, _viGTL, _viOpen, _viSetAttribute, _viSetTimeout

Example

; - This assumes that you have instrument set to GPIB address 3
; If you have an instrument in a different address change "GPIB::3::0" to the
; corresponding descriptor. Do the same for the call to _viOpen
; It shows how to use the _viExecCommand function in stand alone mode and combined
; with _viOpen and _viClose.
; It also shows the _viGTL function

#include <MsgBoxConstants.au3>
#include <Visa.au3>

Local $h_Session = 0

; Query the ID of the instrument in GPIB address 3
MsgBox($MB_SYSTEMMODAL, "Step 1", "Simple GPIB query using a VISA Descriptor")
Local $s_Answer = _viExecCommand("GPIB::3::0", "*IDN?", 10000) ; 10 secs timeout
MsgBox($MB_SYSTEMMODAL, "GPIB QUERY result", $s_Answer) ; Show the answer

MsgBox($MB_SYSTEMMODAL, "Step 2", "Go to LOCAL using VISA Descriptor")
_viGTL("GPIB::1::0") ; Go to local (exit remote control mode)

MsgBox($MB_SYSTEMMODAL, "Step 3", "Simple GPIB query using a VISA address shortcut")
$s_Answer = _viExecCommand("1", "*IDN?") ; The address MUST BE A STRING
MsgBox($MB_SYSTEMMODAL, "GPIB QUERY result", $s_Answer) ; Show the answer
MsgBox($MB_SYSTEMMODAL, "Info", "Now let's use _viOpen and _viClose")

MsgBox($MB_SYSTEMMODAL, "Step 4", "Open the instrument connection with _viOpen")
Local $h_Instr = _viOpen(3)
MsgBox($MB_SYSTEMMODAL, "Instrument Handle obtained", "$h_Instr = " & $h_Instr) ; Show the Session Handle
; Query the instrument

MsgBox($MB_SYSTEMMODAL, "Step 5", "Query the instrument using the VISA instrument handle")
$s_Answer = _viExecCommand($h_Instr, "*IDN?") ; $h_Instr is NOT A STRING now!
MsgBox($MB_SYSTEMMODAL, "GPIB QUERY result", $s_Answer) ; Show the answer
; Query again. There is no need to OPEN the link again

MsgBox($MB_SYSTEMMODAL, "Step 6", "Query again. There is no need to OPEN the link again")
$s_Answer = _viExecCommand($h_Instr, "*IDN?")
MsgBox($MB_SYSTEMMODAL, "GPIB QUERY result", $s_Answer) ; Show the answer

MsgBox($MB_SYSTEMMODAL, "Step 7", "Go to LOCAL using VISA instrument handle")
_viGTL($h_Instr); Go to local (this is optional)

MsgBox($MB_SYSTEMMODAL, "Step 8", "Close the Instrument connection using _viClose")
_viClose($h_Instr) ; Close the instrument connection