蜗蜗牛 发表于 2018-3-31 11:27:57

arduino Mega2560

使用两块2560主板进行串口通讯,主机发送指令,从机能够接收到,可是主机读取不了从机串口的讯息是怎么回事?我改用两块uno板,同样的程序可以实现,2560不管怎么弄,都不行

弘毅 发表于 2018-3-31 18:26:35

感觉还是你代码写的问题

蜗蜗牛 发表于 2018-4-1 16:59:42

弘毅 发表于 2018-3-31 18:26
感觉还是你代码写的问题

谢谢,我偶然查到了,原来2560,只有10,11,12,13等等串口才能使用软串口?大概是这个意思吧?那篇讲解是英文,只看懂了大概

蜗蜗牛 发表于 2018-4-2 10:13:34

弘毅 发表于 2018-3-31 18:26
感觉还是你代码写的问题

想继续问一下,我想用uno板当主机,2560当从机,主机接收从机串口打印的数字,UNO板接受后输出的怎么是一个一个的数字,就像这样
1
2
3
4



主机程序:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);
String n="";
//int n;
int a,b;
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}

void loop() {
if(mySerial.available()){
    n+=char(mySerial.read());
    delay(2);
    a=1;
}
if(a==1){
    Serial.println(n);
    delay(1000);
    n="";
}   
}
从机:
int i;
void setup() {
Serial.begin(9600)
}

void loop() {
i=1234;
Serial.println(i);
delay(1000);
}

蜗蜗牛 发表于 2018-4-2 11:07:41

谢谢,不用回复了,已解决:lol
将主机程序中的 if(mySerial.available())改为while(mySerial.available()),我一直以为if和while用法应该差不多,看来我还得详细看看它们的区别:L
页: [1]
查看完整版本: arduino Mega2560