Simultaneous data acquisiton from two sensors, using the session based interface and serial commands
4 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2011-12-22
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 个)
Manisha
2011-12-22
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 National Instruments Frame Grabbers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!