%% Read Waveform Data from Keysight(R) DSO-X 2002A Oscilloscopes using the IVI-C Driver
% This example shows how to initialize the driver, read a few properties of 
% the driver, acquire waveform data using Agilent Technologies DSO-X 2002A
% oscilloscope and output the result in MATLAB(R).

%%
% Copyright 2015 The MathWorks, Inc.

%% Requirements
% This example requires the following to be installed on the computer:
%
% * Keysight (Agilent) IO libraries version 17.1 or newer; 
% * Keysight (Agilent) 2000, 3000, 4000, 6000 InfiniiVision X-Series
% Oscilloscope IVI driver version 2.1.6.0 or newer;
%

%% Enumerate Available IVI-C Drivers on the Computer
% This enumerates the IVI drivers that have been installed on the computer.
IviInfo = instrhwinfo('ivi');
IviInfo.Modules

%% Create MATLAB Instrument Driver And Connect To The Instrument

% Create the MATLAB instrument driver
makemid('AgInfiniiVision','AgInfiniiVision.mdd')

% Use icdevice with the MATLAB instrument driver name and instrument's 
% resource name to create a device object. In this example the instrument 
% is connected by GPIB at board index 0 and primary address 1.
myInstrument = icdevice('AgInfiniiVision.mdd', 'GPIB0::01::INSTR','optionstring','simulate=true');

% Connect driver instance
connect(myInstrument);

%% Get General Device Properties
% Query information about the driver and instrument

% Get information about the driver
Utility = get(myInstrument, 'Utility');
Revision = invoke(Utility, 'revisionquery');
DriverIdentification = get(myInstrument,'Inherentiviattributesdriveridentification');
InstrumentIdentification = get(myInstrument,'Inherentiviattributesinstrumentidentification');
Vendor = get(DriverIdentification, 'Specific_Driver_Vendor');
Description = get(DriverIdentification, 'Specific_Driver_Description');
InstrumentModel = get(InstrumentIdentification, 'Instrument_Model');
FirmwareRev = get(InstrumentIdentification, 'Instrument_Firmware_Revision');

% Print the queried driver properties
fprintf('Revision:        %s\n', Revision);
fprintf('Vendor:          %s\n', Vendor);
fprintf('Description:     %s\n', Description);
fprintf('InstrumentModel: %s\n', InstrumentModel);
fprintf('FirmwareRev:     %s\n', FirmwareRev);
fprintf(' \n');

%% Setup The Measurements And Fetch A Waveform From The Oscilloscope
Measurement = get(myInstrument, 'Instrumentspecificmeasurement');
invoke(Measurement, 'measurementsautosetup');
WaveformArray = zeros(1,100);
[WaveformArray,ActualPoints,InitialX,Xincreament] = invoke(Measurement, 'measurementfetchwaveform', 'Channel1', size(WaveformArray,2), WaveformArray);

%% Visualize Data And Display Any Errors

% Display the fetched data
plot(WaveformArray(1:ActualPoints));

% If there are any errors, query the driver to retrieve and display them.
ErrorNum = 1;
while (ErrorNum ~= 0)
    [ErrorNum, ErrorMsg] = invoke(Utility, 'errorquery');
    fprintf('ErrorQuery: %d, %s\n', ErrorNum, ErrorMsg);
end

%% Disconnect the Device Object and Clean Up
disconnect(myInstrument);
% Remove instrument objects from memory
delete(myInstrument);

%%



%% Additional Information:
% This example shows the setup and acquisition of data from an oscilloscope
% using the IVI driver. Once the measured waveform is retrieved from the 
% instrument, MATLAB can be used to visualize and perform analyses on the 
% data using the rich library of functions in the Signal Processing
% Toolbox(TM) and Communications Systems Toolbox(TM). Using Instrument
% Control Toolbox(TM), it is possible to automate control of instruments,
% and, build test systems that use MATLAB to perform analyses that may not
% be possible using the built-in capability of the hardware.
