When you create a data acquisition session, it applies to a specific vendor, and allows you to add applicable devices and channels. Discovery and configuration is part of setting up your session. This example shows a typical session setup with the demo adaptor.
To enable the demo adaptor in your installation, see the instructions in Enable the Demo Adaptor.
v = daq.getVendors
v = Number of vendors: 2 index ID Operational Comment ----- -- ----------- ------------------------ 1 ni false Click here for more info 2 mw true MathWorks Properties, Methods, Events Additional data acquisition vendors may be available as downloadable support packages. Open the Support Package Installer to install additional vendors.
d = daq.getDevices
d = Data acquisition devices: index Vendor Device ID Description ----- ------ --------- ------------------ 1 mw MWDev0 MathWorks MW314159 2 mw MWDev1 MathWorks MW314159 3 mw MWDev2 MathWorks MW628318
With a listing of available vendors and devices, you can create a session and add channels to it.
s = daq.createSession('mw')
s = Data acquisition session using MathWorks hardware: Will run for 1 second (1000 scans) at 1000 scans/second. No channels have been added. Properties, Methods, Events
To see details about one of the devices, use its index to access the array of devices.
mw: MathWorks MW314159 (Device ID: 'MWDev0') Analog input subsystem supports: 3 ranges supported Rates from 0.1 to 1000000.0 scans/sec 2 channels ('ai0','ai1') 'Voltage','Current' measurement types Analog output subsystem supports: 3 ranges supported Rates from 0.1 to 1000000.0 scans/sec 2 channels ('ao0','ao1') 'Voltage','Current' measurement types
The first and second devices are the same model, so this example
uses one for input (
'ai0' and the other for output
ch1 = addAnalogInputChannel(s,'MWDev0','ai0','voltage')
ch1 = Data acquisition analog input voltage channel 'ai0' on device 'MWDev0': Coupling: DC TerminalConfig: Differential Range: -10 to +10 Volts Name: '' ID: 'ai0' Device: [1x1 daq.sdk.DeviceInfo] MeasurementType: 'Voltage'
ch2 = addAnalogOutputChannel(s,'MWDev1','ao0','voltage')
ch2 = Data acquisition analog output voltage channel 'ao0' on device 'MWDev1': TerminalConfig: SingleEnded Range: -10 to +10 Volts Name: '' ID: 'ao0' Device: [1x1 daq.sdk.DeviceInfo] MeasurementType: 'Voltage'
View the session to see the configuration.
s = Data acquisition session using MathWorks hardware: No data queued. Will run at 1000 scans/second. Number of channels: 2 index Type Device Channel MeasurementType Range Name ----- ---- ------ ------- ------------------- ---------------- ---- 1 ai MWDev0 ai0 Voltage (Diff) -10 to +10 Volts 2 ao MWDev1 ao0 Voltage (SingleEnd) -10 to +10 Volts
The session is now ready to send and receive single scans or streams of data.
A single scan is when you send an output or read input from
the channels at one moment in time. The data transfer is handled by
the adaptor MEX layer. For the demo adaptor this is contained in the
which provides the functionality shown in the following diagram.
To generate a single scan analog output of 1.25 V, enter the following code.
DemoDriver output: 1.25
To read a single scan of analog input:
data = inputSingleScan(s)
With only one input channel, this returns only a single value.
If you add more analog input channels to the session,
a vector, with an element for each channel.
ch3 = addAnalogInputChannel(s,'MWDev2','ai0','voltage'); data = inputSingleScan(s)
data = 0 200
Remove channels from the session you no longer need.
removeChannel(s,[1 2 3]);
Streaming involves a sequence of input or output data on each channel, typically a waveform, comprised of many scans. Streaming can be accomplished in the foreground (blocking MATLAB until the stream is complete), or in the background (running asynchronously while MATLAB continues). Streaming might involve more scans or samples than the device memory can hold. For these reasons, the toolbox uses streaming channels to accommodate data flow. This allows data to be sent and received without causing a memory overflow, and without interrupting MATLAB.
Use MWDev2 to generate a 1 KHz sine wave in the background for ten seconds. The default sample rate is 1000 per second.
ch4 = addAnalogOutputChannel(s,'MWDev2','ao1','voltage')
ch4 = Data acquisition analog output voltage channel 'ao1' on device 'MWDev2': TerminalConfig: SingleEnded Range: -10 to +10 Volts Name: '' ID: 'ao1' Device: [1x1 daq.sdk.DeviceInfo] MeasurementType: 'Voltage'
Y = sin(linspace(0,2*pi*10,10000))' % 10 cycles in 10000 samples; queueOutputData(s,Y); startBackground(s) s.IsRunning
pause (10) s.IsRunning