Display continuous stream of serial data in GUI text fields with callback function
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponse acceptée
Jan
le 27 Jan 2021
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 commentaire
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Entering Commands dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!