Make an array of incoming serial data using bytesAvailablefcn

Hi,
I am using bytesAvailablefcn to collect data from serial port. I would like to make an array of incoming data. I could achieve it using global variables. I would like to do the same thing without using global variables. I will highly appreciate if anyone can help me with this.
Thanks in advance !
global dataArray
dataArray = zeros(1,5);
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
pause(0.1)
end
s = serial('COM17');
s.DataBits = 8;
s.StopBits = 1;
s.BaudRate = 115200;
s.Parity = 'None';
s.InputBufferSize = 2^20;
s.BytesAvailableFcnMode = 'byte';
s.BytesAvailableFcnCount = 7;
s.ByteOrder = 'bigEndian';
s.BytesAvailableFcn = {@collectData};
fopen(s);
flushinput(s);
pause(10)
fclose(s);
delete(s);
-----------------------------------------------------------------------------------------
function collectData(obj,event)
global dataArray
ID = fread(obj,6,'uint8');
data = fread(obj,1,'int8');
dataArray(1) =[] ;
dataArray(end+1) = data;
end

 Accepted Answer

More Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!