Problem of data missing while reading data through Serial port
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am reading data in ASCII (text) format, which is coming through raspberry pi pico micro-controller. I am fascing the problem of missing of lots of sample data. I am using by default sampling rate of micr-controller 11KHz and baud rate of 115200 but when i am try to acquire even 10Hz sine wave. Data shows only 120 samples in a second for 10Hz sine signal.
My objective is to create a Real time display like a oscilloscope.
how to avoid the problem of data samples missing?
I have figured out that due to "flushinput(s)" command data samples are missing, but if i am not using the buffer clear command then any change in input reflects in recording window after a long dealy.
Also there is a warning message in command window: " Warning:- Unsuccessful read: the input buffer was filled before the terminator reach. How to avoid that as well?
Here the program:
instrreset; % reset all serial ports
clear; close all; clc;
serialPort = 'COM3';
plotTitle = 'Read Throughput';
xLabel = 'Samples'; yLabel = 'Data';
plotGrid = 'on';
min = -5; max = 5; delay = 1e-6;
baudRate = 115200;
buffersize = 1024;
%Define Function Variables
time = 0; data = 0; count = 0;
%Set up Plot
plotGraph = plot(time,data,'-r');
title(plotTitle,'FontSize',18);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
grid(plotGrid);
dialogBox = uicontrol('Style', 'PushButton', 'String', 'Break Loop','Callback', 'loop_break = false;');
loop_break = true;
%% Open Serial COM Port
s = serial(serialPort,'BaudRate', baudRate,'InputBufferSize',buffersize,'DataBits',8,'Terminator','LF');
fopen(s);
% start stopwatch timer
tic
set(s,'TimeOut', 0.01); % if no data in the buffer matlab will keep trying to read for "Timeout" seconds
%Loop when Plot is Active
while loop_break
%Read Data from Serial as Float
dat = fscanf(s,'%f');
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %stop the stopwatch and extract time
data(count) = dat(1); %Extract 1st Data Element
set(plotGraph,'XData',time,'YData',data,'LineWidth',2);
axis([0 time(count) min max]);
flushinput(s);
end
%Allow MATLAB to Update Plot
pause(delay);
end
%Close Serial COM Port and Delete useless Variables
if loop_break == 0
fclose(s);
clear count dat delay max min plotGraph plotGrid plotTitle s ...
serialPort xLabel yLabel;
end
0 commentaires
Réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!