关于matlab和arduino的通信,matlab有一个专门可以支持arduino的工具包。
https://cn.mathworks.com/hardware-support/arduino-matlab.html
- 获取工具包
下载后是arduinoio.mlpkginstall文件。
该文件不能直接打开。使用需按以下步骤。
1,
2,
3,
4,
5,
但是需要在matlab注册,因此一般用网上提供的下载好的工具包,这里我也给出网盘链接。
链接:http://pan.baidu.com/s/1kUY6pAR 密码:wyjy
使用工具包
解压,解压ArduinoIO的压缩文件。
打开matlab,将工作路径至于ArduinoIO文件夹下。matlab与arduino通信
通信媒介:ArduinoIO\ArduinoIO\pde\adioes.pde文件- matlab直接控制arduino:
如果使用matlab给arduino编程,就无需使用arduino IDE对arduino编程了。只需烧录adioes.pde文件到arduino中,让他和matlab进行通信。
example:
global a;
a=arduino('COM3');
a.pinMode(3,'output');
a.pinMode(5,'input');
a.digitalWrite(3,1);
a.digitalRead(5);
delete(instrfind({'Port'},{'COM3'}));
http://wenku.baidu.com/link?url=_Dr82cIwLbCHlkWgf4H4-hh46ThSvgJs-Usu5WaBrdOjbxnEp89qWkUDZ3u3vW0JcaTc5lVbHaCgK40cc0oC1QV8xen6MwKkIRRK8V-zjQS
http://www.360doc.com/content/16/0118/20/9200790_528915932.shtml
matlab通过串口与arduino通信:
-烧写arduino程序
首先给arduino编好了控制程序,并且有读取串口数据的代码
Serial.read(); //读取数据后,会将该数据从接收缓冲区移除
Serial.peek(); //不会移除接收缓冲区中的数据-给arduino烧写adioes.pde
-matlab端程序通过串口读写arduino数据
s = serial('COM3'); %定义串口对象
set(s,'BaudRate',9600); %设置波特率s
fopen(s); %打开串口对象s
pause(3); %打开串口通常需要一点时间,延时3秒
fprintf(s,'1'); %给串口写‘1’
fclose(a);
delete(instrfind({'Port'},{'COM3'}));