【发布时间】:2013-01-07 15:48:21
【问题描述】:
我有以下情况:
两个Arduinos 和两个XBees。我想将数据从一个发送到另一个。 XBees 进行通信,因为我进行了提议测试(将一个 XBee 与 Arduino 连接,另一个连接到 PC,从一个 XBee 写入,并在另一个终端中观察另一个)。
现在我想将数据从一个发送到另一个:
这是我的两个脚本:
对于发送(在前一个发送所有字母的测试中测试过):
#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表示串口没有数据。
【问题讨论】:
-
您使用的是什么 Arduino 板和版本?
-
我试过 arduino Uno 到 Uno 和 rduino Uno 到 micro。
标签: arduino