基于下面的电路,我尝试连接两个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的串行控制台中没有输出。有人能告诉我可能的原因和解决办法吗?如果我遗漏了什么,过分强调或者强调过少,请在评论中告诉我。
发布于 2018-02-05 16:20:42
如果我理解正确,你有一个阿迪诺连接到你的电脑和另一个阿迪诺?
问题是您需要指定要使用的串行端口:这很容易,只需键入Serial1
或Serial2
,而不是只输入Serial
。这允许您打开两个串口:一个到您的另一个Arduino和您的计算机显示结果!
发布于 2018-02-05 14:45:30
您需要检查串行的可用数据:
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
Serial.readBytes(mystr, 5);
Serial.print("I received: ");
Serial.println(mystr, DEC);
}
}
https://stackoverflow.com/questions/48622905
复制相似问题