将 Arduino Leonardo 用作 USB/UART 适配器
与 Arduino Uno 等旧设计相比,Arduino Leonardo 具有用于 TTL-UART 的单独连接 Serial1,而 Serial 用于 USB CDC UART 接口。
这允许将 Leonardo 用作 USB/UART 桥接器,而无需求助于 Arduino Mega 2560 等更昂贵的板。为此,使用此草图,它也可以修改为提供智能 UART 桥接器。
记住为你的应用程序调整波特率。此版本的草图不支持通过 CDC 外设自动选择波特率。
arduino_leonardo_usbuart_bridge.ino
/*
Arduino Leonardo 的 USB/UART 桥接器
最初由 Uli Koehler 于 2015 年编写
发布在 techoverflow.net
版本 1.0
在公共领域发布 (CC 1.0 Universal)
*/
void setup() {
// 在此处调整波特率
Serial.begin(115200);
Serial1.begin(115200);
//等待直到 USB CDC 端口连接
while (!Serial) {
}
}
void loop() {
//复制通过 TTL 串口传入的字节
if (Serial1.available() > 0) {
Serial.write(Serial1.read());
}
//复制通过 CDC 串口传入的字节
if (Serial.available() > 0) {
Serial1.write(Serial.read());
}
}更新 2017-09-13:修复 const int baudrate 在 avr-gcc 上是 2 字节 int 因此不能将 115200 作为数字。感谢 Andrei Kovari!
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow