【问题标题】:Xbee Serial.read is not clearing bufferXbee Serial.read 未清除缓冲区
【发布时间】:2020-12-26 19:36:13
【问题描述】:

我编写了一个示例程序来测试从 xbee 读取的串行数据。我期待每 5 秒从发送器到接收器传递一条消息,但在接收器的串行监视器中,我观察到连续的重复消息流。任何人都可以我想念的东西。仅供参考:还附有串行监视器屏幕截图的链接。 [1]:https://i.stack.imgur.com/Lgxx5.png

/*  ~ Simple Arduino - xBee Transmitter sketch ~ Router
*/

int count = 0;

void setup() {
    Serial.begin(9600);
}

void loop() {
    //Send the message:
    count ++;
    Serial.println(String("Hello World : " + String(count)));
  delay(5000);
}
/*  ~ Simple Arduino - xBee Receiver sketch ~ Coordinator
*/

void setup() {
    Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0){
    Serial.write(Serial.read());
  }
}

【问题讨论】:

    标签: arduino xbee


    【解决方案1】:

    对于接收方,您不只是想将Serial.read() 传递给print() 而不是Serial.write() 吗?如果您有两个串行端口,一个到控制台,一个到 XBee,它们应该有不同的名称。

    您能否提供更多有关您的串行连接的详细信息? COM3 和 COM6 连接到什么位置?您是否与 XBee 和您的控制台共享串行端口引脚?似乎这可能是您的问题的一部分,如果 Arduino 或 XBee 可以驱动接收器串行端口的 RX 引脚,您最终会将您的角色回显给自己。

    【讨论】:

    • 您好,感谢您的回复。我已经通过 USB 将两个 Ardunio(发射器和接收器)连接到我的 PC。发射器为 COM3。接收器为 COM6。这是从 Arduino 到 XBEE Shield 的电路连接:D0/RX 到 TX D1/TX 到 RX 5V 到 5V GND 到 GND
    • 这就是问题所在。您将串行端口连接到 PC 的 USB 连接和 XBee 模块。
    • 发射器上的 Serial.println() 连接到 XBee 和 COM3(这就是为什么你会看到你正在发送的内容)。然后你在接收器上的接线是这样的,你从 XBee 接收数据,然后再次回显它,但是以一种可能循环数据的方式(可能它的ATDHATDL 设置导致数据发送给自己)。想办法拥有两个串行端口(一个可能带有“SoftwareSerial”),一个连接到 XBee,一个连接到您的 PC 的 USB 连接。
    【解决方案2】:

    想出一个变通办法来解决这个问题。以下是详细信息:

    https://i.stack.imgur.com/3qZMi.png

    Circuit connections from Arduino to XBEE Shield: 
    D0/RX to TX
    D1/TX to RX 
    5V to 5V 
    GND to GND 
    
    /*  ~ Simple Arduino - xBee Transmitter sketch ~ Router
     */
    
    void setup() {
        Serial.begin(9600);
    }
    
    void loop() {
      //Send the message:
      Serial.print('<');
      Serial.print("Hello World");
      Serial.println('>');
      delay(1000);
    }
    
    /*  ~ Simple Arduino - xBee Receiver sketch ~ Coordinator
    */
    
    bool started = false; //True: Message is strated
    bool ended = false;   //True: Message is finished
    byte index;           //Index of array
    
    char character; //Variable to store the incoming byte
    char msg[13];   //Message - array
    
    void setup()
    {
      Serial.begin(9600);
    }
    
    void loop()
    {
    
      while (Serial.available())
      {
        character = Serial.read();
        if (character == '<')
        {
          started = true;
          index = 0;
          msg[index] = '\0'; // Throw away any incomplete packet
        }
    
        //End the message when the '>' symbol is received
        else if (character == '>')
        {
          ended = true;
          break; // Done reading - exit from while loop!
        }
    
        //Read the message!
        else
        {
          if (index < 11)
          {                         // Make sure there is room
            msg[index] = character; // Add char to array
            index++;
            msg[index] = '\0'; // Add NULL to end
          }
        }
      }
    
      if (started && ended)
      {
        Serial.print("Message: ");
        Serial.println(msg);
    
        index = 0;
        msg[index] = '\0';
        started = false;
        ended = false;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-27
      • 2017-01-26
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-18
      相关资源
      最近更新 更多