Documentation

Transition Your Code to Session-Based Interface

This topic helps you transition your code from the legacy interface to the session-based interface. For more information on choosing your interface, see Choose the Right Interface.

Transition Common Workflow Commands

This table lists the legacy commands for common workflows and their corresponding session-based commands.

To do this Legacy CommandSession-Based Command
Find supported hardware available to your system.daqhwinfodaq.getDevices
Registered DAQ adaptor.daqregisterYou do not need to register an adaptor if you are using session-based interface.
Reset MATLAB to initial state.daqresetdaq.reset
Discover newly connected hardware.Shut down MATLAB and restart.daq.reset
Create analog input object and add a channel.
ai = analoginput
		('nidaq', 'Dev1');
addchannel(ai, 1)
s=daq.createSession('ni'); 
addAnalogInputChannel
      (s,'Dev1',1,'Voltage');
Create analog output object
ao = analogoutput
		('nidaq', 'Dev1'); 
addchannel(ao, 1)
addAnalogOutputChannel
      (s,'Dev1',0,'Current')
Create a digital input and output object and add a digital input line.
dio = digitalio
   ('nidaq','Dev1'); 
addline(dio,0:3,'in');
s = daq.createSession('ni'); 
addDigitalChannel
  (s,'Dev1','Port0/Line0:1','InputOnly');
Create counter input channelsYou cannot use counter channels in the legacy interface.
s = daq.createSession ('ni') 
addCounterInputChannel
      (s,'Dev1','ctr0','EdgeCount')
Start the object.
start(ai)
startForeground(s);

for operations that block MATLAB® when running.

startBackground (s);

for operations that run without blocking MATLAB.

Set rate of acquisition.
ai.SampleRate=48000
s.rate=48000
Specify an external trigger.
ai.TriggerType=
			'HwDigital';
addTriggerConnection
(s,'External','Dev3/PFI0','StartTrigger');
Specify a range of input signals
ai.Channel.InputRange=[-5 5];
ch = addAnalogInputChannel
			(s,'Dev1',1,'Voltage');
ch.Range = [-5 5];

Acquire Analog Data

Legacy Interface

Using the legacy interface, you find hardware available to your system, create an analog input object and start acquisition.

  1. Find hardware available to your system.

    d = daqhwinfo;
  2. Create an analog input object and add a channel using a National Instruments® device, with ID, Dev1.

    ai = analoginput('nidaq', 'Dev1');
    addchannel(ai, 1)
  3. Set the sample rate to 8000 and start the channel.

    ai.SampleRate = 8000;
    start(ai)

Session-Based Interface

Using the session-based interface, you create a vendor session and add channels to the session. You can use any device or chassis from the same vendor available to your system and can add a combination of analog, digital, and counter input and output channels. All the channels operate together when you start the session.

  1. Find hardware available to your system.

    d = daq.getDevices
  2. Create a session for National Instruments devices.

    s = daq.createSession('ni');
  3. Set the session's sample rate to 8000.

    s.Rate = 8000
  4. Add an analog input channel for the device with ID Dev1 with Voltage measurement type and start the acquisition.

    addAnalogInputChannel(s,'Dev1',1,'Voltage');
    startForeground(s);

Use Triggers

Acquire analog data using hardware triggers

Legacy Interface

Analog operations are configured to trigger immediately by default. You must specify hwDigital trigger type.

Create an analog input object and add two channels

  1. Create an analog input object and add two channels

    ai = analoginput('nidaq', 'Dev1');
    chan = addchannel(ai, 0:1)
    
  2. Specify the ranges of the channel to scale the data uniformly. Configure the input type to be SingleEnded terminal.

    chan.InputRange = [-10 10];
    chan.UnitsRange = [-10 10];
    chan.SensorRange = [-10 10];
    chan.InputType = 'SingleEnded';
    
  3. Specify the trigger type, source and condition. Set TriggerRepeat to 0.

    ai.TriggerType = 'HwDigital';
    ai.HwDigitalTriggerSource = 'PFI0';
    ai.TriggerCondition = 'PositiveEdge';
    ai.TriggerRepeat = 0;
  4. Specify rate and duration.

    actualRate = setverify(ai, 'SampleRate', 50000);
    duration = 0.01;
    ai.SamplesPerTrigger = duration*actualRate);
  5. Start the channel, wait until the channel receives the specified amount of data and get the data.

    start(ai);
    
    wait(ai, duration+1);
    [data, timestamps] = getdata(ai);
  6. Plot the data.

    plot(timestamps, data);

Session-Based Interface

You can specify an external event to trigger data acquisition using the session-based interface.

  1. Create a session and add two analog input channels.

    s = daq.createSession('ni');
    ch = addAnalogInputChannel(s,'Dev1', 0:1, 'Voltage');
    
  2. Configure the terminal and range of the channels in the session.

    ch(1).TerminalConfig = 'SingleEnded';
    ch(1).Range = [-10.0 10.0];
    ch(2).TerminalConfig = 'SingleEnded';
    ch(2).Range = [-10.0 10.0];
  3. Create an external trigger connection and set the trigger to run one time.

    addTriggerConnection(s,'External','Dev1/PFI0','StartTrigger');
    s.Connections(1).TriggerCondition = 'RisingEdge';
    s.TriggersPerRun = 1;
  4. Set the rate and the duration of the acquisition.

    s.Rate = 50000;
    s.DurationInSeconds = 0.01;
    
  5. Acquire data in the foreground and plot the data.

    [data, timestamps] = startForeground(s);
    plot(timestamps, data);

Log Data

Legacy Interface

You can log the data to disk and use daqread to read the data back.

  1. Create the analog input object and add two channels.

    ai = analoginput('winsound');
    ch = addchannel(ai,0:1);
  2. Define a 2 second acquisition for each trigger, set the trigger to repeat three times, and log information to the file file00.daq.

    duration = 2; 
    ai.SampleRate = 8000
    actualRate = ai.SampleRate;
    ai.SamplesPerTrigger = duration*ActualRate
    ai.TriggerRepeat = 3
    ai.LogFileName = 'file00.daq'
    ai = LoggingMode = 'Disk&Memory'
  3. Start the acquisition, wait for duration of the acquisition times the number of triggers for the acquisition to complete. Then extract all the data stored in the log file as sample-time pairs.

    start(ai)
    wait(ai, (ai.TriggerRepeat + 1) * duration + 1)
    [data,time] = daqread('file00.daq');

Session-Based Interface

Session-based interface does not have a specified file format to log data. You can write to a file in binary mode or save data to a MATLAB file.

  1. Create a session and add 4 analog input channels from Dev1.

    s = daq.createSession('ni');
    ch = addAnalogInputChannel(s,'Dev3', 0:3, 'Voltage');
    
  2. Set the same range and terminals for all the channels.

    ch.Range = [-10.0 10.0];
    ch.TerminalConfig = 'SingleEnded');
  3. Set the sessions rate and duration of acquisition.

    s.Rate = 50000;
    s.DurationInSeconds = 0.01;
  4. Start the acquisition and plot the data.

    [data, timestamps] =  startForeground(s);
    figure; plot(timestamps, data);
    
  5. Save the acquired data to a MATLAB file.

    fileName = 'data.mat';
    save(fileName, 'timestamps', 'data');
    
  6. Load data from the file into the MATLAB workspace.

    savedData = load('data.mat');
    figure; plot(savedData.timestamps, savedData.data);
    

Set Range of Analog Input Subsystem

You can specify the measurement range of an analog input subsystem.

Legacy Interface

  1. Create the analog input object ai for a National Instruments® device, and add two channels to it.

    ai = analoginput('nidaq','Dev1');
    addchannel(ai,0:1);
  2. Configure both channels to accept input signals between -10 volts and 10 volts.

    ai.Channel.InputRange = [-10 10];

Session-Based Interface

  1. Create a session and add an analog input channel.

    s = daq.createSession('ni');
    ch = addAnalogInputChannel(s, 'Dev1', 'ai1', 'Voltage')
  2. Change the range to —10 to 10 volts.

    ch.Range = [-10 10];

Fire an Event When Number of Scans Exceed Specified Value

You can specify your acquisition to watch for a specified number of scans to occur and fire an event if the acquisition exceeds the specified number.

Legacy Interface

You can use the BufferingConfig property to specify allocated memory for a specified channel. If the number of samples acquired exceeds the allocated memory, then an error is returned.

  1. Create an analog input object ai for a National Instruments device and add a channel to it

    ai = analoginput('nidaq','Dev1');
    ch = addchannel(ai,0);
    
  2. Set the rate to 800,000.

    ai.SampleRate = 800000)
  3. Set the bufferingConfigMode to Manual and set the bufferingConfig to

    ai.bufferingConfigMode = 'Manual'
    ai.bufferingConfig = [512 30];

Session-Based Interface

Use the NotifyWhenDataAvailableExceeds property to fire a DataAvailable event.

  1. Create an acquisition session, add an analog input channel.

    s = daq.createSession('ni');
    addAnalogInputChannel(s,'Dev1','ai0','Voltage');
  2. Set the Rate to 800,000 scans per second, which automatically sets the DataAvailable notification to automatically fire 10 times per second.

    s.Rate = 800000;
    s.NotifyWhenDataAvailableExceeds
    ans =
                    80000
  3. Increase NotifyWhenDataAvailableExceeds manually to 160,000.

    s.NotifyWhenDataAvailableExceeds = 160000;

Use Timeout to Block MATLAB While an Operation Completes

Legacy Interface

  1. Create an analog output object ai for a National Instruments device, add a channel and set it to output data at 8000 samples per second with one manual trigger.

    ai = analogoutput('nidaq','Dev1');
    ch = addchannel(a0,1);
    ao.SampleRate =  8000;
    ao.TriggerType = 'manual';
    ao,.RepeatOutput = 0;
    putdata (ao(zeros(10000,1)));
  2. Start the acquisition and issue a wait command for the acquisition to block MATLAB for two seconds. If the acquisition does not complete in two seconds, a timeout occurs.

    start(a0)
    wait(a0,2)

Session-Based Interface

Background operations run without interrupting the MATLAB command window. Use wait to block operations in the MATLAB command window during background operations.

  1. Create an acquisition session, add an analog output channel.

    s = daq.createSession('ni');
    addAnalogOutputChannel(s,'Dev1','ao0','Voltage');
  2. Set the session rate to 8000.

    s.Rate=8000
  3. queue some output data

    queueOutputData(s,zeros(10000,1));	
  4. Start the acquisition and issue a wait to block MATLAB for 16 seconds. If the operation does not complete in 2 seconds, a timeout occurs.

    startBackground(s);
    s.wait(2);

Count Pulses

You can count pulses to clock your data acquisition.

Legacy Interface

You cannot use counter input and output channels using the legacy interface. You can use the analog input subsystem's internal clock to create a threshold and look for consecutive samples that are on opposite sides of the threshold. This will give you results similar to using a counter input channel.

ai = analoginput('nidaq');
addchannel(ai, 1)
threshold = 3.5;
offsetData = [data(2:end); NaN];
risingEdge = find(data < threshold & offsetData > threshold);
fallingEdge = find(data > threshold & offsetData < threshold);

Session-Based Interface

Count edges of a pulse using a counter input channel on your device.

s.createSession('ni')
addCounterInputChannel(s,'Dev1', 'ctr0', 'EdgeCount');
inputSingleScan(s)
Was this topic helpful?