Measurement Computing   Easy to Use | Easy to Integrate | Easy to Support catalog banner

Using DaqFlex with MatLab Data Acquisiton

Expand / Collapse
 

Using DaqFlex with MatLab Data Acquisiton


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:

  • USB-201, USB-201-OEM
  • USB-202, USB-202-OEM
  • USB-204, USB-204-OEM
  • USB-205, USB-205-OEM
  • USB-1208FS-Plus
  • USB-1408FS-Plus
  • USB-1608FS-Plus
  • USB-1608G
  • USB-2001-TC
  • USB-2408
  • USB-2408-2AO 
  • USB-7202
  • USB-7204

%///////////////////////////////// DaqFlex.m //////////////////////////////////
%% 
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)


Rate this Article:

Attachments


DaqFlex.m DaqFlex.m (2.90 KB, 877 views)

Add Your Comments


For comments email TechSupport@mccdaq.com.

Details
Article ID: 50539

Last Modified:6/6/2014 1:50:09 PM

Article has been viewed 6,038 times.

Options