
platformio串口监视器功能
刚收到 Arduino Leonardo,我写了个 LED 闪烁加串口通信的小程序,用 platformio 的 device 命令列出设备、开启串口监视器。这篇适合爱用命令行的朋友,用 Vim 就能搞定。
今天收到了前些天买的 Arduino Leonardo,写了一个简单的程序测试了一下基本的LED blinky和串口通信,本文简单介绍一下如何使用 platformio 监视 Arduino的串口数据。
预警:如果您对操作命令行不感冒,那么本篇文章不适合您的口味,不过看看了解一下也不错!
本文测试条件:
- 电脑操作系统 -> macOS
- 使用工具 -> platformio
- 开发板 -> Arduino Leonardo
测试程序
本人喜欢使用vim,所以使用文章 vim也是platformio的IDE 中的方法新建工程:
1 2mkdir leonardo_play && cd leonardo_play pio init --ide vim --board leonardo
完成工程的新建后,为了方便构建程序和上传程序到开发板 Arduino Leonardo,这里还新建文件 Makefile,内容为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17all: platformio -f -c vim run upload: platformio -f -c vim run --target upload clean: platformio -f -c vim run --target clean program: platformio -f -c vim run --target program uploadfs: platformio -f -c vim run --target uploadfs update: platformio -f -c vim update
添加测试源码 src/main.cpp,代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15#include <Arduino.h> void setup() { Serial.begin(115200); pinMode(13, OUTPUT); } void loop() { digitalWrite(13, HIGH); Serial.print("Led on!\n"); delay(100); digitalWrite(13, LOW); Serial.print("Led off!\n"); delay(900); }
上述代码主要实现LED管脚和串口的初始化,在主循环中,每一秒LED闪动一次,期间串口会发送LED状态。以此代码来测试新收到的开发板,还有介绍本文的主题,串口数据监视器功能。
连接 Arduino Leonardo 和 电脑,编译工程并上传程序:
1make all && make upload
程序上传成功后看到,板载led开始闪动,说明程序基本运行了:
下面我们只需要看一下,串口是否打印了LED状态。
device 命令
这个命令用来控制设备,常用的功能就是监视设配通过串口发送的数据,或向设备发送串口数据。串口用的很多,所以这里只拿串口来分享经验。
执行命令,查看一下帮助信息:
1 2 3 4 5 6 7 8 9➜ leonardo pio device -h Usage: pio device [OPTIONS] COMMAND [ARGS]... Options: -h, --help Show this message and exit. Commands: list List devices monitor Monitor device (Serial)
可以看到有两个子命令:
- list : 列出所有可用设备;
- monitor : 监视串口设备;
列出可用设备
执行命令:
1pio device list
此时就会列出所有可用大的串口接口,我的是这样:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25➜ leonardo pio device list /dev/cu.SOC ----------- Hardware ID: n/a Description: n/a /dev/cu.MALS ------------ Hardware ID: n/a Description: n/a /dev/cu.Bluetooth-Incoming-Port ------------------------------- Hardware ID: n/a Description: n/a /dev/cu.yeAirPods-WirelessiAP-1 ------------------------------- Hardware ID: n/a Description: n/a /dev/cu.usbmodem1441 -------------------- Hardware ID: USB VID:PID=2341:8036 LOCATION=20-6 Description: Arduino Leonardo
能够看到, Arduino Leonardo 对应的串口应该是 /dev/cu.usbmodem1441。
开启监视器
pio device monitor [ARGS] 就能开启监视器[1],ARGS 如果没有,就会使用默认值,串口监视相关的 ARGS 相关常用项使用信息如下表:
还有很多其它参数项可以设置,这里列出的只是常用的。不加 ARGS 只执行 pio device monitor 可以自动识别和匹配参数,其实在这里执行了无参数的命令就可以正常看到 Arduino Leonardo 的打印信息了。
指定端口
有时,命令不能自己识别端口,需要我们自己指定一个特定端口,这项参数其实也可以配置到 platformio.ini 文件中作为参数的默认值[2]。在这里我们使用前面提到的端口 /dev/cu.usbmodem1441:
1pio device monitor -p /dev/cu.usbmodem1441
通信速率
决定通信速率的参数是 波特率 ,程序中我们设置了波特率为 115200,同样这个参数也支持配置到文件 platformio.ini 中以作默认值[2]。如果在命令中的话,可以这样指定:
1pio device monitor -p /dev/consoleu.usbmodem1441 -b 115200
校验位
在串口通信中,为了提高串口数据剧的准确性,会使用校验位,常用的是奇偶校验,奇校验是 odd,所以对应值为 O,偶校验是 even,对应 E,默认无校验 None,对应 N,如果要指定偶校验位,这样做:
1pio device monitor --patity E
编码
这个编码的功能类似于windows下好多串口助手中的文本/二进制选择的功能,默认串口助手开启以后会按照Ascii字符显示打印数据:
1 2 3 4 5 6 7➜ leonardo pio device monitor --echo --- Miniterm on /dev/cu.usbmodem1441 9600,8,N,1 --- --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- Led on! Led off! Led on! Led off!
调试程序的时候,有可能会需要串口通信收发二进制数据,尤其是使用一定通信协议的情况下,此时想看二进制形式的数据,使用命令的时候需要指定 --encoding 为 hexlify:
1 2 3 4➜ leonardo pio device monitor --encoding hexlify --- Miniterm on /dev/cu.usbmodem1441 9600,8,N,1 --- --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- 4C 65 64 20 6F 6E 21 0A 4C 65 64 20 6F 66 66 21 0A
可以看到打印结果就是原来字符串对应的Ascii码了!
数据回显
主要用于发送数据的时候,默认情况是不会显示发送的字符或数据的,当开启回显时就能显示发送的数据:
1 2 3 4 5 6 7 8 9➜ leonardo pio device monitor --echo --- Miniterm on /dev/cu.usbmodem1441 9600,8,N,1 --- --- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H --- Led off! Led on! Led off! Led on! yLed off! yyyyyLed on!
可以看到我发了好多 y,在开启串口监视后,在键盘上按按键就会发送按键对应的字符,在二进制模式(hexlify)下时,只识别十六进制码然后发送。
临时配置
device 命令使用的是 miniterm,所以在使用中可以临时配置一些参数,开启监视器后可以按快捷键 CTRL+T CTRL + H 就会打印帮助信息:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23--- pySerial (3.4) - miniterm - help --- --- Ctrl+C Exit program --- Ctrl+T Menu escape key, followed by: --- Menu keys: --- Ctrl+T Send the menu character itself to remote --- Ctrl+C Send the exit character itself to remote --- Ctrl+I Show info --- Ctrl+U Upload file (prompt will be shown) --- Ctrl+A encoding --- Ctrl+F edit filters --- Toggles: --- Ctrl+R RTS Ctrl+D DTR Ctrl+B BREAK --- Ctrl+E echo Ctrl+L EOL --- --- Port settings (Ctrl+T followed by the following): --- p change port --- 7 8 set data bits --- N E O S M change parity (None, Even, Odd, Space, Mark) --- 1 2 3 set stop bits (1, 2, 1.5) --- b change baud rate --- x X disable/enable software flow control --- r R disable/enable hardware flow control
根据帮助信息进行操作即可修改通信参数。
总结
本文主要分享了 platformio 的串口监视功能,程序设计中少不了串口的使用,所以这个功能一定是非常需要的,尤其是对于喜欢使用 CLI 的童鞋,希望本文对您有所帮助!



