MATLAB Answers

0

GUI Serial Communication Edittext

Asked by Tobias Wzl on 20 Oct 2017
Latest activity Commented on by 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
on 20 Oct 2017
Tobias - where in your code do you try to open the connection to the arduino? I would have expected to see something like from connect to Arduino hardware.
To update your edit text with the port name, you would do something like
portname = 'some port name';
set(handles.text1, 'String', portname);
where portname is the variable with the COM-port (?) and text1 is the control whose text you want to update with the port.
so if I put a=arduino() before my own code it should work ? or is there more I have to add or change?
by variable is meant that, I'll probably will not always plug my Arduino into the same USB-Port.
" where portname is the variable with the COM-port (?) and text1 is the control whose text you want to update with the port"
Sorry but I don't understand what you are trying to say or to ask.
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.

0 Answers