0%

【Arduino学习手记】2 用VScode写Arduino程序

Arduino IDE没有补全功能,写起来实在费劲
而VScode作为万能的编辑器,当然是可以支持Arduino的

插件下载

搜索下载Arduino扩展
1.png
配置插件,在settings.json中加入如下配置

1
2
"arduino.path": "{path}\\Arduino", //{path}是你的Arduino安装路径
"arduino.logLevel": "info"

自动补全&语法检查

到这里,你已经可以编辑.ino文件了,但是有一个问题,类似于digitalWrite(3,HIGH)一类的语句会有红线告诉你存在错误,看起来很烦,我们这样配置一下用户区的settings.json

1
2
3
4
5
6
7
8
9
{
"arduino.path": "{path}\\Arduino", //{path}是你的Arduino安装路径
"C_Cpp.intelliSenseEngine": "Tag Parser",
"editor.insertSpaces": true,
"files.autoGuessEncoding": true,
"arduino.logLevel": "info",
"explorer.confirmDelete": false,
"editor.detectIndentation": false,
}

这样就解决了,但是调用函数的时候还是没有补全功能,这是因为没有加入库文件造成的
在c_cpp_properties.json的includePath中加入

1
2
3
4
5
6
7
8
"includePath": [
"{path}\\Arduino\\hardware\\arduino\\avr\\cores\\arduino",
"{path}\\Arduino\\hardware\\arduino\\avr\\variants\\eightanaloginputs",
"{path}\\Arduino\\hardware\\tools\\avr\\lib\\gcc\\avr\\7.3.0\\include",
"{path}\\Arduino\\hardware\\tools\\avr\\lib\\gcc\\avr\\7.3.0\\include-fixed",
"{path}\\Arduino\\hardware\\tools\\avr\\avr\\include",
"{path}\\Arduino\\libraries"
],

上传程序

点击[Select Board Type]
选择你的开发板型号
2.png
再点击[Select Serial Port]选择使用的串口
然后Ctrl+Alt+U即可上传

中文乱码修复

上传过程中,有可能会发现控制台中出现中文乱码,修复的方法如下
打开

C:\Users\YourUserName\%.vscode%\extensions\vsciot-vscode.vscode-arduino-0.4.0\out\src\common中的util.js

找到如下代码
1
2
3
4
5
6
7
8
9
10
11
if (os.platform() === "win32") {
try {
const chcp = child_process.execSync("chcp.com");
codepage = chcp.toString().split(":").pop().trim();
}
catch (error) {
outputChannel_1.arduinoChannel.warning(`Defaulting to code page 850 because chcp.com failed.\
\rEnsure your path includes %SystemRoot%\\system32\r${error.message}`);
codepage = "850";
}
}

将其注释掉,然后重新启动编辑器,再次上传,问题解决

现在,就可以愉快的用VScode编写Arduino程序辣

编译缓慢解决方法

注意到

1
[Warning] Output path is not specified. Unable to reuse previously compiled files. Upload could be slow. See README.

只需在.vscode -> arduino.json文件中将输出目录 output 配置一下就可以大大提高二次下载的编译速度。

1
2
3
4
5
6
7
{
"configuration": "cpu=atmega328",
"board": "arduino:avr:nano",
"port": "COM5",
"output": "./build",
"sketch": "test.ino"
}