gui_mainfcn(gui_State, varargin{:});

18 vues (au cours des 30 derniers jours)
Beyza
Beyza le 30 Nov 2023
Commenté : Sathvik le 7 Déc 2023
I want to extract data from Arduino, transfer it to Matlab via Bluetooth, and create an interface using GUI. But when I click any button on the GUI, I get the following error.
Error using project>connect_Callback
Connection to this device already exists in the
workspace.
See related documentation for troubleshooting steps.
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in project (line 17)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)project('connect_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
I can't figure out what exactly is the problem. Am I doing something wrong ?
Here is my code:
function varargout = project(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @project_OpeningFcn, ...
'gui_OutputFcn', @project_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% --- Executes just before project is made visible.
function project_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.TempGraph = plot(0, 0, 'r'); % Temperature graph
handles.HumGraph = plot(0, 0, 'b'); % Humidity graph
handles.connection = []; % Bluetooth connection
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = project_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in connect.
function connect_Callback(hObject, eventdata, handles)
guidata(hObject, handles); % Update handles structure
% Check if a connection already exists
if ~isempty(handles.connection) && isvalid(handles.connection)
fclose(handles.connection);
end
% Specify your Bluetooth COM port
bluetoothCOMPort = 'COM5';
% Create a Bluetooth connection
handles.connection = bluetooth('HC-05', bluetoothCOMPort);
% Check if the connection is valid
if ~isempty(handles.connection) && isvalid(handles.connection)
disp(['Bluetooth connection opened successfully on ', bluetoothCOMPort]);
else
disp(['Failed to create Bluetooth connection on ', bluetoothCOMPort]);
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in disconnect.
function disconnect_Callback(hObject, eventdata, handles)
% If a connection is open, close it
if ~isempty(handles.connection) && isvalid(handles.connection)
fclose(handles.connection);
disp('Bluetooth connection closed.');
else
disp('No active Bluetooth connection.');
end
guidata(hObject, handles);
% --- Executes on button press in getdata.
function getdata_Callback(hObject, eventdata, handles)
% If a connection exists, read the data from it
if ~isempty(handles.connection) && isvalid(handles.connection)
dataStr = fscanf(handles.connection); % Read string from connection
if isempty(dataStr)
disp('No data received from Arduino.');
return;
end
disp(['Received data: ', dataStr]);
data = strsplit(dataStr, ','); % Split the string into temperature and humidity
if numel(data) >= 2 % Check that we have enough data
% For each plot, gather the x and y data, append to the data arrays, and update the data in the plot
for plotHandle = [handles.TempGraph, handles.HumGraph]
xData = get(plotHandle, 'XData');
yData = get(plotHandle, 'YData');
xData(end+1) = xData(end) + 1; % Increment the time
yData(end+1) = str2double(data{1}); % Append the new data point into the array
set(plotHandle, 'XData', xData, 'YData', yData);
data(1) = []; % Remove the first element from data
end
disp('Data plotted successfully.');
else
disp('Insufficient data received from Arduino.');
end
else
disp('No active Bluetooth connection. Cannot get data.');
end
guidata(hObject, handles); % Update handles structure
  1 commentaire
Sathvik
Sathvik le 7 Déc 2023
Hi
The error message you have received suggests that the connection to the device already exists. Can you modify the 'connect_Callback' function as follows and check if the issue still persists?
...
function connect_Callback(hObject, eventdata, handles)
guidata(hObject, handles); % Update handles structure
% Check if a connection already exists
if ~isempty(handles.connection) && isvalid(handles.connection)
fclose(handles.connection);
handles.connection=[]; % Modification made here
end
...
Ensure that the workspace is empty before running.

Connectez-vous pour commenter.

Réponses (0)

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by