how can I automatically connect Arduino to the correct port?

4 views (last 30 days)
Hi,
I am trying to connect an Arduino to Matlab, and then write to it. I can do this without any problems if I know the correct port in advance. However, this device will have to work on many different PC's (and in the future, cross platform) so I need to read the ports and see if an Arduino is connected.
The problem is, some computers have other devices connected to the serial port, and I have no way of knowing if the Arduino is the serial port that my code (below) found. Using get('serial port object') does not give me any special properties by which I can identify an Arduino is connected.
Yes, I know its easy to look at the device manager on Windows and see which port the Arduino is connected to, but I am trying to avoid this step for my users...
Thanks
Roee
function msp= startArduino()
% find out which port Arduino is connected to:
portNum=1;
wrongPort=1;
while wrongPort
msp = serial(['COM' num2str(portNum)]);
try
set(msp, 'BaudRate', 9600);
set(msp, 'OutputBufferSize', 7);
fopen(msp); %go to catch if can't open
wrongPort=0; % found the right one - exit while loop!
catch
delete(msp);
clear msp
instrreset % close any wrongly opened connection
wrongPort =1 ; % keep trying...
end
portNum=portNum+1;
if portNum==16
error('Arduino is not connected')
end
end
end
  2 Comments
Ryan G
Ryan G on 16 Jul 2013
You could program the arduino to periodically send a message/flag letting you know what it is. There is likely a better way, might be a better suited question for arduino forums as it's more generic to arduino.
Adithya Vivek
Adithya Vivek on 24 Jul 2017
Did you get any answer? I'm having exactly the same issue. I'm unable to make any other codes I have seen of similar functionality to work and I understand them regardless.

Sign in to comment.

Answers (1)

Dennis Kachila
Dennis Kachila on 21 Apr 2023
Edited: Dennis Kachila on 21 Apr 2023
Hello I was faced with the same exact challenge you describe and searched online for avaible solution unfortunately I didn't find the one I need .
So I decided to breakdown the problem into parts as shown in the folling steps below:
step 1: Listing all avalable ports
ports = serialportlist("all");
step 2 loop through the ports while Trying to connect to Arduino on current port
for i = 1:length(ports)
try
arduinoObject = arduino(ports(i), 'Uno');
catch ME
end
end
step 3 To verify if the board is connect to arduino use one of the arduinoObject property to check if it matches
if strcmp(arduinoObject.Board, "Uno") % You may usem arduinoObject.Board, or arduinoObject.Port, in my case i used 'Board'
% if success return the arduinoObject so that you may use it later for
% reading voltages or writing
end
Here is the COMPLETE sample code
% Get list of available COM ports
ports = serialportlist("all");
% Loop through all available ports and try to connect to an Arduino
for i = 1:length(ports)
try
% Try to connect to Arduino on current port
arduinoObject = arduino(ports(i), 'Uno'); % if The Arduino board is Nano use 'Nano3' and so on
% Check if Arduino is an Uno
if strcmp(arduinoObject.Board, "Uno")
% Store the Arduino object in the app.Arduino property to
ArduinoObj = arduinoObject;
% Display message for successful connection
disp(["Arduino connected to " + ports(i)]) ;
% Exit the loop
break;
else
% If the port is not connected to an Uno, close the Arduino object
clear arduinoObject;
end
catch
% If there is an error connecting to the Arduino, try the next port
disp(["Arduino port " + ports(i) + " incorrect trying the next com"]);
clear arduinoObject;
continue;
end
end
% If no Arduino was found, display error message
if isempty(arduinoObj)
disp("No Arduino found");
end
Hoping it helps.

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!