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')endend % 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