ISO485.ino 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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/unit/iso485
  7. * 获取更多资料请访问: https://docs.m5stack.com/zh_CN/unit/iso485
  8. *
  9. * Describe: iso485.
  10. * Date: 2021/8/30
  11. *******************************************************************************
  12. Please connect to PortC,Pressed ButtonA :send "hello world"
  13. 请连接端口C,Pressed ButtonA :send "hello world"
  14. */
  15. #include <M5Stack.h>
  16. String str = "";
  17. void setup() {
  18. M5.begin();
  19. M5.Lcd.drawString("ISO485", 20, 0, 2);
  20. Serial2.begin(115200, SERIAL_8N1, 16, 17);
  21. M5.Lcd.setCursor(0, 20);
  22. }
  23. void loop() {
  24. if (M5.BtnA.wasPressed()) {
  25. Serial2.write("Hello World\r\n");
  26. }
  27. if (Serial2.available()) {
  28. char ch = Serial2.read();
  29. str += ch;
  30. if (str.endsWith("\r\n")) {
  31. Serial.print(str);
  32. M5.Lcd.print(str);
  33. str = "";
  34. }
  35. }
  36. M5.update();
  37. }