Send a Command/Query to an Instrument/Device through the VISA interface (GPIB / TCP)
#include <Visa.au3>
_viExecCommand ( $h_session, $s_command [, $i_timeout_ms = -1] )
$h_session | 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. |
$s_command | 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) |
$i_timeout_ms | [可选参数] 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. |
Success: | Returns ZERO |
Failure: | Returns -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: | Returns the answer of the instrument to the QUERY |
Failure: | Returns -1 if the VISA DLL could not be open |
Returns -3 if the VISA DLL returned an unexpected number of results | |
or returns a NON ZERO value representing the VISA | |
error code (see the VISA programmer's guide) |
;- 这里假设您安装了仪器到 GPIB 地址 3
; 如果您的仪器安装到了不同的地址, 则改变 "GPIB::3::0" 为
; 相应描述符. 进行对 _viOpen 的相同调用
; 此例子演示如何使用 _viExecCommand 函数, 在单独模式和与
; _viOpen 和 _viClose 的组合.
; 此例子也展示了 _viGTL 函数
#include <Visa.au3>
Local $h_session = 0
; 查询在 GPIB 地址 3 的仪器 ID
MsgBox(4096, "Step 1", "Simple GPIB query using a VISA Descriptor")
Local $s_answer = _viExecCommand("GPIB::3::0", "*IDN?", 10000) ; 超时为 10 秒
MsgBox(4096, "GPIB QUERY result", $s_answer) ; 显示应答
MsgBox(4096, "Step 2", "Go to LOCAL using VISA Descriptor")
_viGTL("GPIB::1::0") ; 到本地 (退出远程控制模式)
MsgBox(4096, "Step 3", "Simple GPIB query using a VISA address shortcut")
$s_answer = _viExecCommand("1", "*IDN?") ; 此地址必须是字符串
MsgBox(4096, "GPIB QUERY result", $s_answer) ; 显示应答
MsgBox(4096, "Info", "Now let's use _viOpen and _viClose")
MsgBox(4096, "Step 4", "Open the instrument connection with _viOpen")
Local $h_instr = _viOpen(3)
MsgBox(4096, "Instrument Handle obtained", "$h_instr = " & $h_instr) ; 显示会话句柄
; 查询仪器
MsgBox(4096, "Step 5", "Query the instrument using the VISA instrument handle")
$s_answer = _viExecCommand($h_instr, "*IDN?") ; $h_instr 现在不是字符串!
MsgBox(4096, "GPIB QUERY result", $s_answer) ; 显示应答
; 再次查询. 不需要再次打开连接
MsgBox(4096, "Step 6", "Query again. There is no need to OPEN the link again")
$s_answer = _viExecCommand($h_instr, "*IDN?")
MsgBox(4096, "GPIB QUERY result", $s_answer) ; 显示应答
MsgBox(4096, "Step 7", "Go to LOCAL using VISA instrument handle")
_viGTL($h_instr); 到本地 (这是可选的)
MsgBox(4096, "Step 8", "Close the Instrument connection using _viClose")
_viClose($h_instr) ; 关闭仪器连接