【问题标题】:Reading data over serial Arduino and XBee通过串行 Arduino 和 XBee 读取数据
【发布时间】: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.

如果我将 tempint 更改为 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


【解决方案1】:

int 更改为byte 实际上是将int 更改为char。非ASCII 符号是尝试渲染字符 (0b11111111) 的结果。十进制中的负一 (-1) 是二进制中的所有一,因为 int 是默认签名的。查看Bin/Dec/Hex Converter 进行验证。

所有这一切都是说xbee.read() 返回一个byte/char。我在the documentation 中找不到任何东西,但我认为-1 是由于错误造成的(基于硬件Serialdocumentation)。这是因为没有什么可读的。

您可以尝试以下方法:

  • 确保 RX/TX 线路正确。相信我,它会发生。
  • 在读取之前检查 XBee 是否有可用数据。 (打印的行数会少很多,因为它会等到准备好读取一个字节。)
if (xbee.available()) {
    byte temp= xbee.read();
    Serial.print(temp);
}
  • 使用内置(硬件)。 SoftwareSerial 应该可以工作,但根据我的经验,硬件串行更可靠。
    • 根据您的 Arduino 型号,您可能需要 (Disable Auto Reset on Serial Connection)。这似乎仅在您尝试通过 FTDI 芯片从 IDE 的串行监视器(一般而言)以外的其他位置发送数据时才需要。
  • 这个线程 Arduino to Arduino XBee Serial Communication 有一个非常相似的设置,似乎可以正常工作。尽可能简化您的工作,然后慢慢添加功能。
  • 将 XBee RX 和 TX 线直接连接到 USB-to-FTDI 连接器,例如 this cablebreakout board

在您获得有效的概念证明之前,您应该使其尽可能简单。一旦它工作,然后一次添加功能。这看起来就像您已经在做的事情,但这可能会进一步简化(通过仅使用 FTDI、使用硬件串行等将 Arduinos 排除在外)。

这听起来是一个很酷的项目。祝你好运!

【讨论】:

  • 它总是转到 else.. “没有可用的数据”。只有 FTDI 才有效。
  • 如果 FTDI 工作,然后尝试使用 Arduino 的硬件串行。我怀疑问题出在 SoftwareSerial 的使用或 RX/TX 线路的混淆处。 (可能两者都有)。您可以在tristate 模式下尝试您的 Arduino,以确保在使用硬件串行时您的 RX/TX 线是正确的。一旦解决了,你就可以开始调试你的代码了。
  • 我尝试更改端口(TX 和 RX)以防我连接错误但仍然没有更改。我会用硬件序列号再次检查。
  • 我找不到任何有关如何使用硬件串行的示例?你什么意思?
  • Here 是程序文档。硬件串行意味着您将 xbee 的串行线连接到 "tx-&gt;1""rx&lt;-0" (arduino.cc/en/Main/ArduinoBoardUno)(板的右上角)
【解决方案2】:
void loop()  {
    unsigned char temp; = 
    if (Serial.available() > 0){
       temp = xbee.read();
       Serial.print("Character received:");
       Serial.println(temp,HEX);
}

试试这个代码。
我将您的 int temp 转换为 unsigned char 来处理 MSB。这个对我有用。但我用的是 Xbee 系列 2 和硬件系列。无论如何,数据处理很重要。

【讨论】:

    【解决方案3】:

    我知道这有点晚了,但也许目前仍有人需要此解决方案。

    if (xbee.available()) {
        byte temp= xbee.read();
        Serial.print(temp);
    }
    

    对于此代码在接收部分,我将serial.print(tempt) 更改为serial.write(tempt)。原因是当我使用serial.print(variablesname) 时它会显示 ASCII 码,但是当我使用serial.write(variablesname) 时它会按预期显示。如果你们中的任何人仍然卡在这个问题上,可以尝试一下。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多