Software Serial Problem

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
pinkfreak
 
Posts: 1
Joined: Wed Jun 13, 2012 11:18 pm

Software Serial Problem

Post by pinkfreak »

I'm currently doing a project where I interface a GPS module, digital compass and a ZigBee (XBee modem) to Arduino.

XBee currently uses the hardware UART and it is working fine.

The GPS module and the digital compass are both using Software Serial.

My problem is that if I instantiate both software serials, only the latest one will work.

Below is my sample code:

Code: Select all

void CSensor::initSensors()
{
    //for gps
    this->gpsSerial = new SoftwareSerial(2, 3);
    this->gpsSerial->begin(9600);

    //for digital compass
    this->compassSerial = new SoftwareSerial(6, 7);
    this->compassSerial->begin(9600);
}

gpsSerial is declared as a pointer member of CSensor class. -> SoftwareSerial* gpsSerial;
compassSerial is also declared the same -> SoftwareSerial* compassSerial;


In the above sample code, only the digital compass will work, the GPS will not work. But if I interchange the positions of the code such that it will look like this:

Code: Select all

void CSensor::initSensors()
{
    //for digital compass
    this->compassSerial = new SoftwareSerial(6, 7);
    this->compassSerial->begin(9600);

    //for gps
    this->gpsSerial = new SoftwareSerial(2, 3);
    this->gpsSerial->begin(9600);
}

The GPS will now work, but the digital compass won't. I assume Software Serial has a problem, what solution can I do? Also is there a limit to the number of software serial pins you could use?

User avatar
adafruit_support_bill
 
Posts: 88092
Joined: Sat Feb 07, 2009 10:11 am

Re: Software Serial Problem

Post by adafruit_support_bill »

From the reference page: http://arduino.cc/hu/Reference/SoftwareSerial
I believe you are running into the first limitation in the list:
The library has the following known limitations:

If using multiple software serial ports, only one can receive data at a time.
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
Not all pins on the Leonardo support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

mwtse
 
Posts: 8
Joined: Sun May 20, 2012 5:33 am

Re: Software Serial Problem

Post by mwtse »

Before you use each serial, activate it first:

this->gpsSerial->listen();
wait a short time, then
read your gps serial port
...
...
this ->compassSerial->listen();
wait a short time, then
read your compass.


It is described on Newsoft Serial page.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”