What is replacement for instrfind function?
13 views (last 30 days)
Show older comments
Hello,
I just saw that "instrfind" function will be removed in the future releases.I am using this function frequently to detect open COM ports and close them before re-running application.
eg.
out = instrfind('Port',['COM',int2str(ComNum)]);
if ~isempty(out)
fclose(out);
delete(out)
clear out
end
What procedure do you recommend to use in order to access already open COM port?
I see this step as extremeny useful, otherwise "serialport" will report an error (Unable to connect to the serialport device at port...)
Best regards,
Ivan Sojic
0 Comments
Answers (1)
Steven Lord
on 5 Oct 2022
The Compatibility Considerations section of the instrfind function documentation page gives a list of replacement functions for various uses of instrfind. Quickly skimming through the options suggests the serialport interface family of functions is the correct replacement. See this documentation page for more information.
5 Comments
dpb
on 6 Oct 2022
ADDENDUM
It would seem about the only available workaround/kludge available to the user code would be to create a global array or struct that holds the serialport object handles. While globals tend to be ugly and messy, that would at least reduce the likelihood of many of the ways one might otherwise get orphaned handles.
Of course, ideally, one doesn't ever have that occur, but "stuff" happens...
dpb
on 6 Oct 2022
Edited: dpb
on 7 Oct 2022
ADDENDUM SECOND
s=serialport
without any arguments will connect to the last cleared serialport object instance.
This gives one-step recovery, but doesn't let one retrieve status of any port by name as does instrfind
This machine has only one COM port so can't see what happens with more than one -- would a subsequent use of
s(2)=serialport;
reopen the previous second-to-last cleared serialport object instance or ...???
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!