Arduino RP2040 两个CDC虚拟串口通讯

编程入门 行业动态 更新时间:2024-10-26 05:22:09

Arduino RP2040 两个CDC虚拟<a href=https://www.elefans.com/category/jswz/34/1769224.html style=串口通讯"/>

Arduino RP2040 两个CDC虚拟串口通讯

Arduino RP2040 两个CDC虚拟串口通讯


  • 🎬通讯效果演示:
  • 🌿基于Earle F. Philhower的固件开发平台:
  • 🔖USB配置参考:.html
  • ✅本例程使用的开发板:YD-RP2040版(源地YD-RP2040)

📑功能说明

🖋两个CDC虚拟串口通讯,不受波特率限制,物理上只占用一个USB接口。

🛠参数配置

  • 🔧编译并运行此例程需要将默认参数中的USB Stack配置为Adafruit TinyUSB库才可以实现多个USB设备的挂载。
  • 🔨修改Adafruit TinyUSB库中的默认参数

🔖个人的文件路径参考:

C:\Users\Administrator\AppData\Local\Arduino15\packages\rp2040\hardware\rp2040\3.3.0\libraries\Adafruit_TinyUSB_Arduino\src\arduino\ports\rp2040
    • 👉🏻修改宏CFG_TUD_CDC 参数为2,默认是只生成一个CDC虚拟串口。
//--------------------------------------------------------------------
// Device Configuration
//--------------------------------------------------------------------#define CFG_TUD_ENDOINT0_SIZE 64#define CFG_TUD_CDC 2   //默认值是1,只生成一个虚拟CDC端口,2代表生成2个CDC虚拟端口 
#define CFG_TUD_MSC 1
#define CFG_TUD_HID 2
#define CFG_TUD_MIDI 1
#define CFG_TUD_VENDOR 1
  • 🌿程序烧录成功后,将生成2个CDC虚拟串口:
  • ✨如果是第一次使用Arduino平台开发,那么需要按住RP2040开发板上的Boot按键,通过USB数据线连接到电脑USB端口,会出现一个64MB的虚拟U盘,此再编译上传代码即可。

📝例程代码

  • 🔖本例程代码可以在固件库自带的例程示例中找到:
/*This example demonstrates the use of multiple USB CDC/ACM "VirtualSerial" portsWritten by Bill Westfield (aka WestfW), June 2021.Copyright 2021 by Bill WestfieldMIT license, check LICENSE for more information
*//* The example creates two virtual serial ports. Text entered onany of the ports will be echoed to the all ports with- all lower case in port0 (Serial)- all upper case in port1
需要修改库中的配置参数:libraries/Adafruit_TinyUSB_Arduino/src/arduino/ports/rp2040/tusb_config_rp2040.h
#define CFG_TUD_CDC 2 //默认值是1,只生成一个虚拟CDC端口,2代表生成2个CDC虚拟端口Requirement:The max number of CDC ports (CFG_TUD_CDC) has to be changed to at least 2.Config file is located in Adafruit_TinyUSB_Arduino/src/arduino/ports/{platform}/tusb_config_{platform}.hwhere platform is one of: nrf, rp2040, samdNOTE: Currnetly multiple CDCs on ESP32-Sx is not yet supported.An PR to update core/esp32/USBCDC and/or pre-built libusb are needed.We would implement this later when we could.
*/#include <Adafruit_TinyUSB.h>#define ARDUINO_ARCH_RP2040
#define LED LED_BUILTIN// Create 2nd instance of CDC Ports.
#ifdef ARDUINO_ARCH_ESP32
#error "Currnetly multiple CDCs on ESP32-Sx is not yet supported. An PR to update core/esp32/USBCDC and/or pre-built libusb are needed."
// for ESP32, we need to specify instance number when declaring object
Adafruit_USBD_CDC USBSer1(1);
#else
Adafruit_USBD_CDC USBSer1;
#endifvoid setup() {pinMode(LED, OUTPUT);Serial.begin(115200);// check to see if multiple CDCs are enabledif ( CFG_TUD_CDC < 2 ) {digitalWrite(LED, HIGH); // LED on for error indicatorwhile (1) {//如果没有修改Adafruit_TinyUSB_Arduino/src/arduino/ports/tusb_config_rp2040.h中的CFG_TUD_CDC参数,程序将停留在这里面运行Serial.printf("CFG_TUD_CDC must be at least 2, current value is %u\n", CFG_TUD_CDC);Serial.println("  Config file is located in Adafruit_TinyUSB_Arduino/src/arduino/ports/{platform}/tusb_config_{platform}.h");Serial.println("  where platform is one of: nrf, rp2040, samd");digitalWrite(LED, HIGH);delay(1000);digitalWrite(LED, LOW);delay(1000);}}// initialize 2nd CDC interfaceUSBSer1.begin(115200);while (!Serial || !USBSer1) {if (Serial) {Serial.println("Waiting for other USB ports");}if (USBSer1) {USBSer1.println("Waiting for other USB ports");}delay(1000);}Serial.print("You are port 0\n\r\n0> ");USBSer1.print("You are port 1\n\r\n1> ");
}int LEDstate = 0;void loop() {int ch;ch = Serial.read();if (ch > 0) {printAll(ch);}ch = USBSer1.read();if (ch > 0) {printAll(ch);}if (delay_without_delaying(500)) {LEDstate = !LEDstate;digitalWrite(LED, LEDstate);}
}// print to all CDC ports
void printAll(int ch) {// always lower caseSerial.write(tolower(ch));// always upper caseUSBSer1.write(toupper(ch));
}// Helper: non-blocking "delay" alternative.
boolean delay_without_delaying(unsigned long time) {// return false if we're still "delaying", true if time ms has passed.// this should look a lot like "blink without delay"static unsigned long previousmillis = 0;unsigned long currentmillis = millis();if (currentmillis - previousmillis >= time) {previousmillis = currentmillis;return true;}return false;
}

更多推荐

Arduino RP2040 两个CDC虚拟串口通讯

本文发布于:2024-02-11 23:42:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1684354.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:串口   两个   通讯   Arduino   CDC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!