Is there anyway to find a "lost" serialport?

33 Ansichten (letzte 30 Tage)
James London
James London am 8 Feb. 2024
Kommentiert: James London am 12 Feb. 2024
I have made a matlab app which uses the new serialport comand. In some rare situations the serialport object can get "lost". This can happen if part of the program crashes and the serialport object gets written over before being closed. Or if the UIObject containing the serialport as a property gets deleted due to a crash. Once the serialport object is "lost" the port cannot be reconnected to without closing out of Matlab. This is a less than ideal solution because a lot of data is sometimes loaded by this program and it can take a while to restart. I have tried to remove all bugs from my program and make sure the serialport is always closed before being opened again but there are some fringe cases that have been hard to handel. As a last resort is it possible to either find open Matlab serialport objects, or "force close" a serialport Matlab is connected to (without restarting Matlab)?

Antworten (1)

Pratyush Swain
Pratyush Swain am 9 Feb. 2024
Hi James,
Yes, it is possible to find open MATLAB serialport objects and close them without restarting MATLAB.
Here's a method to find and close any open "serialport" objects:
% Clear any existing serial connections
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
But "instrfind" function will soon be removed in future releases.Another workaround is to simply clear the serialport object from the workspace which makes the port available again.Please follow the workflow below:
Starting by inspecting the serial ports conditions:
>> availableSerialObj = serialportlist("available");
>> disp(availableSerialObj);
Lets assume serial port COM6 is available. Initiate serial port COM6:
>> serialObj = serialport("COM6",11500,"Timeout",5);
To close and re-open the serial object you must clear the variable using the "clearvars" function.
>> clearvars('serialObj');
For more information on serial port connections, please refer to https://www.mathworks.com/help/matlab/ref/serialport.html
Hope these workarounds help resolve the issue.
  3 Kommentare
Daniel
Daniel am 12 Feb. 2024
Any luck finding a solution? I am having the same issue.
James London
James London am 12 Feb. 2024
No luck so far.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by