File Exchange

image thumbnail

Get Available COM PORT

version 1.0 (1.05 KB) by

Get available COM port on your PC

14 Ratings



No License

This function output a cell array with string name of each COM port available on your PC.

COM_Port = getAvailableComPort();


Comments and Ratings (17)

Troy Lavigne

Pasc Peli

Pasc Peli (view profile)

Works really well on windows. Thank you!

Thanks Daniel Lavoie

Thanks Daniel Lavoie


saim (view profile)

Nikola Stan

Thanks for this code, it works very well. I adapted it to work for mac and PC,

just change the code :

%Start of the COM ailable port
lIndex1 = findstr(lErrMsg,'COM');
%End of COM available port
lIndex2 = findstr(lErrMsg,'Use')-3;

into :

keyword_start='Available ports: ';
keyword_end='Use ';
%Start of the COM available port
lIndex1 = findstr(lErrMsg,keyword_start)+length(keyword_start);
%End of COM available port
lIndex2 = findstr(lErrMsg,keyword_end)-3;

it includes the Bluetooth port in the list

Shijin Thomas

Thanks Daniel Lavoie

Josiah Renfree

Josiah Renfree (view profile)

Did exactly what I was looking for. However, I ended up modifying the code quite a bit, as parsing the error string for the COM ports can more easily be done using regexp.


Vlad (view profile)

Good job! Was looking for a function like this for some time :-) Thanks

Good job, man. It really helps in real-time case.

Nikolay S.

Nikolay S. (view profile)

Excellent- works like magic, thanks!

Very handy utility, but unfortunately the way MATLAB finds these serial ports does not take into account if other programs are using them - although fopen will of course prevent you from using that port. Also, when I incorporated it into a GUI, it did not stand up to being run a second time - it could not access the error message which instead replies with a 0x1 structure.

Marko Antila

Nice work Daniel - clever to catch the error message. I was thinking this myself but did not have time to work it out.

I think, however that the code could be further improved by inserting these two lines after the first "end":

This removes the unnecessary handle and does not confuse the future operations with the serial port.

anders granstroem

Shows available COM ports. When terminating a matlab program with CTRL-C. Windows 2000 thinks that the com port is occupied. Nice to se this in matlab. Can be seen by doing "mode" command at the command promt in windows as well.

MATLAB Release
MATLAB 7.0.1 (R14SP1)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video