Reduce time execution in a real time serial data reading
30 次查看(过去 30 天)
显示 更早的评论
Hello everybody,
I'm carrying out an app that reads and plot data from sensors by serial Port. Data sent contains the values of 8 sensors plus power value and sample number. Size of the data can change because data of sensors values can changue as well. Data are sent every 0,5 s approximatly. I have to keep the data in a matrix (no problem), and plot in an axes.
If I just read values, time of each loop is fastier than the time that takes a new data to enter in the buffer, so I just have to wait for the new value, and then save it in a matrix. But If I try to plot the 8 values of the sensors, then the code takes longer than those half seconds, what makes that some data get missed. Which solution could I implement? I was thinking to save all the data but just plot one sensor in each iteration, so I'm plotting just one of each 8 values for each sensor. That's not important because after thousend of samples, visually it is impossible to see the difference, since the graph has the limit betwenn 0 and the n, where n can be +15000, but the importan thing which is save the data for processing after is performed.
But even with this last method, time of function is sometimes about 0,5 seconds and I get often an error in the matrix assignament because data collected from serial are no consistent. I was thinking play with the buffer because I could keep there more than one read, since the size is bigger than a packet of data.
The code without changes is the next one. Some ideas about how to get it? Thanks in advance.
function A = leerpuertoSerial(com,app)
hold(app.UIAxes,'on');
a = serial(com,'BaudRate',115200);
fopen(a);
A = zeros(10000,10);
n = 1;
i = 0;
cleanup = onCleanup(@()myCleanupFun(a));
ylim([0 800]);
while n<2000
if a.BytesAvailable > 0
i=i+1;
s =fscanf(a);
% I read the data packet which is something like this 1,20,343,234,234,234,543,543,543,343
C = textscan(s, '%d%d%d%d%d%d%d%d%d%d','delimiter',',');
A(i,:) = cell2mat(C);
%I plot just from the 3 value up to 10 th value (1 to 8 th sensor)
plot(app.UIAxes,i,A(i,3),'x-b');
plot(app.UIAxes,i,A(i,4),'x-b');
plot(app.UIAxes,i,A(i,5),'x-g');
plot(app.UIAxes,i,A(i,6),'x-k');
plot(app.UIAxes,i,A(i,7),'x-m');
plot(app.UIAxes,i,A(i,8),'x-c');
plot(app.UIAxes,i,A(i,9),'x-b');
plot(app.UIAxes,i,A(i,10),'x-r');
%Legends makes really slow the code.
%legend(app.UIAxes,'s1','s2','s3','s5','s6','s7','s8');
drawnow;
end
n = n+1;
flushinput(a);
end
hold(app.UIAxes,'off');
msgbox('Proceso Terminado');
xlswrite('OUTPUT',A);
end
function myCleanupFun(a)
fclose(a);
end
0 个评论
回答(1 个)
J. Alex Lee
2020-2-27
If instead of a loop, you use a timer (https://www.mathworks.com/help/matlab/matlab_prog/use-a-matlab-timer-object.html), you might have enough control to do what you want.
I think in general the plot is slow...if you don't need each plot to have a distinct symbol, maybe you can combine the plot into a single command. You might also save time by plotting once, and updating the XData and YData.
Another problem is that A seems to grow on every iteration, and this can slow down code especially if A is really large.
Isn't it redundant to use fscanf together with textscan? I don't know if it will affect execution time significantly, but how about fileread() instead of fscanf?
I think a better overall approach would be to decouple your data collector and displayer.
4 个评论
J. Alex Lee
2020-2-28
Triggering your save+display routine with the event supplied by the serial() object seems like a good idea, but it's not clear to me what you're asking with this new scheme.
I would still think you want to trigger only data saving when data is available. You can have that data saving function trigger another event to start the display function (which hopefully my above comment can help with), and apply some interruption logic to that so you can skip displays or whatever.
If you anticipate really going into the tens of thousands of data points, you might consider resampling the data for the display to ensure that you never have more than some number of data points displayed in the screen.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!