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

backgroundData Acquisition Toolboxdata acqusitionlistener eventserialsession based interfacesimultaneous

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
s.addAnalogInputChannel(d.ID,3,'Voltage');
% Define length of data acquisiton and sampling rate
s.DurationInSeconds = 20;
s.Rate = 1000; % default
lh = s.addlistener('DataAvailable', @collectData);
s.startBackground();
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
fopen(sL)
if strcmpi(sL.Status,'open')
% Turn the sensor on
fprintf(sL,'LO');
% start data acquisition
fprintf(sL,'DX');
%read a set of data
for t = 1:1000
test{t} = fgetl(sL);
timestamp(t) = now;
end
% turn the sensor
fprintf(sL,'LF')
end
end
% stop the measurements and delete listener
s.stop();
delete(lh);
% close the serial connection
fclose(sL)
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

Best Answer

myTimer = timer('TimerFcn', {@timer_callback,SerialObject}, 'Period', 1/s.Rate, 'ExecutionMode', 'fixedRate');
function timer_callback(src, evt, serialobject)
%put your serial code here
end
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 http://www.mathworks.com/help/toolbox/daq/ref/notifywhendataavailableexceeds.html
Related Question