How to close serial port in R2019b

30 ビュー (過去 30 日間)
JAVIER BURGUES CALDERON
JAVIER BURGUES CALDERON 2019 年 10 月 29 日
コメント済み: James 2023 年 10 月 12 日
Good morning,
I am porting my code to the new serialport() interface, but I have been unable to find how to close the serial port once I have finished reading from it.
What is the function to do that in R2019b?
Thanks,
Javier.

採用された回答

Walter Roberson
Walter Roberson 2019 年 10 月 29 日
The new serialport() interface does not use fopen() or fclose() . You just delete the object to close it.
I prefer the fopen() / fclose() interface myself... but then I used to write modem control programs where it was common to want to configure a port and open and close it while not deleting it.
  9 件のコメント
Walter Roberson
Walter Roberson 2021 年 6 月 11 日
/Applications/MATLAB_R2021a.app/toolbox/matlab/serialport/+internal/Serialport.m handles delete() by clearing a stored InstrumentImpl object. Presumably the delete() method of InstrumentImpl takes care of the details of disconnecting the object. However the details are inside /Applications/MATLAB_R2021a.app/toolbox/shared/instrument/+instrument/+internal/InstrumentImpl.p which we cannot read.
The stored InstrumentImpl property is inside
properties (Hidden, Access = ...
{?internal.Serialport, ?instrument.internal.ITestable})
so you cannot get at the object. Not unless perhaps struct()
Will Reeves
Will Reeves 2023 年 2 月 1 日
編集済み: Will Reeves 2023 年 2 月 1 日
within a class with a serialport assigned to a property,
delete(obj.serialPortInstance)
doesn't seem to work for me. Could you let me know the correct syntax for clean deletion from within a class?
obj.serialPortInstance=[];
doesn't work for me either...
Thanks!

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

その他の回答 (2 件)

Michael Borovik
Michael Borovik 2022 年 3 月 23 日
Create the object with comport and baudrate. This will open the port.
a = serialport(comport,baudrate);
Write an empty matrix to clear the port. This will close the port and let other processes to access it (Arduino IDE for that matter). This will work where "clear a" would not. For example a handle class that keeps "a" as one of the properties.
a = [];
  1 件のコメント
James
James 2023 年 10 月 12 日
Thanks Michael ~ that works for me !
I'm new to Matlab. Writing [ ] to the port object isn't exactly intuitive (to me anyway).

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


JAVIER BURGUES CALDERON
JAVIER BURGUES CALDERON 2019 年 10 月 29 日
Thanks Walter. I agree that the fopen/fclose interface was more intuitive.
Do you know if it is possible to delete a callback function currently associated to a serialport object, without deleting the object?
Thanks,
Javier.
  2 件のコメント
Sakari Vekki
Sakari Vekki 2020 年 9 月 11 日
Late to the party but this should work:
configureCallback(serialhandle, "off");
where serialhandle is your serialport object.
Walter Roberson
Walter Roberson 2020 年 9 月 11 日
Thanks, Sakari Vekki, I had not noticed that method before.

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

カテゴリ

Help Center および File ExchangeSerial and USB Communication についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by