Arduino and Matlab Serial communication is slow.

4 Ansichten (letzte 30 Tage)
Xing Yik Wong
Xing Yik Wong am 26 Jan. 2021
Bearbeitet: Xing Yik Wong am 26 Jan. 2021
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

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by