Serial data lag when transmitted without delay

조회 수: 4 (최근 30일)
Markus Baecker
Markus Baecker 2019년 5월 1일
편집: Joao Ferreira 2020년 6월 12일
Hello Community!
I want to display Serial Data from an Arduino Uno Photodiode Array(16 by 1 Elements). The values are 8 bit. The arduino also turns a stepper motor after each aquisition, so you get some sort of basic computer tomography image.
Problem is, that the data should be displayed in realtime for learning purpose, but if the data is sent by the Arduino (115200 baudrate) without a delay of 5 ms after each 8 bit value, the imagesc() /drawnow() function gets delayed be approximatly 20 sec.
If a delay of 5 ms on the arduino side is added, the drawnow() function feels nearly realtime responsive. But i can't leave that delay in, because the steppermovement would get choppy.
I guess it has to do with the ineffective way of plotting on the Matlab side. Or maybe the standard serial buffer is to small (12 steps per sec*16 values of 8bit(or 1 byte)=192 bytes per second)? Could anybody suggest better practice?
Thank you,
Markus
Code:
A=zeros(16,360); %Defines 16 diodes array with 360 degrees rotation/steps
B=size(A,2);
serialPort = 'COM3'; % defines COM port of Arduino Matlab Command "seriallist" zeigt alle vorhandenen
s = serial(serialPort,'BaudRate',115200)
fopen(s);
pause(2); % Pause of 2 sec is crucial, otherwise connection is not established correctly
figure('Name','Sinogram')
fprintf(s,'%c','a'); %starting command for the image aqusition
try
for y=1:B
for x=1:16
dat = fscanf(s,'%d'); %reads serial data
A(x,y)=dat; %fills Matrix
end
disp(floor(y/B *100)+"% loaded");
drawnow('limitrate')
imagesc(A)
xlabel('Angle - \theta (degree)');
ylabel('Value - x\prime (Pixel)');
end
catch
instrreset;
end
fclose(s);
disp('Data aquiered');
colorbar;
  댓글 수: 1
Joao Ferreira
Joao Ferreira 2020년 6월 12일
편집: Joao Ferreira 2020년 6월 12일
Try adding the function:
flushinput(serialPort);
after the drawnow, so Matlab discards any data buffered..

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by