Go Down

Topic: Fio + Xbee communication and variables transfer (Read 1 time) previous topic - next topic

owar

Hi,

I need to transfer some variables(3bytes of data) over the XBee connected to Fio on one side and XBee connected to Uno on the other side.

I have tried the EasyTransfer libraryhttp://www.billporter.info/easytransfer-arduino-library/.But this has quite long response for my 4WD robot.

So I have written my code to send the variables.It is sending the Start byte(255) the ID byte(to identify which data I am sending) 3 bytes of data(3 variables 0-255) and two bytes of some check if the data was recieved correctly. It is working but only when I send this packet one per +-100ms which is even worse than the EasyTransfer.

What could be the problem?

Hardware:
Arduino Uno
Arduino Fio
XBee S2 with XB24-ZB firmware(Coordinator and end device)

Software:
TX side
Code: [Select]

int x=0;
int y=5;
int z=10;

int a=0;
int b=5;
int c=10;
int SB = 255;

void setup() {
  Serial.begin(9600);
}

void loop() {

//increase the values to simulate variables change
  x++;
  y++;
  z++;

  SEND(2,x,y,z); // send variables x,y,z with ID=2
  delay(100);
  SEND(3,a,b,c); // send variables a,b,c with ID=3
  delay(100);

}

void SEND(int ID, int A, int B, int C){

  //count the checksum
  int CB1 = (A+B+C)/255;
  int CB2 = (A+B+C)%255;

  Serial.write(SB);  // startbyte
  Serial.write(ID);  // ID byte
  Serial.write(A);   //data
  Serial.write(B);   //data
  Serial.write(C);   //data
  Serial.write(CB1); //checksum1
  Serial.write(CB2); //checksum2

}




RX side
Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial mySerial(5, 6); // RX, TX

int SB;
int ID;
int data1;
int data2;
int data3;
int CB1;
int CB2;

int a;
int b;
int c;
int x;
int y;
int z;

void setup() {

  mySerial.begin(9600); // for the XBee
  Serial.begin(9600); // for the PC terminal
}

void loop() {

  if(mySerial.available()==7){ //check if 7 bytes are available
    serialread();         
    setparameters(ID);

  }
}

void serialread(){
 
  //read all bytes
  SB = mySerial.read();
  ID = mySerial.read();
  data1 = mySerial.read();
  data2 = mySerial.read();
  data3 = mySerial.read();
  CB1 = mySerial.read();
  CB2 = mySerial.read();


}

boolean check(){
 
  int crc1 = data1+data2+data3; // count the "crc"
  int crc2 = (CB1*255)+CB2;     

  if(crc1==crc2){               // if "crc" is the same return true
    return true;
  }
  else{
    return false;
  }
}

void setparameters(int ID){
  if(check()){                  // if "crc" is true set the
    if(ID==2){                  // recieved data to local variables
      x = data1;                // for each ID
      y = data2;
      z = data3;
    }

    if(ID==3){
      a = data1;
      b = data2;
      c = data3;
    }
    printout();               // print data to terminal
  }
  else
    RESET();                  // if "crc" is false
}

void printout(){

  Serial.println("ABC");
  Serial.println(a);
  Serial.println(b);
  Serial.println(c);
  Serial.println(" ");

  Serial.println("xyz");
  Serial.println(x);
  Serial.println(y);
  Serial.println(z);
  Serial.println(" ");
  Serial.println(" ");
  Serial.println(" ");
}

void RESET(){                  //set all local variables to zero
  a = b = c = x = y = z = 0;
}



I hope someone will find where is the problem.Thank you very much for every input :)

PaulS

Sending an int using Serial.write() is not a good idea. How many bytes does it actually send?

Code: [Select]
  if(mySerial.available()==7){ //check if 7 bytes are available
What happens if there are ever more than 7 bytes? You will never read another thing.

You are doing a lot of printing at a really slow speed. Why is the Serial baud rate so low?


owar

Sorry that is an old code.(but that worked same as the new one) The new one doesnt have integers but bytes.The Serial speed is 9600 because later I want to use XBee 900 XSC and it cannot work on higher rates.

PaulS

Quote
The Serial speed is 9600 because later I want to use XBee 900 XSC and it cannot work on higher rates.

The baud rate that the Arduino talks, via the USB cable, to the PC has nothing to do with the baud rate that the XBee uses to talk to the Arduino.

Do you have a link to the slow XBees you are talking about?

owar

#4
Oct 08, 2012, 03:29 pm Last Edit: Oct 08, 2012, 03:38 pm by owar Reason: 1
here is the link for the XBee XSC https://www.sparkfun.com/products/9085

I have changed the baud rate for Serial on higher rate(115200) and the result is still same.I cannot get under some 100ms. It is 50ms delay now, but I think that it could be something like 10ms or less...


Go Up