MATLAB Answers

0

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

Asked by August Mendelsohn on 22 Aug 2019
Latest activity Answered by Walter Roberson
on 22 Aug 2019
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

  0 Comments

Sign in to comment.

Tags

1 Answer

Answer by Walter Roberson
on 22 Aug 2019
 Accepted Answer

>> 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'

  0 Comments

Sign in to comment.