我有以下内容:
两个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 );
}
Run Code Online (Sandbox Code Playgroud)
问题是我连接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);
}
Run Code Online (Sandbox Code Playgroud)
输出总是:
Character received: -1.
Run Code Online (Sandbox Code Playgroud)
如果我改变了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表示序列中没有数据.
更改int到byte真是日新月异的int char.非ASCII符号是尝试呈现character(0b11111111)的结果.-1十进制的负一()是二进制的,因为int默认情况下是有符号的.检查Bin/Dec/Hex转换器以进行验证.
所有这一切都是说xbee.read()返回一个byte/ char.我无法在文档中找到任何内容,但我认为这-1是由于错误(基于硬件Serial 文档).这是因为没有什么可读的.
if (xbee.available()) {
byte temp= xbee.read();
Serial.print(temp);
}
Run Code Online (Sandbox Code Playgroud)
在你有一个可靠的概念证明之前,你应该尽可能简单.一旦它工作,然后一次添加一个功能.这看起来就像你已经在做的那样,但这可能会进一步简化(通过仅使用FTDI,使用硬件序列等将Arduinos排除在等式之外).
这听起来像一个非常酷的项目.祝好运!
| 归档时间: |
|
| 查看次数: |
39272 次 |
| 最近记录: |