Serial interrupt to automatically update edit boxes,

14 ビュー (過去 30 日間)
Corne van Zyl
Corne van Zyl 2019 年 4 月 21 日
コメント済み: Geoff Hayes 2019 年 4 月 22 日
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;

回答 (1 件)

Geoff Hayes
Geoff Hayes 2019 年 4 月 21 日
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 件のコメント
Corne van Zyl
Corne van Zyl 2019 年 4 月 22 日
Geoff, I really appreciate you taking the time to help me, thank you!
It interrupts everytime new data is recieved from the seial port and updates the my editbox! I could cry tears of joy. I've been struggling with this for days.
After taking your advice and applying it, somewhat blindly, I got no more errors but my handles structure did not update. I knew that I needed to use guidata, but did not know what parameters to use. Eventually with some luck, the last line of code fixed it.
function Connect_Callback(hObject, eventdata, handles)
instrreset
handles.s = serial('COM9');
handles.s.BytesAvailableFcnCount = 1;
handles.s.BytesAvailableFcn = {@Serial_int_Fcn,handles.figure1}
set(handles.s,'BaudRate',115200);
fopen(handles.s);
handles.i = 1;
handles.output = hObject;
guidata(hObject, handles);
function Serial_int_Fcn(hObject, eventdata, GUI) %GUI is the name of my GUI.
end
handles = guidata(GUI);
Serial_in = strsplit(fscanf(handles.s),';')
Voltage(handles.i) = str2double(Serial_in(1,3))
set(handles.AmbientT,'String',Voltage(handles.i))
handles.i = handles.i+1
guidata(GUI,handles);
Geoff Hayes
Geoff Hayes 2019 年 4 月 22 日
glad that you got it working!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by