MATLAB Answers

Problem of Data acquisition from Arduino Due in the GUI Serial communication

2 views (last 30 days)
Minhee Kim
Minhee Kim on 6 Oct 2016
Edited: Minhee Kim on 6 Oct 2016
Hello, I am currently working with Serial communication using Arduino Due.
In the arduino, when I press 1 in the serial monitor, arduino sends 16 datas such as 1111; 2222; 3333; ... 2222; 1111; 1
(last number is for checking that there is loss of data line) with 16 Hz of sampling rate.
and the Matlab GUI program scan serial data whenever data is shown in serial monitor.
When Matlab scans two times, start to process data using 2 lines and make a plot
so that actual signal sampling rate is 8 Hz.
this m file worked for 20 seconds and stopped. I displayed the data scanned but the data scanned is less than 16 numbers. It looks something happen in buffer, or unstable of Matlab code. (when working with Arduino alone, it works over 40 mins) Do you know what causes this problem?
What I did is..
1. add pause end of while loop -still stop
2. change input and outputbuffersize - standard size was 512, even I increase, it still stop
I welcome any idea from you. thank you.
I tried to upload my code,,,but it is not arranged well when I upload here.. I skipped basic GUI struncture.
if true
% code
% --- Executes on button press in CONNECT.
function CONNECT_Callback(hObject, eventdata, handles)
% hObject handle to CONNECT (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global result_850
global result_735
global HbO
global Hb
global s_time %Variable definition
if strcmp(get(hObject,'String'),'CONNECT') % currently disconnected //if you press CONNECT, the tag is printed out as String of hObject.
serList = get(handles.COM,'String');
serConn=serial(serList); serConn.BaudRate=250000;
try
fopen(serConn);
handles.serConn = serConn;
% enable Tx text field and Rx button
set(handles.START, 'Enable', 'On');
set(hObject, 'String','DISCONNECT');
set(handles.WELCOME, 'String', 'Connected');
catch e
errordlg(e.message);
end
else set(handles.START, 'Enable', 'Off'); set(hObject, 'String','CONNECT') set(handles.WELCOME, 'String', 'Disconnected'); fclose(handles.serConn); result_850= 0; result_735= 0; HbO= 0; Hb= 0; s_time= 0; end guidata(hObject, handles);
% --- Executes on button press in START. function START_Callback(hObject, eventdata, handles) % hObject handle to START (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) global result_850 global result_735 global HbO global Hb global s_time global THb
temp_data_num=1; k=1; data_850=0; L=0.28; a=1; tim=0; Fs=8; tempbase735=0; tempbase850=0; j=2; data_735=ones(16,1); data_850=zeros(16,1);
file_save = sprintf('%s.xlsx',get(handles.Filename,'String')); % file name definition according to the tag in GUI
if strcmp(get(hObject,'String'),'START') fprintf(handles.serConn, '1'); %In the serial monitor on the arduino, it's same with pressing '1' there. set(hObject, 'String','STOP') %And change the button string to 'STOP', showing it's running now. set(handles.CONNECT, 'Enable', 'Off'); %inactivate CONNECT botton, to not make mistake by pressing it. set(handles.WELCOME, 'String', 'Running'); while(strcmp(get(hObject,'String'),'STOP')) %run function below until press STOP
if rem(k,2)==1 %first line will be 735 nm
data_735=str2num(fscanf(handles.serConn)); %one row line of data is scanned, total 16 elements including time header.
leng(1)=length(data_735);
if length(data_735)~=16
pause(0.01);
data_735=str2num(fscanf(handles.serConn));
result_735=data_735(1:15);
end
result_735=data_735(1:15);
else %second, even line will be 850 nm
data_850=str2num(fscanf(handles.serConn));
% data_850
if length(data_850)~=16
pause(0.01);
data_850=str2num(fscanf(handles.serConn));
result_850=data_850(1:15);
end
result_850=data_850(1:15);
end
if rem(k,2)==0 %735, 850
%data processing here...
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by