How can I read data from a buffer at certain intervals (ie. every 20 ms)?

2 ビュー (過去 30 日間)
David
David 2013 年 7 月 17 日
Hi, I am trying to read data from a buffer every 20 ms. I would like the data to continue to fill up the buffer while I fread it at 20 ms intervals. I used the asynchrnous command for this. However, I am not getting the correct time for data being read from the buffer. I used timerPeriod equal to .02 and had a callback function to an fread. However, the data is not being read at 20ms. (I used tic toc to try to measure times) My reason for doing all this is to plot visually data that is being logged such that the plot updates itself every 20ms.
Attached is my code.
function [x,meas,Bytes,lengthRead]=myfunc()
DATA_LOG_TIME_SEC = 1; s = serial(COM_PORT); s.InputBufferSize = 1000000; s.BaudRate = BAUD_RATE; s.StopBits = 2; s.Timeout = 1.0;
s.Timeout = DATA_LOG_TIME_SEC; s.ReadAsyncMode = 'continuous' ; s.TimerPeriod = .02 ; s.TimerFcn = {'servicetimer'};
tic fopen(s); toc fprintf('INFO: Begin logging data...\n')
fclose(s) ; delete(s) ; end
function y = servicetimer(obj, event)
global x;
global i;
global meas;
global Bytes;
global lengthRead;
global EmptyCount
global nonEmptyCount
while(i<=10)
Bytes(i)= obj.BytesAvailable;
tic;
meas{i}= fread(obj);
x(i)=toc;
lengthRead(i) = length(meas);
i=i+1;
end
end

回答 (1 件)

Lokesh Ravindranathan
Lokesh Ravindranathan 2013 年 7 月 17 日
I would recommend following the approach showing the help page: http://www.mathworks.com/help/matlab/matlab_external/timerperiod.html
  2 件のコメント
David
David 2013 年 7 月 17 日
does the timeout command end the continuous asynchronous read (if it is readasyncmode = continuous and s.Timeout = .02)?
Lokesh Ravindranathan
Lokesh Ravindranathan 2013 年 7 月 23 日
Is there a particular reason why you would want to combine asyncmode and timer? Or do you want timeout when you receive no data?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by