【问题标题】:Zigbee/Xbee as Receiver and Transmitter - missing packets on receiver sideZigbee/Xbee 作为接收器和发送器 - 接收器端丢失数据包
【发布时间】:2015-10-23 10:05:16
【问题描述】:

我的问题陈述很简单。我有一个 Arduino Uno 和另一个 Arduino Mega Board。两者都安装了 Zigbee Shield。其中一个用作发射器(Uno),另一个(Mega)用作接收器。

Tx 代码:

void setup() {
    Serial.begin(9600);
}
void loop() {
    Serial.println("High"); 
    delay(200);
    Serial.println("Low");
    delay(200);
}

Rx 代码:

char msg;
const int led = 13; //led at pin 13
void setup() {
    Serial.begin(9600);//Remember that the baud must be the same on both arduinos
    pinMode(led,OUTPUT);
}
void loop() {
    while(Serial.available() ) {
        msg=Serial.read();
        if(msg=='H') {
            Serial.println("Message High");
        }
        if(msg=='L') {
            Serial.println("Message Low");
        }
        delay(200);
    }
}

在 Tx 端,它正在串行发送数据包 High Low

High
Low
High
Low
High
Low
High
Low
High
Low
High

但是在接收方,我丢失了一些数据包。就像

Message High
Message High
Message Low
Message High
Message High
Message High
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low

我希望它应该打印出来

Message High
Message Low
Message High
Message Low

如何同步接收数据包以及如何知道 Rx 端的任何数据包丢失。

感谢您的建议、指正、cmets!

【问题讨论】:

    标签: arduino serial-port broadcast wireless zigbee


    【解决方案1】:

    首先,这句话是错误的:“请记住,两个 arduino 的波特率必须相同。”您可以在无线链路的每一端以不同的波特率运行,因为 XBee 模块会缓冲您的请求。 XBee 波特率仅决定主机和无线电模块之间串行连接的线路速度。一切都以 250kbps 的速度“无线”发送。

    其次,问题在于接收器的 while 循环中有延迟。有了这个延迟,你只能每 200 毫秒处理一个字符,所以你的缓冲区溢出了。另一端每 400 毫秒发送 7 个字符 ("HighLow"),而您在这段时间内只处理其中 2 个。

    将延迟移到循环之外,你应该没问题。

    【讨论】:

      【解决方案2】:

      以下内容对初学者来说可能会很有趣

      Arduino 代码:传感器 + Xbee Tx

      //Reference: Sparkfun
      // Declaration of all necessary header file
      #include "Wire.h" 
      #include <SPI.h> 
      
      // Declarations of Parameters 
      int scale_ADXL337 = 3;  // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
      int scale_ADXL377 = 200;// 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
      float rawX_ADXL337, rawY_ADXL337, rawZ_ADXL337;  // Raw values for each axis of Sensor ADXL337
      float rawX_ADXL377, rawY_ADXL377, rawZ_ADXL377;  // Raw values for each axis of Sensor ADXL377
      float scaledX_ADXL337, scaledY_ADXL337, scaledZ_ADXL337; // Scaled values for each axis of Sensor ADXL337
      float scaledX_ADXL377, scaledY_ADXL377, scaledZ_ADXL377; // Scaled values for each axis of Sensor ADXL377
      
      boolean micro_is_5V = false; // Set to true if using a 5V microcontroller such as the Arduino Uno, false if using a 3.3V microcontroller, this affects the interpretation of the sensor data
      
      void setup()
      {
        // Initialize serial communication at 115200 baud
        Serial.begin(9600);
        //Serial.print("The Accelerometer ADXL377 and ADXL337 are connected to the MEGA BOARD");
        //Serial.println();
      }
      
      // Read, scale_ADXL337, and print accelerometer data
      void loop()
      {
        // Get raw accelerometer data for each axis
        rawX_ADXL377 = analogRead(A0);
        rawY_ADXL377 = analogRead(A1);
        rawZ_ADXL377 = analogRead(A2);
      
        rawX_ADXL337 = analogRead(A3);
        rawY_ADXL337 = analogRead(A4);
        rawZ_ADXL337 = analogRead(A5);
      
        scaledX_ADXL377 = mapf(rawX_ADXL377, 0, 1023, -scale_ADXL377, scale_ADXL377);
        scaledY_ADXL377 = mapf(rawY_ADXL377, 0, 1023, -scale_ADXL377, scale_ADXL377);
        scaledZ_ADXL377 = mapf(rawZ_ADXL377, 0, 1023, -scale_ADXL377, scale_ADXL377);
      
      
        scaledX_ADXL337 = mapf(rawX_ADXL337, 0, 1023, -scale_ADXL337, scale_ADXL337);
        scaledY_ADXL337 = mapf(rawY_ADXL337, 0, 1023, -scale_ADXL337, scale_ADXL337);
        scaledZ_ADXL337 = mapf(rawZ_ADXL337, 0, 1023, -scale_ADXL337, scale_ADXL337);
      
        Serial.println(rawX_ADXL337);Serial.println(rawY_ADXL337);Serial.println(rawZ_ADXL337);
        delay(200); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
        Serial.println(rawX_ADXL377);Serial.println(rawY_ADXL377);Serial.println(rawZ_ADXL377);
        delay(200); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
      
      }
      
      // Same functionality as Arduino's standard map function, except using floats
      float mapf(float x, float in_min, float in_max, float out_min, float out_max)
      {
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
      }
      

      接收器:在 Aduino + XBEE Explorer 上(tomlogic 建议)

      long msg;
      const int led = 13; //led at pin 13
      void setup() {
      Serial.begin(9600);//Remember that the baud must be the same on both arduinos
      pinMode(led,OUTPUT);
      }
      void loop() {
      while(Serial.available() ) {
                 msg=Serial.read();
      
                     //Serial.println(msg);
                      Serial.write(msg);
      //delay(200);
      }
      }
      

      Ubuntu 中 USB XBEE USB 上的接收器 Imp : chown username /dev/ttyS1 (在我的例子中是 ttyS1)

      //Source : http://ubuntuforums.org/archive/index.php/t-13667.html
      #include <fcntl.h>
      #include <stdio.h>
      #include <termios.h>
      #include <stdlib.h>
      #include <strings.h>
      
      /* Change to the baud rate of the port B2400, B9600, B19200, etc */
      #define SPEED B9600
      
      /* Change to the serial port you want to use /dev/ttyUSB0, /dev/ttyS0, etc. */
      #define PORT "/dev/ttyS1"
      
      int main( ){
          int fd = open( PORT, O_RDONLY | O_NOCTTY );
          if (fd <0) {perror(PORT); exit(-1); }
          struct termios options;
      
          bzero(&options, sizeof(options));
          options.c_cflag = SPEED | CS8 | CLOCAL | CREAD | IGNPAR;
          tcflush(fd, TCIFLUSH);
          tcsetattr(fd, TCSANOW, &options);
      
          int r;
          char buf[255];
          while( 1 ){
              r = read( fd, buf, 255 );
              buf[r]=0;
              printf( "%s", buf );
          }
      }
      

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 2017-08-20
      • 1970-01-01
      • 2012-10-03
      • 2012-05-20
      相关资源
      最近更新 更多