Traditionally, to use Measurement Computing (MCC) devices in MatLab required the MatLab Data Acquisition Toolbox add on module. The Data Acquisition Toolbox uses the MCC Universal Library to control the devices. However, if you do not have the Data Acquisition Toolbox MCC offers select products that feature the DaqFlex message based driver in addition to the Universal Library. Below is an example program demonstrating control of a USB-7202 data acquisition board. DaqFlex enable products include the following:
%%
clear; clc;
%%
mcAssembly = NET.addAssembly('DAQFlex');
%% get list of the attached daq devices
mcArgument = MeasurementComputing.DAQFlex.DeviceNameFormat.NameAndSerno;
mcDevices = MeasurementComputing.DAQFlex.DaqDeviceManager.GetDeviceNames(mcArgument);
%% Choose First Device, and create a DaqDevice object for it
mcDeviceString = mcDevices(1);
mcDevice = MeasurementComputing.DAQFlex.DaqDeviceManager.CreateDevice(mcDeviceString);
try
%set first channel
mcDevice.SendMessage('AISCAN:LOWCHAN=0').ToString();
%set last channel
mcDevice.SendMessage('AISCAN:HIGHCHAN=0').ToString();
%set sample rate in hertz
mcDevice.SendMessage('AISCAN:RATE=5000').ToString();
rate=5000; %use to scale display
%enable data conversion to volts
mcDevice.SendMessage('AISCAN:SCALE=ENABLE').ToString();
%set acquisition to run continously
mcDevice.SendMessage('AISCAN:SAMPLES=0').ToString();
%set the input range to +/- 5 volts
mcDevice.SendMessage('AISCAN:RANGE=BIP5V').ToString();
range = 5.0;
%begin acquisiton
mcDevice.SendMessage('AISCAN:START').ToString();
fprintf('Acquisition Start\n');
%get the number of samples acquired so far
Count = char(mcDevice.SendMessage('?AISCAN:COUNT').ToString());
%parse the string
ct = strsplit(Count,'=');
%convert the number string to number
CurrentCount = str2num(ct{2});
%desiredCount is the amount I want to read on each iteration
blocksize = 256;
tm = blocksize/rate; %time to acquire blocksize
%create window for displaying plots
h = figure;
x = transpose((0:blocksize-1) * 1 / rate);
myCount = 0;
%The acquisition is set to continuously acquire data
%however for example purpose stop after 50000 reading
while myCount < 50000
%need to add sleep or pause so that DaqFlex can update the count
%variable
pause(tm);
%read the device and put the data into an array
DataArray = transpose(double(mcDevice.ReadScanData(blocksize,10000)));
myCount = myCount + blocksize;
%plot the data
plot(x(1:blocksize),DataArray(1:blocksize));
axis([0.0 (blocksize * 1/rate) -range range ]);
title('Voltage Waveform');
xlabel('Time');
ylabel('Volts');
Count = char(mcDevice.SendMessage('?AISCAN:COUNT').ToString());
ct = strsplit(Count,'=');
CurrentCount = str2num(ct{2});
end
fprintf('\nTotal number read %d\n', myCount)
catch err
getReport(err,'basic')
mcDevice.SendMessage('AISCAN:STOP').ToString()
MeasurementComputing.DAQFlex.DaqDeviceManager.ReleaseDevice(mcDevice)
break
end
fprintf('\nAcquisition Stop\n');
mcDevice.SendMessage('AISCAN:STOP').ToString();
MeasurementComputing.DAQFlex.DaqDeviceManager.ReleaseDevice(mcDevice)