How do I close the port Arduino is connected to?

31 ビュー (過去 30 日間)
Mathews John
Mathews John 2015 年 7 月 15 日
編集済み: Walter Roberson 2021 年 2 月 8 日
I have developed a GUI which uses the Arduino support package. Every time I close and rerun the program an error pops up saying the port is already being used by MATLAB. I clear all the variables and can rerun the program without any issues. I wanted to know if there was a way to close the connection with the arduino when the figure is closed.
I already tried to delete the arduino object but that did not work. I also tried to clear the object which also oddly did not work. Can someone help?
  2 件のコメント
Adithya Vivek
Adithya Vivek 2017 年 7 月 24 日
編集済み: Walter Roberson 2021 年 2 月 8 日
Dear Mathews, I apologize for not being able to answer your question, but could you please help with a similar problem I have? I am trying to create a way to autodetect the com port the arduino is connected to such that I can create an application usable on any Windows computer. I created a link for this question here: https://www.mathworks.com/matlabcentral/answers/349958-auto-detection-of-the-com-port-that-arduino-is-connected-to-then-defined-as-an-argument-that-can-be
I would be infinitely grateful!
PAUL ANGARITA
PAUL ANGARITA 2021 年 2 月 8 日
編集済み: PAUL ANGARITA 2021 年 2 月 8 日
I will give my contribution. Sorry for late reply. I recommend using the Command
.... clear... .
Remember it cleans all Variables in WORKSPACE. If you need to delete only the object ...a = arduino().. so only delete ...a
Remember to close or delete all objects regarding arduino stuff, firstly.

サインインしてコメントする。

回答 (3 件)

Mathews John
Mathews John 2015 年 7 月 17 日
I think I figured it out.
a = arduino(); %Creates an arduino object and connects to the hardware.
% Do stuff while open
fclose( serial(a.Port) ); %Create a serial object with the port Arduino is connected to it and close it
clear a; %Remove the variable
Don't know if there's a better way
  2 件のコメント
Image Analyst
Image Analyst 2015 年 7 月 18 日
Not that I know of. It's only a line of code so I don't know how it could be simpler or better. glad my suggestion of fclose() worked for you.
Bill Silk
Bill Silk 2016 年 12 月 27 日
Worked for me -thank you.

サインインしてコメントする。


Azrg
Azrg 2019 年 10 月 23 日
Whenever I get the message that a Arduino is already used when it is not, I use these two functions:
fclose(instrfindall);
delete(instrfindall);
Works for me :) hope it helps someone else who has this issue

Image Analyst
Image Analyst 2015 年 7 月 15 日
Perhaps call fclose():
s = serial('COM1');
fopen(s)
fprintf(s, '*IDN?')
idn = fscanf(s);
fclose(s)
  1 件のコメント
Mathews John
Mathews John 2015 年 7 月 17 日
Sorry for the late reply. I just tried this and it did not work.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeArduino Hardware についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by