无法读取 NodeMCU 上的 serial/uart 个引脚

Unable to read serial/uart pins on NodeMCU

我无法在 NodeMCU Lua 环境中读取串行引脚。一直只能读取USB串口

我已将串行适配器连接到 rx、tx 和 g 引脚。

我试过这个代码:

uart.on("data","\n",function(data) print("receive from uart:", data) end, 0)

我在 ESplorer 控制台中输入文本,它确实读取了该文本。它没有读取我通过插入 rx/tx/g 引脚的串行适配器发送的任何内容。


uart.write(0, "hello")

我断开了 USB 电缆并使用串行适配器为其供电。使用此代码未发送任何内容。我尝试了 uart.write(0,uart.write(1,.


如何读取pin串口而不是usb串口?

我需要拔下 USB 数据线。如果插入 USB 数据线并且您尝试使用 pin 串口,设备会变得混乱。

在 esp 论坛上查看我的问题: https://www.esp8266.com/viewtopic.php?f=22&t=19768

您必须使用与 RX 和 TX 不同的引脚,因为它们与您将 NodeMCU 连接到 PC 的 USB 端口相同

https://github.com/scottwday/EspSoftSerial 库的帮助下,您可以使用任何其他 2 个空闲的 gpio 引脚作为串行端口。该库专门用于您的 NodeMCU 所基于的 ESP8266。

这样你就有了2个串口,一个通过usb,另一个连接到其他设备。

一些简单的代码来实现下面的软件序列。

    #include <SoftwareSerial.h>

    #define BAUD_RATE 9600

    SoftwareSerial Serial2(D8, D7, false, 8); //Here you choose the pins you connect the RX TX device to 
                                             //The first pin you choose is RX the second TX 
                                             // in my example these are the D8 and D7 pins on the nodeMCU
                                             // D8=RX .... D7=TX 

    void setup() {
      Serial.begin(BAUD_RATE);
      Serial2.begin(BAUD_RATE);

      Serial.println(" ### Hello ###");
      Serial2.println(" ### Hello ###");

 }


void loop() {

}
}