Can I read data from a serial port and a data acquisition board simulataneously using the Data Acquisition Toolbox 2.14 (R2009a)?

3 views (last 30 days)
I have two separate devices: one connected to the serial port and the other is a National Instruments data acquisition board. I would like to know if it is possible for me to read data from both devices simultaneously.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Nov 2016
It is possible to read data simultaneously from a serial port and a data acquisition board using the Data Acquisition Toolbox 2.14 (R2009a). The "StartFcn" property of the ANALOGINPUT object can be used to initiate reading from the SERIAL port. The code snippet below creates an ANALOGINPUT object and performs the data acquisition:
 
%Create and open serial object
s = serial('COM1','OutputBufferSize',4096)
fopen(s)
% Create analoginput object
ai = analoginput('nidaq','Dev1');
set(ai,'StartFcn',{@myserial,s});
addchannel(ai,0,{'chan1'})
% Start reading data from analoginput
start(ai)
% Retrieve data from analoginput object
getdata(ai)
The Callback function MYSERIAL, used with the StartFcn event in the code above, is shown below and contains the MATLAB code to read data from the serial port.
 
function myserial(obj, event, s)
%Read data from Serial. This will vary on how you want to retrieve the data
serialdata = fread(s,1)
set(obj,'UserData',serialdata)
For more information about events and callback functions, please check the following link:

More Answers (0)

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2009a

Community Treasure Hunt

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

Start Hunting!