我有一个XBee-Arduino,它向接收器XBee-Arduino发送一个简单的int计数器。接收器可以正常打印,直到达到16或更高。这是我的输出示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 48 17 50 19 52 21 54 23 56 25 26 59 28 61 62 63 64 65岁 98 67 68 69
我尝试了新的XBees,而XBees似乎不是问题。
发射器代码:
#include "SoftwareSerial.h"
int count = 1;
// RX: Arduino pin 2, XBee pin DOUT. TX: Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3);
void setup() {
XBee.begin(115200);
Serial.println ("Initializing...");
}
void loop() {
XBee.write(count);
delay(1000);
count++;
}
接收方代码:
#include "SoftwareSerial.h"
// RX: Arduino pin 2, XBee pin DOUT. TX: Arduino pin 3, XBee pin DIN
SoftwareSerial XBee(2, 3);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Started Ground station");
XBee.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
if (XBee.available())
{
int c = XBee.read();
Serial.println(c);
delay(1000);
}
else
{
Serial.println("XBee not available.");
delay(1000);
}
}
我只希望接收者按原样打印计数器。不知道为什么我在15点之后会得到那些随机数。感谢您的帮助。
答案 0 :(得分:2)
不正确的数字相差一位。
这可能是由于读取串行线时的时序不同而引起的,尤其是在高波特率下(在高波特率下软件串行不是特别可靠)。您可以看到某个位置已漂移到相邻位的时序中,例如
16 = 0001 0000 was interpreted as 0011 0000 = 48
18 = 0001 0010 was interpreted as 0011 0010 = 50
20 = 0001 0100 was interpreted as 0011 0100 = 52
27 = 0001 1011 was interpreted as 0011 1011 = 59
34 = 0010 0010 was interpreted as 0110 0010 = 98
尝试使用较低的波特率,以使时序不再那么关键,并且位不太可能漂移到相邻位的时序中。