serialport オブジェクトはどのように閉じますか?

10 views (last 30 days)
MathWorks Support Team
MathWorks Support Team on 29 Sep 2025

"serial" 関数を "serialport" 関数に置き換えています。"serial" 関数を使用していたときは、デバイスが抜かれた場合に "delete(instrfindall)" コマンドで serial オブジェクトを削除する必要がありました。しかし、"serialport" 関数を使用する場合、オブジェクトの削除方法について情報がありません。serialport オブジェクトはどのように削除しますか?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 29 Sep 2025
instrfindall 関数は、レガシーの serial や instrument オブジェクト(serial、visa、tcpip など)のみを検索し、新しい serialport インターフェースでは機能しません。
現時点では、レガシーの instrfind や instrfindall と同等の機能を持つ関数はありませんが、新しい serialport インターフェース使用時にハードウェアから切断するには、以下の方法が利用できます。例は serialport 向けですが、同様の方法は tcpclient、tcpserver、udpport、visadev、bluetooth にも有効です。
serialport オブジェクト使用時に COM ポートから切断する
serialport オブジェクト s がコマンドラインやスクリプトから作成され、ベースの MATLAB ワークスペースに存在する場合、clear 関数を呼び出せます。
s = serialport("COM1",9600);
...
% communication operations
...
% Disconnect and clear the object
clear(s)
serialport オブジェクトのハンドルが、たとえば App Designer で作成したアプリなど、別のオブジェクトのプロパティに保存されている場合、delete 関数を呼び出して COM ポートから切断できます。
% serialport object is created in app code
s = serialport("COM1",9600);
app.Connection = s;
...
...
% To disconnect, you can call delete in app code, for example from a button callback function
delete(app.Connection)​​​​

More Answers (0)

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!