%% Reading Waveforms from an oscilloscope using a Quick-Control Oscilloscope Object

% This example shows how to use the Quick-Control Oscilloscope to acquire waveforms from an oscilloscope.


% Instrument Control Toolbox(TM) software supports communication with
% instruments through  Quick-Control Instrument objects.  In this example you will
% learn to acquire a waveform from a Keysight Technologies(R) (formerly 
% Agilent Technologies(R)) MSO6014 mixed signal oscilloscope using a 
% Quick-Control oscilloscope object.
%
% For a complete list of hardware supported by the toolbox, visit the Instrument
% Control Toolbox homepage at <https://www.mathworks.com/products/instrument/>
%
% Copyright 2011-2017 The MathWorks, Inc.


%% Introduction
% This example is tested on a 32-bit Microsoft(R) Windows(R) system,
% National Instruments(R) Compliance Package 4.1 . Keysight I/O Suite and
% 546XX IVI-C driver version 1.3.20.0, which can be downloaded from Keysight's website:
% <http://www.keysight.com>.  Ensure that the VISA utility has been set up 
% to recognize the instrument resource before you execute this example.

%% Creating an Oscilloscope
% Before acquiring any data, you must create an oscilloscope instance.
myScope = oscilloscope()

%% Discovering Available Resources
% Find out available resources. A resource is a string identifier to the 
% instrument. You have to set it before connecting to the instrument.
availableResources = getResources(myScope)

%% Connecting to the Oscilloscope Object
% If multiple resources are available, use a VISA utility to verify the
% correct resource and set it.
myScope.Resource = 'TCPIP0::a-m6104a-004598::inst0::INSTR';

% Connect to the instrument.
connect(myScope);

%% Examine the Current Oscilloscope Setting
get(myScope);

%% Configuring the Oscilloscope
% Configure the oscilloscope's settings. The configuration we will use in this example is:
% acquisition time of 0.01 second with 2000 data points, trigger level of
% 0.1v and normal trigger mode, channel one enabled and vertical settings as shown below.

% Automatically configuring the instrument based on the input signal.
autoSetup(myScope);
 
myScope.AcquisitionTime = 0.01;
 
myScope.WaveformLength = 2000;

myScope.TriggerMode = 'normal';

myScope.TriggerLevel = 0.1;

enableChannel(myScope, 'Channel1');

setVerticalCoupling (myScope, 'Channel1', 'AC');

setVerticalRange (myScope, 'Channel1', 5.0);

%% Getting a Waveform from Channel One
% This function initiates an acquisition on the enabled channel. It
% then waits for the acquisition to complete and returns the waveform
% for the specified channel.
waveformArray = getWaveform(myScope, 'acquisition', true);

% Plot the waveform.
plot(waveformArray);
xlabel('Samples');
ylabel('Voltage');


%% Cleaning Up
% Once you have finished configuring the instrument and retrieved data
% from it, you need to close the connection and remove it from the workspace.
disconnect(myScope);
clear myScope;

%%
