Serial interrupt to automatically update edit boxes,
13 views (last 30 days)
Show older comments
I want to design a GUI that automatically updates an edit box every time it receives new data over the serial port. I cannot figure out the proper syntax GUIDE/Matlab uses to pass arguments to functions or what arguments are necessary.
I have looked https://www.mathworks.com/matlabcentral/answers/280169-gui-serial-communication-interrupt-function but the syntax makes no sense to me and I can't find documentation that explains it past the basic user level.
% --- 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)
instrreset
handles.s = serial('COM9');
handles.s.ReadAsyncMode = 'continuous';
handles.BytesAvailableFcnMode = 'byte';
handles.s.BytesAvailableFcnCount = 1;
handles.s.BytesAvailableFcn = @Serial_int_Fcn;
set(handles.s,'BaudRate',115200);
fopen(handles.s);
function Serial_int_Fcn (hObject, eventdata, handles)
%
i = 1;
Serial_in = strsplit(fscanf(hObject),';') ? Why does this work and not handles.s as argument?
ADC_raw1(i) = str2double(Serial_in(1,1)) **Error - Not enough input arguments.
Filter_out(i) = str2double(Serial_in(1,2))
WSB_voltage(i) = str2double(Serial_in(1,3))
AmbientT(i) = str2double(Serial_in(1,4))
set(handles.Loadcell1,'String',ADC_raw1(i)) **Error - Not enough input arguments.
set(handles.Loadcell2,'String',ADC_raw1(i))
set(handles.Loadcell3,'String',ADC_raw1(i))
set(handles.TotalWeight,'String',Filter_out(i)+Filter_out(i)+Filter_out(i))
set(handles.AmbientT,'String',AmbientT(i))
set(handles.FluidT,'String',WSB_voltage(i))
i = i + 1;
0 Comments
Answers (1)
Geoff Hayes
on 21 Apr 2019
Corne - in the line of code where you assign the callback, you need to make clear that another parameter is to be pased in. This other parameter can be whatever you want, but usually for GUIs, I pass in the handle to the figure/GUI. So your callback assignment would become
handles.s.BytesAvailableFcn = {@Serial_int_Fcn, handles.figure1};
and your callback function signature would become
function Serial_int_Fcn (hObject, eventdata, hGui)
handles = guidata(hGui); % get the updated handles structure
% your code
end
The handles structure doesn't automatically get passed in as the third parameter to the callback...you need to pass this in. While we could pass in handles like
handles.s.BytesAvailableFcn = {@Serial_int_Fcn, handles;
the problem is that handles is a copy at the time you do this callback assignment and so will not have the updates that any other functions/callbacks might do against it (in case you are using it to store local data).
2 Comments
See Also
Categories
Find more on Dialog Boxes in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!