Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have write code as follow

C++
#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();
    }
  }
}


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 )
. 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 )
Can anyone tell me how to do so?
Posted
Updated 4-Apr-15 9:42am
v4

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900