循环中Digi Xbee V1读取函数出现Arduino定时问题()

循环中Digi Xbee V1读取函数出现Arduino定时问题(),arduino,xbee,Arduino,Xbee,我开始使用XBee射频模块,并对如何以及为什么会出现这种定时问题有一个普遍的关注。。。两个XBee确实可以通信,我正在Arduino Uno上测试一个通过屏蔽,另一个通过PC和X-CTU上的USB接口。代码(通常)如下所示,使用Sparkfun的示例代码作为基础(位于) //我们将使用SoftwareSerial与XBee通信: #包括 //XBee的DOUT(TX)连接到引脚2(Arduino的软件RX) //XBee的DIN(RX)连接到引脚3(Arduino的软件TX) 软件系列XBee(

我开始使用XBee射频模块,并对如何以及为什么会出现这种定时问题有一个普遍的关注。。。两个XBee确实可以通信,我正在Arduino Uno上测试一个通过屏蔽,另一个通过PC和X-CTU上的USB接口。代码(通常)如下所示,使用Sparkfun的示例代码作为基础(位于)

//我们将使用SoftwareSerial与XBee通信:
#包括
//XBee的DOUT(TX)连接到引脚2(Arduino的软件RX)
//XBee的DIN(RX)连接到引脚3(Arduino的软件TX)
软件系列XBee(2,3);//接收,发送
无效设置()
{
//将两个端口设置为9600波特。此值最重要
//对于XBee。确保波特率与配置匹配
//设置您的XBee。
XBee.begin(9600);
Serial.begin(9600);
}
void循环()
{
if(XBee.available())
{//如果数据来自XBee,则将其发送到串行监视器
Xbee.写入(“准备就绪”);
Serial.write(XBee.read());
}
}
我担心的是,我发送2个ASCII字符(2位数字,10、20、30等)的数据包只是为了测试。在X-CTU的控制台监视器中,我注意到以下几点。(接收到粗体,发送斜体。)

10readyready20readyready30readyready

有人能用外行的语言向我解释一下这是怎么发生的吗?我无法理解代码是如何按此顺序执行的。

您在XBee上使用的是“AT模式”或“透明串行”模式,因此字符一次只能到达一个。没有进行任何打包

因此,您从X-CTU发送一个带有
10
的数据包。Arduino上的XBee接收该数据包并开始向Arduino发送有效负载。Arduino接收
1
,触发代码发送
ready
,作为响应。然后,Arduino接收
0
,并发送另一个
ready

如果希望以块而不是流的形式查看数据,则需要向串行流中添加某种帧(如在每行数据之后添加回车符)或切换到API模式(以头和校验和的形式提供网络有效负载)


我还没有用过它,但有一个库是为在Arduino微控制器上以API模式使用XBee模块而设计的。

谢谢!现在我需要研究如何正确地实现这个功能,我发现它不像AT模式下的聊天那么简单。不过听起来很合适!
// We'll use SoftwareSerial to communicate with the XBee:
#include <SoftwareSerial.h>
// XBee's DOUT (TX) is connected to pin 2 (Arduino's Software RX)
// XBee's DIN (RX) is connected to pin 3 (Arduino's Software TX)
SoftwareSerial XBee(2, 3); // RX, TX

void setup()
{
  // Set up both ports at 9600 baud. This value is most important
  // for the XBee. Make sure the baud rate matches the config
  // setting of your XBee.
  XBee.begin(9600);
  Serial.begin(9600);
}

void loop()
{
  if (XBee.available())
  { // If data comes in from XBee, send it out to serial monitor
    Xbee.write("ready");
    Serial.write(XBee.read());
  }
}