%% octave_vxi11_demo.m % % No-GUI GNU Octave demo talking to an HP 34401A (or any SCPI DMM) over % native VXI-11 -- the same wire protocol the GPIBee itself speaks, so % this connects directly, without needing any vendor VISA runtime % installed at all (unlike the Codeblocks/Lazarus/Excel examples next % door, which all go through visa32.dll). % % Uses the instrument-control package's built-in vxi11() interface -- % see README.md for why that's the recommended choice over Octave's % visa() function right now, and for the one real caveat: some % instrument-control builds are compiled WITHOUT VXI-11 support (this % is silent and looks nothing like a network problem). Run % check_vxi11_support.m first if you're not sure about yours. % % Run: octave --no-gui octave_vxi11_demo.m % % Note on structure: Octave requires a script's local functions to be % defined before the executable code that calls them (unlike MATLAB, % which allows either order) -- that's why the helper functions come % first here and the actual "main" logic is the block at the bottom. % The lone "1;" right below is required too: without it, Octave sees % the file starting with "function" and treats the whole file as a % function definition instead of a script, silently skipping the % executable code at the bottom. 1; %% ---- local functions ------------------------------------------------- function pkg_load_instrument_control() try pkg load instrument-control catch err error(["Could not load the instrument-control package: %s\n" ... "Install it with: pkg install -forge instrument-control"], err.message); end_try_catch endfunction function vi = open_instrument(ip, instr) if exist("vxi11") == 0 error("vxi11() is not defined -- this instrument-control build has no VXI-11 support at all."); endif try vi = vxi11(ip, instr); catch err if !isempty(strfind(err.message, "doesn't support the VXI11 interface")) error(["vxi11(): this Octave/instrument-control build was compiled WITHOUT VXI-11 " ... "(RPC) support -- run check_vxi11_support.m and see README.md for details. " ... "(original error: %s)"], err.message); else error("vxi11(\"%s\", \"%s\") failed: %s", ip, instr, err.message); endif end_try_catch % Note: this package version has no separate "set timeout" call for % vxi11 objects (io_timeout_s in the main block below is informational % only) -- the RPC call itself blocks using the VXI-11 server's own % timeout handling. endfunction function response = query_instrument(vi, command) vxi11_write(vi, [command "\n"]); [data, count] = vxi11_read(vi, 4096); response = strip_termination(char(reshape(data(1:count), 1, count))); endfunction function s = strip_termination(s) while !isempty(s) && any(s(end) == sprintf("\r\n")) s(end) = []; endwhile endfunction %% ---- configuration ----------------------------------------------------- instrument_ip = "192.168.3.2"; % GPIBee / instrument LAN address instrument_name = "inst0,4"; % VXI-11 device name (same convention as % the "TCPIP::::inst0,4::INSTR" VISA % resource used throughout the other % examples in this project) read_count = 10; % number of MEAS:VOLT:DC? readings io_timeout_s = 3; % informational only -- see open_instrument() %% ---- main ---------------------------------------------------------------- pkg_load_instrument_control(); printf("Connecting to VXI-11 instrument %s at %s ...\n", instrument_name, instrument_ip); vi = []; unwind_protect vi = open_instrument(instrument_ip, instrument_name); printf("Connected.\n"); idn = query_instrument(vi, "*IDN?"); printf("*IDN?: %s\n", idn); printf("Reading %d DC-voltage measurement(s) via MEAS:VOLT:DC? ...\n", read_count); for i = 1:read_count response = query_instrument(vi, "MEAS:VOLT:DC?"); value = str2double(response); printf("[%s] #%2d: %-18s -> %.6f Vdc\n", datestr(now(), "HH:MM:SS"), i, response, value); endfor unwind_protect_cleanup if !isempty(vi) vxi11_close(vi); printf("Connection closed.\n"); endif end_unwind_protect