从arduino发送数据到matlab时出错

时间:2017-05-16 06:09:01

标签: matlab arduino

我想在arduino中运行简单示例并将数据从arduino发送到matlab以绘制图表,

我的arduino代码如下:

#include <SoftwareSerial.h>

int i=0;
void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
   Serial.println(i);
   i++; 
}

我的matlab代码如下:

arduino=serial('COM4','BaudRate',9600);
fopen(arduino)
x=linspace(1,100);
for i=1:length(x)
    y(i)=instrfind(arduino);
    y(i)=fscanf(arduino,'%d');
end
fclose(arduino);
disp('making plot..')
plot(x,y);

但我遇到了错误!以下是matlab错误:

Error using serial/fopen (line 72)
Open failed: Port: COM4 is not available. No ports
are available.
Use INSTRFIND to determine if other instrument
objects are connected to the requested device.

Error in kh (line 2)
fopen(Arduino)

这是arduino错误:

avrdude: ser_open(): can't open device "\\.\COM4": Access is denied.

安装了设备管理器中的我的arduino uno(COM4)驱动程序。 我使用matlab 2013a和Arduino 1.8.1 Genuino

如何解决此错误?

2 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法。

在设备管理器中,您将找到Arduino COM端口。

enter image description here

转到端口的高级属性

enter image description here

将COM端口号设置为COM4

enter image description here

然后重新插入USB。

答案 1 :(得分:0)

If you are using RS-232 for Arduino IDE/Monitor to communicate with your Arduino on COM4.

Since RS-232 is a point to point methodology, you can only have one program/device on each end unless you add a bunch of hijinks.

When you have the Arduino IDE/Monitor open it uses the com port and other programs are denied access.

I think you need to close the Arduino monitor program to release Com port 4 and then the Matlab program should be able to open the port.

PS : Test this using a terminal. Obviously the port needs to be open for the terminal to receive the arduino's prints. The terminal DOES open when you have the arduino ide open, but it DOES NOT open when the arduino's COM terminal is open. So theoretically, MATLAB shouldn't be having a problem when just the IDE is open.