Transition Your Code to Session-Based Interface This topic helps you transition your code from the legacy interface to the session-based interface.

Texto completo

(1)

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 Command Session-Based Command

Find supported hardware available to your system.

daqhwinfo daq.getDevices

Registered DAQ adaptor.

daqregister You do not need to register an adaptor if you are using session-based interface.

Reset MATLAB to initial state.

daqreset daqreset

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)

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 channels

You 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 Data and Generate Signals at the Same Time

This example shows how to use analog input channels and analog output channels together with a single

Session object running near simultaneous.

(2)

Create a Session Object

Use daq.createSession to create a session.

s = daq.createSession('ni') s =

Data acquisition session using National Instruments hardware:

Will run for 1 second (1000 scans) at 1000 scans/second.

No channels have been added.

Setup Hardware

For the purpose of this example use a compactDAQ chassis NI c9172 with NI 9205 (cDAQ1Mod1 - 4 analog input channels) module and NI 9263 (CDAQ1Mod2 - 4 analog output channels) module. Use the daq.getDevices function to get more information about connected hardware. For more information see the Discovering Available Devices example.

In this example the analog output channels are connected back to the analog input channels so that the acquired data is same as the data generated from the analog output channel.

Adding Analog Input Channel

Use the addAnalogInputChannel command to add an analog input voltage channel to the session object for acquisition.

addAnalogInputChannel(s,'cDAQ1Mod1',0,'Voltage') ans =

Data acquisition session using National Instruments hardware:

Will run for 1 second (1000 scans) at 1000 scans/second.

Number of channels: 1

index Type Device Channel MeasurementType Range Name --- ---- --- --- --- --- ---- 1 ai cDAQ1Mod1 ai0 Voltage (Diff) -10 to +10 Volts

Adding Analog Output Channel

Use the addAnalogOutputChannel command is used to add an analog output voltage channel to the session for sending data.

The Session objects can contain channels of different types in the same object and can be used to start acquisition and generate signals at the same time.

addAnalogOutputChannel(s,'cDAQ1Mod2',0,'Voltage')

ans =

Data acquisition session using National Instruments hardware:

No data queued. Will run at 1000 scans/second.

Number of channels: 2

(3)

index Type Device Channel MeasurementType Range Name --- ---- --- --- --- --- ---- 1 ai cDAQ1Mod1 ai0 Voltage (Diff) -10 to +10 Volts 2 ao cDAQ1Mod2 ao0 Voltage (Diff) -10 to +10 Volts

Queuing Output Data for the Analog Output Channel

The analog output channel you just added shows that no data is queued for the object. This is because analog output channels require data to be queued before the operation is started.

A sine wave signal is generated and queued. Note that for each analog output channel the data is in a separate column.

output_data = sin(linspace(0,2*pi,1000)');

Use the queueOutputData command to queue the data.

queueOutputData(s,output_data)

Plot the output data.

plot(output_data);

title('Output Data Queued');

Start Acquisition and Plot Data

Once the data is queued, you can start operations in the foreground or background. Foreground operations block MATLAB® until the operations are complete and data is returned. Background operations are non blocking and return control to MATLAB command line processing immediately, allowing other MATLAB code to run in parallel.

Use the startForeground command to start operations in the foreground. Both generation of signal

(analog output) and acquiring of signal (analog input) are synchronized and start together. You can see this

from the acquired data being the same as the generated data as the analog output channels are connected

back to the analog input channels.

(4)

[captured_data,time] = s.startForeground();

Plot the acquired data acquired and see that it is exactly the same as generated data.

plot(time,captured_data);

ylabel('Voltage');

xlabel('Time');

title('Acquired Signal');

Change the Length of Operation

The duration of acquisition depends upon the amount of data queued for the analog output channel. Since both the channels run at the same rate of 1000 scans per second, the duration of operation, after queuing 1000 data points above is 1 second as shown DurationInSeconds.

output_data = 2*sin(linspace(0,2*pi,1000)'); % Amplitude of 2V queueOutputData(s,output_data);

duration = s.DurationInSeconds duration =

1

If you queue more data, the duration of operation will change automatically.

output_data = 2*sin(linspace(2*pi,3*pi,500)');

queueOutputData(s,output_data);

duration = s.DurationInSeconds

duration = 1.5000

Use the startForeground command to start the acquisition and the generation simultaneously.

[captured_data,time] = s.startForeground();

Plot the acquired data acquired. It is exactly the same as generated data.

(5)

plot(time,captured_data);

ylabel('Voltage');

xlabel('Time');

title('Acquired Signal');

Figure

Actualización...

Related subjects :