【发布时间】: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