MultSerial.ino 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. *******************************************************************************
  3. * Copyright (c) 2022 by M5Stack
  4. * Equipped with M5Core sample source code
  5. * 配套 M5Core 示例源代码
  6. * Visit for more information: https://docs.m5stack.com/en/core/gray
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/core/gray
  8. *
  9. * Describe: MultSerial. 多串口
  10. * Date: 2021/8/5
  11. ******************************************************************************
  12. */
  13. #include <M5Stack.h>
  14. void setup() {
  15. M5.begin(); // Init M5Core. 初始化 M5Core
  16. // Serial2.begin(unsigned long baud, uint32_t config, int8_t rxPin, int8_t
  17. // txPin, bool invert)
  18. Serial2.begin(115200, SERIAL_8N1, 16,
  19. 17); // Init serial port 2. 初始化串口2
  20. }
  21. void loop() {
  22. if (Serial
  23. .available()) { // If the serial port reads data. 如果串口读到数据
  24. int ch = Serial.read(); // Copy the data read from the serial port to
  25. // the CH. 把串口读取到的数据复制给ch
  26. Serial2.write(
  27. ch); // Serial port 2 Outputs the CH content. 串口2输出ch的内容
  28. M5.Lcd.printf("Serial:%d\n",
  29. ch); // The screen prints the data received by serial
  30. // port 2. 屏幕打印串口2收到的数据
  31. }
  32. if (Serial2.available()) {
  33. int ch = Serial2.read();
  34. Serial.write(ch);
  35. M5.Lcd.printf("Serial2:%d\n", ch);
  36. }
  37. }