Zigbee / Xbee作为接收器和发送器 - 在接收器端丢失数据包

时间:2015-10-23 10:05:17

标签: arduino serial-port broadcast wireless zigbee

我的问题陈述非常简单。我有一个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

然而,在接收方,我得到一些丢失的数据包。就像

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端的任何数据包。

感谢您的建议,更正,评论!

2 个答案:

答案 0 :(得分:1)

首先,这句话是错误的:“请记住,两个arduinos上的波特率必须相同。”由于XBee模块可以缓冲您的请求,因此您可以在无线链路的每一端以不同的波特率运行。 XBee波特率仅决定主机和无线电模块之间串行连接的线速度。所有内容都以250kbps的速度“无线传输”。

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

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

答案 1 :(得分:0)

以下内容对初学者来说很有意思

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 );
    }
}
相关问题