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);
댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Entering Commands에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!