ElieNamias / xbee-arduino

Automatically exported from code.google.com/p/xbee-arduino
GNU General Public License v3.0
0 stars 0 forks source link

Request to allow Constructor to specify Serial Device #2

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
This a request to restore the old behavior where a Serial Device could be
specified when calling the Constructor.

What steps will reproduce the problem?
1. Latest version of code does not allow (or require) a serial device to be
specified when calling the constructor.
2. Older versions (before 0.1.5, I think) required that a serial device be
specified.

What is the expected output? What do you see instead?
What I'd like to have is two constructors; one that does not require
specification of a serial device, and one which allows (requires) such
specification. Then the user could choose the one best suited to their needs.

What version of the product are you using? On what operating system?
I'm using 0.2.1 with a constructor from an earlier version (0.1.2, I think)
that allows me to specify the serial device.

Please provide any additional information below.
The main reason I have for wanting to do this is that I am using an
Arduino-like device that has a USB port and supports it as the default
Serial device. Obviously, this won't work with an XBee. The device also has
a "normal" serial port and library routines to support it (perfect for
XBee), but the device has a different name (not "Serial"). To make use of
that port with the XBee library, I have to be able to specify the serial
device in the constructor.

Since there are other folks using similar devices and since the new Arduino
Maxis (name?) have multiple serial ports available, I believe this feature
would be generally useful.

The specific device I am using is detailed here:
http://www.pjrc.com/teensy/index.html

Original issue reported on code.google.com by jlar...@pacifier.com on 17 Jan 2010 at 10:15

GoogleCodeExporter commented 9 years ago
I changed XBee.h as:
#include <WProgram.h>
#include <inttypes.h>
#include <NewSoftSerial.h>
but I get a "file not found" error for NewSoftSerial
I changed Xbee.h as well here:

* \author Andrew Rapp
 */
class XBee {
public:
    XBee();
    XBee(rxpin, txpin);
    // for eclipse dev only
    void setSerial(HardwareSerial serial);
    void setSerial(NewSoftSerial serial);
but I get compiler error at XBee(rxpin, txpin);

I added line to XBee.h as in:
    uint8_t _responseFrameData[MAX_FRAME_DATA_SIZE];
    uint8_t _rxpin, _txpin;

I overloaded constructor in XBee.cpp as in:
#include "XBee.h"
#include "WProgram.h"
#include "HardwareSerial.h"
#include "NewSoftSerial.h"

XBee(uint8_t rxpin, uint8_t txpin)
{
_rxpin = rxpin;
_txpin = txpin;
}

I overloaded setserial in XBee.cpp:
void XBee::setSerial(NewSoftSerial serial) {
    Serial = serial(_rxpin, _txpin);
}

void XBee::setSerial(HardwareSerial serial) {
    Serial = serial;
}

I included NewSofSerial.h in XBee.cpp and added new constructor:

#include "XBee.h"
#include "WProgram.h"
#include "HardwareSerial.h"
#include "NewSoftSerial.h"

XBee(uint8_t rxpin, uint8_t txpin)
{
_rxpin = rxpin;
_txpin = txpin;
}

But I get a  few errors:

In file included from XBee.cpp:20:
/XBee.h:673: error: expected `)' before ',' token
XBee.cpp:25: error: expected `)' before 'rxpin'
XBee.cpp: In member function 'void XBee::setSerial(NewSoftSerial)':
XBee.cpp:695: error: no match for call to '(NewSoftSerial) (uint8_t&, uint8_t&)'
hardware\libraries\XBee/XBee.h:25:27: error: NewSoftSerial.h: No such file or 
directory

Can somebody please help - I really need this to work!
Reason: when the Nano3 (or Arduino) runs, it asks for initialization; if no new
initialization parameters are submitted within 5 seconds, it runs with old
parameters, so the Serial port (on USB) is needed; the XBee must run on 
NewSoftSerial.
My C++ is really rusty (I'm reading Stroustrup's original book as reference, 
that's
how long it's been.)

Original comment by maxm...@telus.net on 2 Apr 2010 at 5:03

GoogleCodeExporter commented 9 years ago
I've actually modified the library to accept a NewSoftSerial object in the XBee 
constructor.  I'd be happy to share the code, just let me know.

Original comment by gdr...@gmail.com on 9 Aug 2010 at 2:37

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Yes, please do!

Original comment by xdowboc@gmail.com on 2 Sep 2010 at 8:12

GoogleCodeExporter commented 9 years ago
I believe this is a solution to the problem.

http://groups.google.com/group/xbee-api/browse_frm/thread/93a6efd5cc52b134/3b801
913f6bd61d4?lnk=gst&q=setserial#3b801913f6bd61d4

BuddhaBandit has posted a modified library. Get it here.

http://groups.google.com/group/xbee-api/files

I used this code so I could use the serial1 UART on an Arduino Mega.

  xbee.begin(9600);
  Serial.begin(9600);
  Serial1.begin(9600);
  xbee.setSerial(Serial1);

This talks to the xbee with Serial1 and outputs to Serial (USB) for monitoring

Many thanks to BuddhaBandit

Original comment by robe...@ps.gen.nz on 3 Sep 2010 at 10:49

GoogleCodeExporter commented 9 years ago
Ironically enough, I just finished my own modified library a couple days ago.  
It looks like BuddhaBandit's modifications are primarily for using the multiple 
Serial ports of the Mega.  My own objective was to use API-mode XBee via 
NewSoftSerial on a ProMini.  It took a little empirical coding and verbal 
encouragement, but I managed to get it to work.  Once we're not on 
deadline-time, I'll look at posting that library and some additional 
documentation.

Original comment by xdowboc@gmail.com on 4 Sep 2010 at 9:36

GoogleCodeExporter commented 9 years ago
Hi - I've been monkeying with the library to use NSS using a couple different 
strategies but haven't made it happen. Anyone care to share a library where it 
is implemented, or hints on how the soution works? 

Original comment by Adam.von...@gmail.com on 1 Oct 2010 at 4:38

GoogleCodeExporter commented 9 years ago
Now that I'm back in the office, I will get the modified XBee library posted as 
soon as possible.  Depending on how much back-from-long-assignment paperwork I 
can dodge, it shouldn't take long.

Caveat: One condition that I did not yet code for is multiple-NSS concurrency.  
Since you can only "listen" to one NSS port at a time, two-way communication 
requires some extra effort.  Otherwise, you run the risk of missing a message 
while you're reading from another NSS device.  It didn't turn out to be an 
issue for our project, but I did scribble out some notes regarding possible 
ways to approach that question.  If you need, I'm happy to share what ideas I 
have.  Even the crazy ones.

Original comment by xdowboc@gmail.com on 4 Oct 2010 at 9:15

GoogleCodeExporter commented 9 years ago
@xdowboc ; Have you finished the modified the library yet? And could you post 
it?

Original comment by marijnoosterveld on 19 Oct 2010 at 8:23

GoogleCodeExporter commented 9 years ago
My apologies for the delay; I've been preparing for a cross-country move and 
had less time available than expected.  But the only part I haven't finished is 
the example code to go along with the library.
In the meantime, I have attached the library itself.  Usage should be very 
similar to that of Andrew Rapp's XBee library.  Operational variances are in 
instantiation and opening of the XBee port, which I have documented in the 
xBeeFG_readme.txt file.

The example code that I will eventually include contains the makings of a 
parallel library specific to API packet assembly.  It takes some of the 
complexity out of building API comm-packets and was instrumental in reducing 
team hair-loss due to failed packets.  It may be after the move before I can 
make sure it all compiles and tests out, but I will get that information up as 
soon as possible.

I hope this helps,
  --LC

Original comment by xdowboc@gmail.com on 19 Oct 2010 at 8:29

Attachments:

GoogleCodeExporter commented 9 years ago
xbowdoc, there is a bug in your xBeeFG. Sending Commands works, but receiving 
Messages won't work because in xBeeFG::readPacketNSS() you are checking 
Serial.available() and Serial.read() instead of NSSerial->available() and 
NSSerial->read().

I fixed this and now I'm able to receive data via NS.

Thanks four your modifications!
Tim

Original comment by tzielo...@googlemail.com on 27 Jan 2011 at 8:12

GoogleCodeExporter commented 9 years ago
Thanks both xdowboc and Tim. Just made my life much easier.

When can this be built back into the proper xbee-arduino library? Also 
interested in handling multiple NSS ports simultaneously, though only need one 
at the moment.

Original comment by nealtov...@yahoo.com on 20 Mar 2011 at 4:44

GoogleCodeExporter commented 9 years ago
Andrew Rapp already added support for NSS some weeks ago, but there is no new 
release yet. Check this out:

http://code.google.com/p/xbee-arduino/source/detail?r=22

Original comment by tzielo...@googlemail.com on 20 Mar 2011 at 5:00

GoogleCodeExporter commented 9 years ago
I've added the following methods so that the serial port can be specified, or 
NSS.  I haven't had chance to test so any help would be appreciated.   You 
would need to get the code from SVN, or download from 
http://code.google.com/p/xbee-arduino/source/browse/#svn%2Ftrunk.  Set USE_NSS 
in XBee.h to 1 for NSS.

    /**
     * Sets the Serial port for communication.
     */
    void setSerial(HardwareSerial &serial);
    /**
     * Tells the library to use NSS for communication, with the specified TX and RX pins
     */
    void setNss(NewSoftSerial &nssSerial);

I 

Original comment by andrew.rapp@gmail.com on 3 Apr 2011 at 2:48

GoogleCodeExporter commented 9 years ago
Is there any way this can be implemented in the official xbee library?   I just 
spent the last 3 hours figuring the issue out, of course I was using the 
"official" libraries.   By the way thank you everyone that worked in this fix!!

Original comment by chris91...@gmail.com on 5 Jul 2011 at 8:10

GoogleCodeExporter commented 9 years ago
Sure, just let me know it works!  My hardware has been tied up so haven't be 
able to test

Original comment by andrew.rapp@gmail.com on 13 Jul 2011 at 2:59

GoogleCodeExporter commented 9 years ago
Hy !

I would use a simply softwareserial to communicate with xbee and the hardware 
serial ti view on pc the text as usual....

How can I comunicate to Xbee instance to use, for example,
SoftwareSerial SerOne(Rxpin,TxPin);
??????

Any help will be very very appreciated...
Matt

Original comment by mattia.f...@gmail.com on 3 Oct 2011 at 10:20

GoogleCodeExporter commented 9 years ago
Sorry, but I try the code you suggest to use Serial for USB and Serial1 for xbee
Serial start to goes well, but when I start Serial1, Serial stop working also 
if I try to re-begin it.... any suggest??

this the code I use.... On Serial monitor I see only "Let's begin" and A :

void setup() {
  pinMode(10, OUTPUT);
  Serial.begin(57600);
  Serial1.begin(57600);

  Serial.println("Let's begin....");

 if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    return;
  }
  //carta ok, apri il file e poi inizializza Xbee
  File datafile = SD.open("test.txt", FILE_WRITE);
  Serial.println("A");

  xbee.setSerial(Serial1);

  Serial.println("C");
    if (datafile) {
       datafile.println("Carta OK, File Ok, Xbee Ok");
       Serial.println("Carta OK, File Ok, Xbee Ok");
     } 
 datafile.close();
 //aspetta che l'Xbee si associ
 delay(5000);

}

Original comment by mattia.f...@gmail.com on 7 Oct 2011 at 6:05

GoogleCodeExporter commented 9 years ago
for xbee work in (Serial1) i just change the libreries and replaces Serial for 
Serial1

regards

Original comment by faren...@gmail.com on 8 Feb 2012 at 2:19

GoogleCodeExporter commented 9 years ago
I use Arduino IDE 1.0 + XBee lib 0.3.

I have a similar problem to use the SoftwareSerial from an Arduino Uno. My code:

#include <SoftwareSerial.h>
SoftwareSerial Serial1(3/*TX*/,2/*RX*/);

then:
void setup()
{
  xbee.begin(9600);
  xbee.setSerial(Serial1);

I can't compile. I have the error: "error: no matching function for call to 
'XBee::setSerial(SoftwareSerial&)'"

Thans a lot.

Original comment by fxd...@gmail.com on 11 Feb 2012 at 7:36

GoogleCodeExporter commented 9 years ago
The answer ssems to be there: 
http://groups.google.com/group/xbee-api/browse_thread/thread/6dce1747a52164d9#

Original comment by fxd...@gmail.com on 12 Feb 2012 at 6:29

GoogleCodeExporter commented 9 years ago
I don't know if this will help anyone but I thought I'd put it up here anyway. 
I found this thread because I wanted to be able to print debug statements using 
Serial without conflicting with the Xbee. I figured hooking up the XBee using 
SoftwareSerial would be a good solution.
I ended up finding a work around that has been really useful. I ran the XBee 
off the hardware serial pins (0,1) and then I ran the debug statements through 
SoftwareSerial on pins 6 and 7 and then connected those to an empty XBee 
Explorer. I then was able to monitor my SoftwareSerial print statements using 
CoolTerm. I've actually started using this over regular Serial for debugging 
whenever possible because I got tired of opening the Serial Monitor each time I 
uploaded. CoolTerm just stays connected so your debug statements start pouring 
in as soon as you upload. It also keeps a history since it isn't reset on 
upload to the arduino. 
Here's a brief summary of how I put it together. I'm doing this from memory and 
lifting lines of code from a larger project so if you're having trouble please 
let me know.

-XBee hooked up to pins 0, 1
-XBee Explorer with no XBee hooked up to computer USB
-arduino pin 7 connected to XBee Explorer pin 2 (1-indexed, so right below VCC)
-arduino pin 6 connected to XBee Explorer pin 3 (below the connection above)
note: If all you're doing is printing debug statements you don't need the last 
step

#include <SoftwareSerial.h>
int8_t ssRX = 6;
uint8_t ssTX = 7;
SoftwareSerial debug(ssRX, ssTX);

XBee xbee = XBee();

void setup() {
    xbee.begin(9600);
    debug.begin(9600);
        debug.println("Arduino Reset");
}

void loop() {
// debug prints here...
}

Original comment by mme...@gmail.com on 12 Feb 2012 at 5:46

GoogleCodeExporter commented 9 years ago
The above poster has a point. Still, there are trade-offs on doing things 
either way. I think it's up to each one of us to choose, regarding the context 
of each implementation.
I also wish the XBee Library could support SoftwareSerial.

I believe the main trade-offs in play are these:
- It's logical, from a perspective of reliability, to choose Hardware Serial 
for interfacing with other devices, and Software Serial for 
debugging/monitoring.
- Doing so will prevent us from uploading new sketches on-the-fly, as we all 
know.
- It also requires additional hardware, which may become problematic in some 
cases.

Anyways, I couldn't possibly code such a library, so thanks for the great work 
so far :)

Original comment by domingue...@gmail.com on 11 Jun 2012 at 5:34

GoogleCodeExporter commented 9 years ago
Any word on when this will be updated to include SoftwareSerial?  Looks like 
there's a need.  What were the issues with the code from Andrew's post on 
4/2011?

Original comment by golczew...@gmail.com on 8 Sep 2012 at 9:10

GoogleCodeExporter commented 9 years ago
Andrew has uploaded a new experimental version, using a patch I wrote to enable 
using SoftwareSerial.

It needs beta testing.  Please, if you're interested in SoftwareSerial support, 
give it a try and report your results?

Original comment by paul.sto...@gmail.com on 16 Oct 2012 at 8:31

GoogleCodeExporter commented 9 years ago
Since version 0.4 beta, Software Serial is supported (thanks to Paul 
Stoffregen), so going to close this issue

Original comment by andrew.rapp@gmail.com on 2 Feb 2014 at 6:33