Zigbee/Xbee 作为接收器和发射器 - 接收器侧丢失数据包

问题描述 投票:0回答:2

我有一个 Arduino Uno 和另一个 Arduino Mega Board。两者都安装了 Zigbee Shield。其中一个用作发射器(Uno),另一个(Mega)用作接收器。

发送代码:

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

接收代码:

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

arduino serial-port broadcast wireless zigbee
2个回答
1
投票

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

其次,问题是您在接收器的 while 循环中有延迟。由于这种延迟,您每 200 毫秒只能处理一个字符,因此缓冲区溢出了。另一端每 400 毫秒发送 7 个字符 (

"HighLow"
),而您在那段时间内只处理其中的 2 个字符。

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


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 用户名/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 );
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.