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

조회 수: 21 (최근 30일)
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

채택된 답변

Walter Roberson
Walter Roberson 2019년 8월 22일
>> 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개)

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by