我想在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
如何解决此错误?
答案 0 :(得分:0)
答案 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.