我有以下设备:
两个 Arduinos 和两个 XBees。我想要从一个设备发送数据到另一个设备。XBees 可以通信,因为我已经进行了测试(将一个 XBee 连接到 Arduino,将另一个连接到 PC,在一个终端中写入,然后在另一个终端中查看)。
现在我想要从一个设备发送数据到另一个设备:
这是我的两个脚本:
用于发送的脚本(在之前的测试中已经测试过可以发送所有字母):
#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3); // RX, TX
char c = 'A';
int pingPong = 1;
void setup()
{
Serial.begin(9600);
Serial.println( "Arduino started sending bytes via XBee" );
//Set the data rate for the SoftwareSerial port
xbee.begin(9600);
}
void loop() {
// Send character via XBee to other XBee connected to Mac
// via USB cable.
xbee.write( c );
//--- Display the character just sent on console. ---
Serial.println( c );
//--- Get the next letter in the alphabet, and reset to ---
//--- 'A' once we have reached 'Z'.
c = c + 1;
if ( c>'Z' )
c = 'A';
//--- Switch LED on Arduino board for every character sent---
if ( pingPong == 0 )
digitalWrite(13, LOW);
else
digitalWrite(13, HIGH);
pingPong = 1 - pingPong;
delay( 1000 );
}
问题在于我连接了一块Arduino来接收另一个XBee发送的数据。
以下是我的代码:
#include <SoftwareSerial.h>
SoftwareSerial xbee(2, 3); // RX, TX
void setup()
{
Serial.begin(9600);
Serial.println( "Arduino started receiving bytes via XBee" );
// Set the data rate for the SoftwareSerial port.
xbee.begin(9600);
}
void loop() {
int temp = xbee.read();
Serial.print("Character received:");
Serial.println(temp);
delay(1000);
}
输出总是:
Character received: -1.
如果我将
temp
从int
更改为byte
,我会看到Character received: (a non-[ASCII][3] symbol)
。我正在使用XBee系列1。
它们通过X-CTU进行配置,基于在ladyada.net上找到的一个教程。
然后我将XBee连接到Arduino(TX连接到引脚3,RX连接到2,分别连接Vcc和GND),另一个XBee通过FTDI电缆连接到PC。我能够从Arduino发送字符并在X-CTU的串行监视器中看到它们。这是否意味着它们被正确配置了?
然后我想将Arduino连接到我的接收器。您可以查看上面的代码。我始终得不到可用数据。
返回的-1表示串行中没有数据。