Serial data lag when transmitted without delay

4 vues (au cours des 30 derniers jours)
Markus Baecker
Markus Baecker le 1 Mai 2019
Modifié(e) : Joao Ferreira le 12 Juin 2020
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 commentaire
Joao Ferreira
Joao Ferreira le 12 Juin 2020
Modifié(e) : Joao Ferreira le 12 Juin 2020
Try adding the function:
flushinput(serialPort);
after the drawnow, so Matlab discards any data buffered..

Connectez-vous pour commenter.

Réponses (0)

Catégories

En savoir plus sur Arduino Hardware dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by