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

조회 수: 76 (최근 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
saleh said bouhliga 2020년 9월 20일
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