我想知道如何知道有多少字节Xbee的Zigbee已经发送,以便我可以接收串行通信 [英] I Wants To Ask How To Know How Many Bytes Xbee Of Zigbee Has Sent So That I Can Receive On Serial Communication

查看:97
本文介绍了我想知道如何知道有多少字节Xbee的Zigbee已经发送,以便我可以接收串行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写的代码如下



I have write code as follow

#include <SoftwareSerial.h>
SoftwareSerial softSerial(10, 11); // RX, TX

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  softSerial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  at_command(01,'S','L',"");
  if(softSerial.available()){
    Serial.print(softSerial.read());
  }
}

// Sending AT command to Connected radio
void at_command(char id, char c1, char c2, char *cmdData) {
  // Start delimiter
  softSerial.write(0x7E);    // [Fixed]
  // paylode length [Depend on length of frame minimum 5]
  softSerial.write((byte)0x00);    // MSB
  softSerial.write(0x05);    // LSB
    // Frame specifid data (Pay load)
    softSerial.write(0x88);  // Frame type [Fixed]
    softSerial.write(id);    // Frame ID [Unique ID to veryfy response. if =0 will not get any response]
    // AT Command
    softSerial.write(c1);    // First Charecter
    softSerial.write(c2);    // Second Charecter
    // Command data
    long cmdDataSum = 0;
    if(sizeof(cmdData) > 0) {
      for(int i=0; i<= sizeof(cmdData)-1; i++) {
        softSerial.write(i);  // [maximum 256 charecters]
        cmdDataSum = cmdDataSum + i;
      }
    } else { // do nothing
      ;
    }
  // Checksum
  long sum = 0x88 + id + c1 + c2 + cmdDataSum;
  softSerial.write(0xFF - (sum - 0xFF));
  // just for a sake of guard
  delay(10);
}

// Receiving AT command response from Radio
void at_response(){
  // Received AT comman responce from Coordinator
  if(Serial.available() > 21 ) {
    if(Serial.read() == 0x7E) {
      for (int i=0; i<19; i++) {
        byte discard = Serial.read();
      }
      byte atResponse = Serial.read();
    }
  }
}





现在假设我将向XBee发送一些命令返回AT命令响应以读取我必须使用循环,就像我在at_response()函数中所做的一样,并将我正在读取的值作为检查,如



Now suppose if i will send some command to XBee that will return AT Command Response to read which i have to use a loop as i have done in at_response() function along with putting values i am reading as check as in

if(Serial.available() > 21 )

。如果XBee发送一些可变长度的响应并且我必须阅读它会怎么样?



我希望问题很清楚!让我再次解释一下,我想通过Zigbee在串行上读取一些可变长度响应,因为我通过

. What if XBee send some variable length response and i have to read that?

I hope question is clear! let me explain once more that i want to read some variable length response through Zigbee on serial as i have done with fixed length response by

if(Serial.available() > 21 )

谁能告诉我怎么做?

推荐答案

请参阅 http: //arduino.cc/en/Reference/SoftwareSerialAvailable [ ^ ]。 available 函数返回的值返回当前在端口上接收的字符数。但它无法判断远程设备可以准备发送多少。您需要阅读设备的文档以了解它的运行方式。
See http://arduino.cc/en/Reference/SoftwareSerialAvailable[^]. The value returned by the available function returns the number of characters currently received on the port. But it cannot tell how many more that the remote device may be ready to send. You need to read the documentation for the device to see how it operates.


这篇关于我想知道如何知道有多少字节Xbee的Zigbee已经发送,以便我可以接收串行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆