Go Down

Topic: xbee communication not working with class (Read 581 times) previous topic - next topic

gabriel42

hi,
I am building a robot and am incorporating xbees. I wrote a simple echo program to test them and it worked perfectly:

Code: [Select]

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

void loop()  {
if(Serial.available() > 0)  {
   char x[1];
   Serial.readBytes(x, 1);
   Serial.println(x[0]);
   }
  }


but when i tried to do the same thing using a class I am writing, it didn't work:

Code: [Select]

/*******************************
class to send and receive
messages using an xbee. the
send and recieve data types
are the ones i need for this
project. for general use, this
should be a template class.
*******************************/

class Zigbee  {
  public:
    // constructor takes starting serial speed
    Zigbee(int speed = 9600);
   
    // send a char
    void send(char message);
   
    // receive an char
    char receive();
  };

// constructor takes starting serial speed 
Zigbee::Zigbee(int speed)  {
  Serial.begin(speed);
  }
 
// send char
void Zigbee::send(char message)  {
  Serial.println(message);
  }

// wait until an char is received and return it
char Zigbee::receive()  {
  //while(Serial.available() < 1); // wait until there is data
  char message[1];
  Serial.readBytes(message, 1); // stores first char in message
  return message[0];
  }

Zigbee Xbee(9600);

void setup()  {}

void loop()  {
  Xbee.send(42);
  delay(100);
  }


Is there something obvious I'm missing? I would really appreciate help.

Thank you!

PaulS

Code: [Select]
   char x[1];
Of what use is a one element array over a scalar variable?

Quote
Is there something obvious I'm missing?

A call the the receive method.

gabriel42

thanks for the suggestions. i changed Zigbee::receive() and loop() but this still doesn't work:

Code: [Select]

/*******************************
class to send and receive
messages using an xbee. the
send and recieve data types
are the ones i need for this
project. for general use, this
should be a template class.
*******************************/

class Zigbee  {
  public:
    // constructor takes starting serial speed
    Zigbee(int speed = 9600);
   
    // send a char
    void send(char message);
   
    // receive an char
    char receive();
  };

// constructor takes starting serial speed 
Zigbee::Zigbee(int speed)  {
  Serial.begin(speed);
  }
 
// send char
void Zigbee::send(char message)  {
  Serial.println(message);
  }

// wait until an char is received and return it
char Zigbee::receive()  {
  //while(Serial.available() < 1); // wait until there is data
  char* message;
  Serial.readBytes(message, 1); // stores first char in message
  return *message;
  }

Zigbee Xbee(9600);

void setup()  {}

void loop()  {
  Xbee.send(Xbee.receive());
  }

PaulS

Code: [Select]
  char* message;
  Serial.readBytes(message, 1); // stores first char in message
  return *message;

Create a pointer pointing nowhere. Write some data there. Send back that garbage.

I'm not surprised that it "didn't work" for some definition of work.

Why is message a pointer? It should not be.

gabriel42

Quote
Create a pointer pointing nowhere. Write some data there. Send back that garbage.

could you show me some example code?

Quote
I'm not surprised that it "didn't work" for some definition of work.

my definition of not work is that it compiles but does nothing when uploaded onto my arduino.

Quote
Why is message a pointer? It should not be.

My version(1.0.1) says that you have to pass an array or pointer to readBytes

gabriel42

problem solved. I turns out that it didn't work because I tried to start the Serial before arduino called init(), which is apparently where all those functions and classes are initialized. Thanks for trying to help me

Go Up