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

132 次查看(过去 30 天)
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)

回答(5 个)

mohammad hosin golshadi
Hi,your ans:
type The following sentence in top of your code in malab
delete(instrfind({'Port'},{'COM3'}));
I hope the problem is solved!

Roshni Garnayak
Roshni Garnayak 2019-11-5
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);
  5 个评论
Mohamed Zineldin
Mohamed Zineldin 2022-11-20
@li I had the same error, but the problem was that when I checked my Device Manager >>Ports(COM&LPT) I found my USB-ttl which is (Prolific USB-to-Serial Comm Port ) has this message: (resolve pl2303hxa phased out since 2012. please contact your supplier) , therefore I followed the steps of this video : https://www.youtube.com/watch?v=m1RTEAo6jRI&t=16s
Downloaded the Prolific PL2303 driver v3.3.2.102
And this solved my problem ,

请先登录,再进行评论。


Harshita Pandey
Harshita Pandey 2020-12-3
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.

Laura Sandoval
Laura Sandoval 2021-7-21
Hello, were you able to figure this out? I am having the same issue. Thanks in advance
  1 个评论
Walter Roberson
Walter Roberson 2021-7-21
fopen() of a COM port will fail if you already have it open in MATLAB (because your code did not close it, or your code stopped running before it reached the close)

请先登录,再进行评论。


pourya rashidi
pourya rashidi 2022-6-3
add
delete(instrfind({'Port'},{'COM6'}));
top of your code

类别

Help CenterFile Exchange 中查找有关 Data Import and Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by