Cannot close connection. MATLAB connection to Due at COM9 exists in your workspace. To create a new connection, clear the existing object.

81 次查看(过去 30 天)
I cannot close the ports of the arduino after a GUIDE GUI program.
All the commands I have tried at the exit function are:
try
myArduinoAddonLibrary = getappdata(0,'ARD_AD');
clear myArduinoAddonLibrary;
arduinoObj = getappdata(0,'ARD');
fclose(serial(arduinoObj.Port));
delete(serial(arduinoObj.Port))
clear arduinoObj;
catch errorObj
errordlg(getReport(errorObj,'extended','hyperlinks','off'),'Arduino Error');
end
What would I be doing wrong?
The error that appears when I try to create another Arduino connection in the same port is:
MATLAB connection to Due at COM9 exists in your workspace. To create a new connection, clear the existing object
For MATLAB 2019a
  1 个评论
Jason
Jason 2019-9-17
However, I have noticed that if I click the exit task several times, the ports duplicate? What could this be?
Instrument Object Array
Index: Type: Status: Name:
1 serial closed Serial-COM9
2 serial closed Serial-COM9
3 serial closed Serial-COM9
At the end of the exit task, I now run
clear all
to make sure no variables are left in the GUI workspace. And the error has now changed to:
"Unable to create a communication link with the serial port. Please check your input argument PORT..."
However, MATLAB crashes with an internal error.

请先登录,再进行评论。

采纳的回答

Jason
Jason 2019-9-17
We solved it using this:
try
clear
myArduinoAddonLibrary = getappdata(0,'ARD_AD');
clear myArduinoAddonLibrary;
arduinoObj = getappdata(0,'ARD');
fclose(serial(arduinoObj.Port));
delete(serial(arduinoObj.Port))
clear arduinoObj;
catch errorObj
errordlg(getReport(errorObj,'extended','hyperlinks','off'),'Arduino Error');
end
%Remove appdata from memory
rmappdata(0,'ARD')
rmappdata(0,'ARD_AD')
clear all;
We just added clear at the start made sure the workspace was totally clear at the end.

更多回答(1 个)

saleh said bouhliga
In the MatLab Help Center they had nice description of how to clear a variable from memory. Check it here: https://www.mathworks.com/help/matlab/ref/clearvars.html
In your case should be: clearvars -global a;

类别

Help CenterFile Exchange 中查找有关 MATLAB Support Package for Arduino Hardware 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by