MATLAB Answers

GUI Serial Communication Edittext

12 views (last 30 days)
Tobias Wzl
Tobias Wzl on 20 Oct 2017
Commented: Geoff Hayes on 20 Oct 2017
Hello everyone,
I have the following Code in my GUI and I'm hoping for some help:
% --- 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)
%suchen der verfügbaren Ports,...
%rausfinden in for-Schleife welches Port...
%die geüwnschte Eigenschaft (offen) besitzt...
%und verbinde Arduino mit diesem Port
handles =guidata (hObject);
h=instrfindall;
for i= 1:length(h)
fopen('COM'+string(i))
end
h=instrfindall;
str=string(h.name(find(cell2mat(h.BytesAvailable))))
fclose('all');
fopen(str);
% --- Executes on button press in Disconnect.
function Disconnect_Callback(hObject, eventdata, handles)
% hObject handle to Disconnect (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
fclose('all');
function Portname_Callback(hObject, eventdata, handles)
% hObject handle to Portname (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Portname as text
% str2double(get(hObject,'String')) returns contents of Portname as a double
% --- Executes during object creation, after setting all properties.
function Portname_CreateFcn(hObject, eventdata, handles)
% hObject handle to Portname (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function Connection_Status_Callback(hObject, eventdata, handles)
% hObject handle to Connection_Status (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Connection_Status as text
% str2double(get(hObject,'String')) returns contents of Connection_Status as a double
% --- Executes during object creation, after setting all properties.
function Connection_Status_CreateFcn(hObject, eventdata, handles)
% hObject handle to Connection_Status (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
If I press the connect button I want to connect my Arduino to a COM-Port ( hope that my code is right referring to that)
In the next step I want to display the Port-Name my ARduino is connected to in the Edittext (Portname), do I have to use a handles-command, because if I straight use display(str) there is no name displayed in my edittext.
To stop the connection I simply used the command fclose('all') hope this is right, if not please correct me.
  3 Comments
Geoff Hayes
Geoff Hayes on 20 Oct 2017
Tobias - you will need to read the info on connecting to the Arduino (from the above link) to see what you will need to connect to this device. Or watch this YouTube post at How to Interface Arduino with MATLAB.
How are you determining the port name by which you will connect to the Arduino? However that is, let's call it the local variable portname, it will be a string that you can then populate in your text control as shown above.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by