How do I close serialport objects?

107 Ansichten (letzte 30 Tage)
MathWorks Support Team
MathWorks Support Team am 7 Mai 2021
I am replacing the "serial" function with the "serialport" function. When using the "serial" function, if the device was unplugged, I had to delete the serial object using the "delete(instrfindall)" command. However, when using the "serialport" function, there is no information on how to delete the object. How do I delete the serialport object?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 27 Jun. 2024
Bearbeitet: MathWorks Support Team am 10 Jul. 2023
instrfindall function finds only legacy serial and instrument objects (serial, visa, tcpip, etc.), and it will not work with the new serialport interface.
While currently there is no equivalent function that has the same functionality as the legacy instrfind or instrfindall, the following approaches can be used to disconnect from hardware when using the new serialport interface. Note that the example is for serialport, however the same approach should work for tcpclient, tcpserver, udpport, visadev, bluetooth.
Disconnect from a COM port when using serialport object
  • If the serialport object s exists in the base MATLAB workspace, as created from the command line or script, you can call the clear function
s = serialport("COM1",9600);
...
% communication operations
...
% Disconnect and clear the object
clear(s)
  • If the serialport object handle is stored in a property of another object, for example an app created with App Designer, you can call the delete function to disconnect from the COM port:
% 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)​​​​

Weitere Antworten (0)

Tags

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by