MATLAB Answers

Error in MATLAB-Open failed: Port: COM Port is not available. No ports are available.

382 views (last 30 days)
mohan khedkar
mohan khedkar on 2 Nov 2019
Answered: Harshita Pandey on 3 Dec 2020
Hello, I am taking serially data in matlab from a hardware.First time when i run the code of taking data in matlab through COM port,it gives me appropriate data, also get appropriate plot.But when second time i run the same code in matlab,i am getting error like 'Open failed: Port: COM Port is not available. No ports are available' even port is available.Please give me any suggestion over it.My code is-
s=serial('COM3','BaudRate',9600);
fopen(s);
data=fscanf(s);
d23=str2num(data);
plot(d23)

Answers (2)

Roshni Garnayak
Roshni Garnayak on 5 Nov 2019
When a serial port connection is opened and the device becomes unavailable, the device remains unavailable after reconnecting. There is currently no way in MATLAB to fully reset the status of this connection, once this state is reached.
As a workaround you can first delete the serial port connection and then reconnect the device. For instance:
myComPort = serial('COM3');
fopen(myComPort);
% now disconnect the device
delete(myComPort)
clear myComPort
% now connect the device again, the following will now be successful:
myComPort = serial('COM3');
fopen(myComPort);
  1 Comment
Luke Middleton
Luke Middleton on 5 Mar 2020
You can use instrfind to attach a handle to all the serial ports and close them all at once:
a = instrfind();
fclose(a);

Sign in to comment.


Harshita Pandey
Harshita Pandey on 3 Dec 2020
Hello,whenever im runnning my code im getting an error given below:
Error using serial/fopen (line 72)
Open failed: Port: COM3 is not available. No ports are available.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Error in IR (line 5)
fopen(arduino);
I'am a beginner so i dont know how to exactly resolve this issue.
My code is:
clear all;
close all;
clc;
arduino = serial('COM3','BaudRate',9600);
fopen(arduino);
i=1;
while(1)
data(i) = fscanf(arduino);
plot(data);
pause(0.01);
i=i+1;
end
grid on;
fclose(arduino);
It would be really helpful if someone can help me with this.

Community Treasure Hunt

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

Start Hunting!