Display continuous stream of serial data in GUI text fields with callback function

조회 수: 2 (최근 30일)
My main problem with this code is that the textfields of my GUI don't get updated. I tried using guidata(hObject, handles) at the end of my callback function to update the GUI data but that doesn't seem to work. The handles inside the callback function get updated properly but they are not sent back to the guidata and the text fields don't get updated.
% --- Executes on button press in startCom.
function startCom_Callback(hObject, eventdata, handles)
% hObject handle to startCom (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% This is the callback of the button, which starts the external function.
s = serialport("COM1",9600);
configureTerminator(s,"CR/LF")
writeline(s,"QS")
a=readline(s);
handles = guidata(hObject);
configureCallback(s, "terminator", @(src, evt) displaySerial(src, evt, hObject, handles))
handles = guidata(hObject);
guidata(hObject, handles);
function displaySerial(src, evt, hObject, handles)
handles = guidata(hObject);
data = readline(src);
src.UserData = data;
a = strrep(data,',',' ');
C = string(strsplit(a));
a=str2double(C);
set(handles.fx_output, 'String', a(1,2)/2);
set(handles.fy_output, 'String', a(1,3)/2);
set(handles.fz_output, 'String', a(1,4)/2);
set(handles.mx_output, 'String', a(1,5)/2);
set(handles.my_output, 'String', a(1,6)/2);
set(handles.mz_output, 'String', a(1,7)/2);
set(handles.error_output, 'String', a(1,1)/2);
guidata(hObject, handles);

채택된 답변

Jan
Jan 2021년 1월 27일
Remember to insert a drawnow to let Matlab update the contents of the figure. This is most likely the solution of your problem. But some hints in addition:
Insert
guidata(hObject, handles);
only after the contents of the handles struct was changed. Therefore the last two lines of startCom_Callback() can be removed.
You do not have to provide the handles struct as input, if you request its current values also. Therefore this is enough:
configureCallback(s, "terminator", @(src, evt) displaySerial(src, evt, hObject))
and
function displaySerial(src, evt, hObject)
handles = guidata(hObject);
...
The lines:
a = strrep(data,',',' ');
C = string(strsplit(a));
a=str2double(C);
can be simplified to:
a = sscanf('%g,', data, [1, inf]);
Again, because the handles struct has not been changed, there is no reason to update its value in the figure. So omit "guidata(hObject, handles);" here.
  댓글 수: 1
Moritz
Moritz 2021년 1월 27일
편집: Moritz 2021년 1월 27일
Thanks again for all your help! I put drawnow at the end of the callback but weirdly enough it only works if I add one line of code (doesn't even matter what) after
configureCallback(s, "terminator", @(src, evt) displaySerial(src, evt, hObject))
and set a debug point there. Then my GUI gets updated properly, otherwise nothing (visible) happens.
I also tried putting drawnow at the end of function startCom_Callback(hObject, eventdata, handles) but that only updates the GUI once. Any ideas what the problem might be?
Edit: Still not really an idea why this happens but i just added pause after
configureCallback(s, "terminator", @(src, evt) displaySerial(src, evt, hObject))
Not the best solution but it somehow works.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by