Loading...
Pages: [1]   Go Down
Author Topic: xBee.h doesn't compile with leonardo - Serial problem  (Read 767 times)
0 Members and 1 Guest are viewing this topic.
New Jersey
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to use xbee.h (http://code.google.com/p/xbee-arduino/) with the new Leonardo.  I get this error when I verify the sketch:
769: error: cannot convert 'Serial_*' to 'HardwareSerial*' in assignment

I assume it has something to do with the Leonardo not using serial by default and you have to use Serial1 or something, but I don't know what code to change to fix this.  Can anyone help?

--Scott
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 69
Posts: 3310
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Replace line 769 of XBee.cpp

Code:
_serial = &Serial;

with

Code:
_serial = &Serial1;

Logged

New Jersey
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That worked, thanks.   In the \libraries\ folder I created a new directory called \XBee_Leo and copied XBee.h and XBee.cpp in there and renamed them to XBeeLeo.h and XBeeLeo.cpp.  In the XBeeLeo.cpp I made the serial1 change and changed #include XBee.h to #include XBeeLeo.h.  In my sketch I just changed the include from <XBee.h> to <XbeeLeo.h> and all is good. 

It would be nice if one library could compile both Leonardo boards and non-Leonardo boards. I tried some stuff with #ifdef, but it didn't work.  Here's what I tried:
First I changed added a #define to my sketch:
Code:
#define LEONARDO
#include <XBee.h>
In XBee.cpp, I changed
Code:
_serial = &Serial;
to
Code:
#ifdef LEONARDO
   _serial = &Serial1;
#else
   _serial = &Serial;
#endif

But this didn't work.



Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 69
Posts: 3310
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This does not work because the defines and includes are rearranged before fed to the compiler.

But you can change your library this way and you don't have to do anything special. If you choose the Leonardo as the board type automatically the correct code is generated.

Code:
#ifdef defined(USBCON)
   _serial = &Serial1;
#else
   _serial = &Serial;
#endif
Logged

New Jersey
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I made the change and made sure I closed then re-opened the Arduino IDE, but my sketch didn't compile when I selected the Leonardo board.  I got this error:

/Volumes/srg/Arduino Sketches/libraries/XBee/XBee.cpp: In constructor 'XBee::XBee()':
/Volumes/srg/Arduino Sketches/libraries/XBee/XBee.cpp:774: error: cannot convert 'Serial_*' to 'HardwareSerial*' in assignment
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 69
Posts: 3310
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, my fault, had a copy/paste error in my posted code. Should be an #if and not an #ifdef:

Code:
#if defined(USBCON)
   _serial = &Serial1;
#else
   _serial = &Serial;
#endif
Logged

New Jersey
Offline Offline
Sr. Member
****
Karma: 0
Posts: 356
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That worked, thanks!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am having the same problem with the MICRO; I tried the fix described here but it does not work.

I am putting the

Code:
#if defined(USBCON)
   _serial = &Serial1;
#else
   _serial = &Serial;
#endif

at the beginning of the xbee.cpp file

is there something else I should do???

Bill
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 69
Posts: 3310
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to replace the line

Code:
_serial = &Serial;

with the code you posted. If you just add the code at the beginning it has absolutely no effect.
Logged

Pages: [1]   Go Up
Print
 
Jump to: