%% Read Voltage Through NI-DMM MATLAB Instrument Driver in Simulation Mode
%
% This example shows how to read voltage from a National Instruments(R)
% NI-DMM driver in the simulation mode.
%
% Copyright 2013-2020 The MathWorks, Inc.


%% Requirements
% This example requires a Microsoft(R) Windows(R) system and NI-DMM
% package 3.06 or higher. Make sure the Measurement & Automation Explorer
% recognizes the NI-DMM device before you use this example.


%% Verify NI-DMM Installation
% Use the |instrhwinfo| command to check if the NI-DMM software package
% is installed correctly. If installed correctly, NI-DMM is listed as one of
% the modules installed on the Windows machine. This example uses libraries
% installed with it.

driversInfo = instrhwinfo ('ivi');
disp(driversInfo.Modules');


%% Create a MATLAB Instrument Object 
% Use the |icdevice| function to create an instrument object from the MDD
% that was part of the NI-DMM support package, and establish a connection
% to the DMM using that object.
%
% The |icdevice| function takes two or more input arguments: the MDD file name,
% the resource name for the DMM, and optional device-specific parameters.
%
% You can get the resource name for the DMM from NI Measurement and
% Automation Explorer. For example: A resource name of |PXI1Slot6| in NI MAX
% would be |DAQ::PXI1Slot6| and |Device 1| would be |DAQ::Dev1|. You can
% remove the |optionstring| argument and the corresponding string parameter
% if you have the actual hardware.
%
% You can establish a connection to the DMM using the |connect| command.

ictObj = icdevice('nidmm.mdd', 'DAQ::Dev1', 'optionstring','simulate=true');
connect(ictObj);
disp(ictObj);


%% Configure the DMM
% For the purpose of this example, the DMM is configured as
%
%   * Measurement Function: DC Voltage
%
%   * Range: 10V
%
%   * Resolution: 5.5 Digits
%
% Use the MATLAB Instrument Driver Editor |midedit| to view other
% properties and functions that allow you to configure a device. The tool
% shows all the properties and functions that the NI-DMM software package
% supports.
%
% The Measurement Function value for DC Voltage is 1. Measurement Function
% will have different values for other measurement types such as AC
% Voltage, DC Current, etc.  

measurementFunction = 1;
range = 10;
resolution = 5.5;

configuration = get(ictObj, 'configuration');
invoke(configuration, 'configuremeasurementdigits', measurementFunction, range, resolution);


%% Read and Display the Voltage
% Once you configure the DMM with the required settings, use an
% appropriate function call to read the voltage.

% Configure DMM to calculate the timeout automatically
AutoTimeLimit = -1;

acquisition = get(ictObj, 'acquisition');
volts =  invoke(acquisition, 'read', AutoTimeLimit);

voltageDisplay = sprintf('Voltage : %d v', volts);
disp(voltageDisplay);


%% Clear the Connection
% When you are finished working with the instrument, disconnect from and
% delete the MATLAB Instrument Object.

disconnect(ictObj);
delete(ictObj);
clear ictObj;
