我有 2 个 Arduinos Leonardo,我希望它们自己交流,所以我做了以下代码:
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
String outMessage = ""; // String to hold input
while (Serial.available() > 0) { // check if at least 1 char is available
char inChar = Serial.read();
outMessage.concat(inChar); // add inChar to outMessage
}
if (outMessage != "") {
Serial.println("Sent: " + outMessage); // View Arduino 1 in Serial Monitor 1
Serial1.print(outMessage); // Send to Arduino 2
}
while (Serial1.available() > 0) {
Serial.print("Received: "); // View Arduino 1 in Serial Monitor 2
Serial.print(Serial1.read()); // Received from Arduino 1
Serial.println();
}
}
我想从 Arduino 1 发送一条消息,在串行监视器中打印并通过 TX1 发送到 Arduino 2,反之亦然。问题是我没有收到我所期望的。例如,如果我输入test
:
阿杜诺1:
Sent: test
阿杜诺 2:
Received: t
Received: e
Received: s
Received: t
我也尝试像发送方一样做接收方并使用Serial.write
但没有成功。有没有更简单的方法来做到这一点或修复它?
谢谢