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

5 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 22 Dez. 2011
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
  1 Kommentar
Kara
Kara am 23 Dez. 2011
Walter,
Thanks for the great explanation. I'll start working with this and see if I can get it to work for my application.
Thanks!
Kara

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Manisha
Manisha am 22 Dez. 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>
end
Hope that helps, Manisha
  1 Kommentar
Kara
Kara am 22 Dez. 2011
Manisha,
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!
Kara

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by