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

 採用された回答

Walter Roberson
Walter Roberson 2016 年 1 月 9 日

0 投票

In your situation I would tend towards using nested functions with a variable shared between them.

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeEnvironment and Settings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by