Warning: A Timeout Occurred Before Terminator Was Reached + Significant Time Delay

1 回表示 (過去 30 日間)
Vincent
Vincent 2013 年 11 月 29 日
回答済み: Walter Roberson 2013 年 11 月 30 日
Hey All,
I am plotting data obtained from an accelerometer (ADXL345) through serial port from an Arduino uno. I have two questions:
1) When the code fails to work, the aforementioned error appears, preventing me from plotting the results.
I looked at similar threads, but I was unable to resolve the issue - I'm hoping if I provide some more details specific to my situation, someone may be able to help me.
2) When the code does work, the plot has a significant time delay - how can I make my code as efficient as possible?
Thanks for your help!
Here's the code:
%%Plots data from serial port in real time
% variables
time = 0;
data1 = 0;
data2 = 0;
data3 = 0;
index = 0;
scrollWidth = 10;
delay = 0.005;
min = -100;
max = 100;
graph1 = plot(time, data1, 'm-');
hold on;
graph2 = plot(time, data2, 'c-');
hold on;
graph3 = plot(time, data3, 'g-');
% setup serial port
s = serial('/dev/tty.usbmodem1411');
set(s, 'InputBufferSize', 1024);
set(s, 'FlowControl', 'hardware');
set(s, 'BaudRate', 115200);
set(s, 'Parity', 'none');
set(s, 'DataBits', 8);
set(s, 'StopBit', 1);
set(s, 'Timeout', 10);
fopen(s);
% start time
tic;
while ishandle(graph1)
stream = fscanf(s, '%f %f %f')';
if(~isempty(stream))
index = index + 1;
time(index) = toc;
if (numel(stream) < 3)
continue;
else
data1(index) = stream(1, 1);
data2(index) = stream(1, 2);
data3(index) = stream(1, 3);
end
if(scrollWidth > 0)
set(graph1,'XData', time(time > time(index) - scrollWidth), 'YData', data1(time > time(index) - scrollWidth));
set(graph2,'XData', time(time > time(index) - scrollWidth), 'YData', data2(time > time(index) - scrollWidth));
set(graph3,'XData', time(time > time(index) - scrollWidth), 'YData', data3(time > time(index) - scrollWidth));
axis([time(index) - scrollWidth time(index) min max]);
else
set(graph1,'XData', time, 'YData', data1);
set(graph2, 'XData', time, 'YData', data2);
set(graph3, 'Xdata', time, 'YData', data3);
axis([0 time(index) min max]);
end
pause(delay);
end
end
fclose(s);
delete(s);

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 11 月 30 日
One bit of improvement is
selidx = time > time(index) - scrollWidth);
xtime = time(selidx);
set(graph1, 'XData', xtime, 'YData', data1(selidx));
set(graph2, 'XData', xtime, 'YData', data2(selidx));
and so on.
You might also want to change the pause(delay) to drawnow()
You should also be considering whether you really need to update the graph after every line that is successfully read in. The less often you update the graph, the more efficient the drawing will be. Well, up to 10000 points or so at least.

カテゴリ

Help Center および File ExchangeCounter and Timer Input and Output についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by