Clear Filters
Clear Filters

Simultaneous data acquisiton from two sensors, using the session based interface and serial commands

6 views (last 30 days)
I'm trying to acquire data simultaneously from two sensors. One is connected to my PC through a NI-PCI-6220 card, the other with an RS-232 serial cable. I'm using the session based interface in the data acquisition toolbox to read data off the PCI card, and serial commands to set up and read from the second sensor. I tried to set up the PCI card to read data using a listener event and the startBackground command, before calling the serial commands. Here's the code I'm running (the collect data function is storing readings based on Chirag Gupta comments from another post)
% Create the NI object
s = daq.createSession('ni');
% Identify computer-assigned device name (d.ID)
d = daq.getDevices;
% Add input channels
% Define length of data acquisiton and sampling rate
s.DurationInSeconds = 20;
s.Rate = 1000; % default
lh = s.addlistener('DataAvailable', @collectData);
while (~s.IsDone)
% Define the serial object
sL = serial('COM1','BAUD',9600);
sL.Terminator = 13; % 'CR'
sL.FlowControl = 'software'; % Xon and Xoff
% open the serial port
if strcmpi(sL.Status,'open')
% Turn the sensor on
% start data acquisition
%read a set of data
for t = 1:1000
test{t} = fgetl(sL);
timestamp(t) = now;
% turn the sensor
% stop the measurements and delete listener
% close the serial connection
When the code executes, the serial commands execute first acquiring all of that data before the background data collection begins. How can i modify this code to acquire from both sensors at the same time? Thanks! Kara

Accepted Answer

Walter Roberson
Walter Roberson on 22 Dec 2011
myTimer = timer('TimerFcn', {@timer_callback,SerialObject}, 'Period', 1/s.Rate, 'ExecutionMode', 'fixedRate');
function timer_callback(src, evt, serialobject)
%put your serial code here
Note: Manisha did not show correct arguments for a timer callback.
When you create a callback function, the first two arguments must be a handle to the timer object and an event structure. An event structure contains two fields: Type and Data. The Type field contains a text string that identifies the type of event that caused the callback. The value of this field can be any of the following strings: 'StartFcn', 'StopFcn', 'TimerFcn', or 'ErrorFcn'. The Data field contains the time the event occurred.
In addition to these two required input arguments, your callback function can accept application-specific arguments. To receive these input arguments, you must use a cell array when specifying the name of the function as the value of a callback property. For more information, see Specifying the Value of Callback Function Properties.
You can find examples of using the DataAvailable event at
  1 Comment
Kara on 23 Dec 2011
Thanks for the great explanation. I'll start working with this and see if I can get it to work for my application.

Sign in to comment.

More Answers (1)

Manisha on 22 Dec 2011
Hi Kara,
There are multiple ways you could achieve this.
Option 1. Read the data from your sensor using serial commands when the 'DataAvailable' event is called. Basically you could put your serial sensor code in the collectData function. The DataAvailable event fires when 1/10 second worth of data is available for analysis.
Option 2: User a timer object
myTimer = timer('TimerFcn',@timer_callback, 'Period', 1/s.Rate, 'ExecutionMode', 'fixedRate');
With timer_callback function as
function timer_callback(event,string_arg)
< Put your serial code here>
Hope that helps, Manisha
  1 Comment
Kara on 22 Dec 2011
Thanks for the suggestions. I'm pretty new to callbacks though. For either of these options, how do I send an established serial object to the callback? I'd like to avoid reestablishing communication with every execution, because i really need about 20 seconds of measurements without any interruptions.
Thanks again!

Sign in to comment.


Find more on Simultaneous and Synchronized Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!