简体   繁体   English

Arduino Mega2560 和 ESP32 之间的串行通信问题

[英]Problem with serial communication between Arduino Mega2560 and ESP32

Good morning everyone.大家,早安。 I am trying to establish serial communication between an arduino mega and an esp32, in both I am using hardware serials.我正在尝试在 arduino mega 和 esp32 之间建立串行通信,我都使用硬件串行。 In the arduino the uart3 in the esp the uart2. arduino中的uart3 esp中的uart2。 I have checked the pin connections several times.我已经多次检查引脚连接。 I also adapted the arduino's tx signal to the esp32 with a level shifter.我还使用电平转换器将 arduino 的 tx 信号适配到 esp32。

Essentially I need to send a string to the esp32.本质上我需要向 esp32 发送一个字符串。 The code of the arduino is as follows: arduino的代码如下:

  String InvioDatiESP() {
  String da_passare = ("hello!!!"); 
  return(da_passare);
  }

  void setup() {
     Serial.begin(9600);
     Serial3.begin(115200);
  }

  void loop() {
     Serial3.println(InvioDatiESP());
     Serial.println(InvioDatiESP());
     delay(1000);  
  }

I create the string in a function since it is a reduced version of the actual code in which the string is to be composed.我在 function 中创建字符串,因为它是组成字符串的实际代码的简化版本。

The code of Esp32 is as follows: Esp32的代码如下:

#define RXp2 16
#define TXp2 17

void setup() {
   Serial.begin(115200);
   Serial2.begin(115200, SERIAL_8N1, RXp2, TXp2);
}

void loop() {
   Serial.println(Serial2.readString());
}

I correctly set the boudrate in both serial ports on the IDE to verify communication.我在 IDE 的两个串行端口中正确设置了波特率以验证通信。

The thing I notice that makes me doubt that the problem is related only to the ESP32 reading the string is that in the serial port of the ESP32 in the IDE while the program is running, blank lines are printed on the screen exactly every 1000ms, as if the data is received but not interpreted correctly.我注意到让我怀疑问题是否仅与 ESP32 读取字符串有关的事情是,在程序运行时,在 IDE 中的 ESP32 串口中,每隔 1000 毫秒就会在屏幕上打印空白行,如如果接收到数据但未正确解释。

How could I solve this in your opinion?您认为我该如何解决? Thanks in advance for the answers!提前感谢您的回答!

SOLVED, the lever shifter was disturbing the signal too much, seen with the oscilloscope, I used a resistive divider and everything works perfectly, thank you all the same!已解决,杠杆移位器对信号的干扰太大,用示波器看到,我使用了电阻分压器,一切正常,谢谢大家!

EDIT: Can you try to lower the boudrate?编辑:你能试着降低波特率吗? Check to see if with a lower one it will start decoding properly.检查是否使用较低的它会正确开始解码。

Your problem is not with the code, it's with the hardware.您的问题不在于代码,而在于硬件。 The arduino Mega2560 is using 5V logic level and ESP32 is using 3.3V. arduino Mega2560 使用 5V 逻辑电平,ESP32 使用 3.3V。 You need to do some level shifting to be able to communicate.您需要进行一些级别转换才能进行交流。

You can take a look at this article to learn more about it.您可以查看这篇文章以了解更多信息。

Hope it helps.希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM