Arduino and Matlab Serial communication is slow.
8 次查看(过去 30 天)
显示 更早的评论
I am sending 10k data from MKR Zero Arduino to MATLAB with serial communcation. The data is finished sending from the Arduino in 3 seconds but the Matlab connot catch up the speed and take more than 5 minutes. Besides, I am plotting the data in GUI and my following codes are below:
I am using fscanf to get the data and define the array before the loop
function testingbtn_Callback(hObject, eventdata, handles)
Y = zeros(10000,1); T = zeros(10000,1);
global X;
%Check COM9's availabillity
ports = serialportlist;
%Check if the Arduino is connected
if ( (logical(nnz(ports == "COM9"))) && (~isempty(ports)) )
if (string(X.status) == "open")
%Send '0' as string to Arduino
fprintf(X,'3');
%Print log
prevlog = get(handles.logtext, 'string');
if (size(prevlog,1)==10)
prevlog(1,:)=[];
end
log = [prevlog; ": MODE = TESTING!"];
set(handles.logtext,'string', log);
%Change button colour
set(handles.testingbtn,'BackgroundColor','green','enable','on');
index = 1;
while get( hObject, 'value')
output = fscanf(X,'%f');
T(index) = index;
Y(index) = output;
plot(handles.Graph1, T, Y);
index = index + 1;
drawnow
end
elseif (string(X.status) == "closed")
%Print log
prevlog = get(handles.logtext, 'string');
if (size(prevlog,1)==10)
prevlog(1,:)=[];
end
log = [prevlog; ": TURN ON STATUS, PRESS CONNECT BUTTON"];
set(handles.logtext,'string', log);
else
fprintf(X.status);
end
else
%If arduino is disconnected
prevlog = get(handles.logtext, 'string');
if (size(prevlog,1)==10)
prevlog(1,:)=[];
end
log = [prevlog; ": FAILED! ARDUINO DC!"];
set(handles.logtext,'string', log);
end
By the way, the type of data I am sending is numerical as below:
676
155
-327
-826
-871
-811
-718
-502
8
319
304
276
7
-170
-376
-475
-584
-577
-327
-240
-62
0 个评论
回答(1 个)
Shreshth
2024-5-15
Hello Xing,
The issue that you are facing is related to how MATLAB handles real-time data plotting within a loop, especially when using ‘fscanf’ to read data from a serial port and plotting each point individually. The ‘drawnow’ command, although necessary for updating plots in real-time, can significantly slow down execution when called repeatedly in a tight loop.
Here are some methods that you can apply to improve the time for the process:
1. Reduce the Frequency of Plot Updates
chunkSize = 100; % Adjust the chunk size as needed
while get(hObject, 'value')
if index <= 10000
output = fscanf(X,'%f');
T(index) = index;
Y(index) = output;
if mod(index, chunkSize) == 0 || index == 10000
plot(handles.Graph1, T(1:index), Y(1:index));
drawnow
end
index = index + 1;
end
end
Instead of plotting every single data point as it's read, consider plotting the data in chunks. This reduces the number of times the plot is refreshed.
2. Increase the Serial Buffer Size
MATLAB's serial port object has a property called ‘InputBufferSize’ that determines how much data it can hold in the buffer. Increasing this size can help MATLAB handle large bursts of data more efficiently. You can set this before opening the serial port connection.
3. Use read Instead of fscanf (MATLAB R2019a Onward)
If you are using MATLAB R2019a or newer, consider using the ‘read’ function for serial port objects created with ‘serialport’ instead of ‘fscanf’. The ‘read’ function can be more efficient and gives you more control over the number of bytes you read at a time.
You can also refer to this documentation from MathWorks about writing and reading Serial port data:
Hope it helps.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!