【问题标题】:Unable to do Arduino Mega to Arduino Mega serial communication无法进行 Arduino Mega 到 Arduino Mega 串行通信
【发布时间】:2018-02-05 12:45:02
【问题描述】:

根据下面的电路,我尝试连接两个Arduino Mega进行串行通信。

发件人代码:

char mystr[3] = "Hello"; //String data

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  Serial.write(mystr, 5); //Write the serial data
  delay(1000);
}

接收方代码:

char mystr[5]; //Initialized variable to store received data

void setup() {
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  Serial.readBytes(mystr, 5); //Read the serial data and store in var
  delay(1000);
}

Arduino 的串口控制台没有输出。有人可以告诉我可能的原因和解决方案。如果我遗漏了什么,过分强调或过分强调某一点,请在 cmets 中告诉我。

【问题讨论】:

  • 不相关,但您声明要发送的字符串没有意义。
  • 您希望如何将 5 个字符 + nul 终止符放入 3 个字符的数组中?该字符串甚至不适合 5 个字符的数组。 readBytes 不会 nul 终止字符串。你究竟是如何连接 Megas 的?您的图片显示 Unos。
  • @gre_gor 正如问题中提到的那样,使用完全相同的方法在 Serial 上连接 MEGA,代码来自此 [create.arduino.cc/projecthub/harshmangukiya/…
  • 那个教程太糟糕了。
  • 在从串口缓冲区读取数据之前,您需要使用if(Serial.available)检查串口是否有数据

标签: arduino serial-port embedded


【解决方案1】:

如果我理解这一点,你有一个 Arduino 连接到你的电脑和另一个 Arduino?

问题是你需要指定使用哪个串口: 这很简单,只需输入Serial1Serial2 而不是Serial。这允许您打开 2 个串行端口:一个连接到您的另一个 Arduino,一个连接到您的计算机以显示结果!

链接:https://www.arduino.cc/en/Tutorial/MultiSerialMega

【讨论】:

  • 为什么 Uno 可以,但 Mega 不行
【解决方案2】:

您需要从串口检查可用数据:

void loop() {
    if (Serial.available() > 0) {
            // read the incoming byte:
            Serial.readBytes(mystr, 5);
            Serial.print("I received: ");
            Serial.println(mystr, DEC);
    }
}

【讨论】:

  • 如果您收到-1,您没有任何串行数据。此外,您通过Serial.write(mystr, 5); 作为字符串发送但想读取二进制数据Serial.readBytes(mystr, 5);,您需要调用Serial.Read()
  • 但是 Rx 缓冲区中没有输出
猜你喜欢
  • 2017-12-10
  • 1970-01-01
  • 2013-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多