How to catch the error thrown when unable to connect to an arduino?

22 views (last 30 days)
I am trying to write some code that will scan the COM ports for an arduino, and if none are found I would like to throw my own error. So I would like to catch this error somehow: "Unable to find Arduino hardware at COM3. First argument must be a valid serial port, IP address/hostname or Bluetooth address/name."
Is there a way to do this?
This function simply returns the COM port for use in creating an arduino object.
Here is my code:
function port = ports()
%% Description
% Searches for the COM Port that the arduino is connected to.
%
%% Try Block
ports = seriallist();
for i = 1:length(ports)
try
a = arduino(ports{i});
clear a
port = ports{i};
catch
clear a
end
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 22 Aug 2019
>> try; arduino("/dev/cu.Bluetooth-Incoming-Port"); catch ME; disp('nan nan nah'); end
nan nan nah
The particular exception is 'MATLAB:hwsdk:general:invalidPort' and arduino() without any arguments can also generate 'MATLAB:hwsdk:general:boardNotDetected'

More Answers (0)

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!