Transition Your Code to Session-Based Interface

This topic helps you transition your code from the legacy interface to the session-based 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.daqresetdaqreset
Discover newly connected hardware.Shut down MATLAB and restart.daqreset
Create analog input object and add a channel.
ai = analoginput
		('nidaq', 'Dev1');
addchannel(ai, 1)
Create analog output object
ao = analogoutput
		('nidaq', 'Dev1'); 
addchannel(ao, 1)
Create a digital input and output object and add a digital input line.
dio = digitalio
s = daq.createSession('ni'); 
Create counter input channelsYou cannot use counter channels in the legacy interface.
s = daq.createSession ('ni') 
Start the object.

for operations that block MATLAB® when running.

startBackground (s);

for operations that run without blocking MATLAB.

Set rate of acquisition.
Specify an external trigger.
Specify a range of input signals
ai.Channel.InputRange=[-5 5];
ch = addAnalogInputChannel
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');
  3. Set the sample rate to 8000 and start the channel.

    ai.SampleRate = 8000;

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.

Compatibility Note

For devices that support both AC and DC coupling, in the session interface the default connection is DC coupling, setting the channel’s Coupling property to 'DC'. For the same devices in the legacy interface, the default was AC coupling.

  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 for voltage measurement, and then start the acquisition.


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.

    [data,timestamps] = getdata(ai);
  6. Plot the 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.

    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);

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.

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

Session-Based Interface

The session-based interface does not have a specified file format for logging data. You can write to a file in binary mode or save data to a MAT-file.

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

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

    ch(1).Range = [-10.0 10.0];
    ch(1).TerminalConfig = 'SingleEnded';
    ch(2).Range = [-10.0 10.0];
    ch(2).TerminalConfig = 'SingleEnded';
    ch(3).Range = [-10.0 10.0];
    ch(3).TerminalConfig = 'SingleEnded';
    ch(4).Range = [-10.0 10.0];
    ch(4).TerminalConfig = 'SingleEnded';
  3. Set the session rate and duration of acquisition.

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

    [data,timestamps] = startForeground(s);
  5. Save the acquired data to a MAT-file.

    fileName = 'data.mat';
  6. Load data from the file into the MATLAB workspace.

    savedData = load('data.mat');

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');
  2. Configure both channels to accept input signals in the range from -10 volts to 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. Set the range from -10 volts 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 sample rate to 800,000.

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

    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');
  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;
    ans =
  3. Increase NotifyWhenDataAvailableExceeds to 160,000.

    s.NotifyWhenDataAvailableExceeds = 160000;

Use Timeout to Block MATLAB While an Operation Completes

Legacy Interface

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

    ao = analogoutput('nidaq','Dev1');
    ch = addchannel(ao,1);
    ao.SampleRate =  8000;
    ao.TriggerType = 'Manual';
    ao.RepeatOutput = 0;
  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.


Session-Based Interface

Background operations run without interrupting the MATLAB Command Window. You can 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');
  2. Set the session rate to 8000.

    s.Rate = 8000
  3. Queue some output data.

  4. Start the acquisition and issue a wait to block MATLAB. If the operation does not complete in 2 seconds, a timeout occurs.


Count Pulses

You can count pulses to clock your data acquisition.

Legacy Interface

You cannot use counter input and output channels with 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 gives you results similar to using a counter input channel.

ai = analoginput('nidaq');
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.

c = inputSingleScan(s);