I am currently trying to to use two different serial ports on my arduino uno that is (2,3) for gsm and (8,9) for gps. I have tried both of this links;
1) arduino-uno-with-multiple-software-serial-devices
2) Two port receive using software serial on Arduino.
But nothing seems to be working. The serial monitor displayed nothing.
UPDATE
I changed the libraries of my gps and serial ports to AltSoftSerial and NeoSWSerial. Although both of them working just fine if I use it in the examples. But when I implement both of the libraries and run the code, there are no results in the serial monitor.
#include <NMEAGPS.h>
#include <GPSport.h>
#include <AltSoftSerial.h>
AltSoftSerial sim900a;
NMEAGPS gps; // This parses the GPS characters
gps_fix fix;
void setup()
{
Serial.begin(9600);
sim900a.begin(9600);
gpsPort.begin(9600);
}
void loop()
{
while (gps.available(gpsPort))
{
fix = gps.read();
if (fix.valid.location)
{
Serial.println();
Serial.print("Latitude= ");
Serial.print(fix.latitude(), 6);
Serial.print(" Longitude= ");
Serial.println(fix.longitude(), 6);
sendData = 1;
}
else
{
sendData = 0;
}
}
if(sendData == 1)
{
//do stuffs
delay(5000);
}
}
P/S: I have already tried listen() method to turn off and on each ports but it doesn't seem to be working as intended. Any helps are welcomed. Thank you.
You will not be able to use two software serial libraries to listen to the SIM900 and the GPS.
The main problem is that SIM900 characters can arrive at any time. They could arrive while you are listening to one software serial port.
The only way is to use the hardware serial port for one of the devices. I suggest connecting the GPS TX pin to the Arduino RX pin 0. Then you will read GPS characters on Serial
. You will have to disconnect pin 0 to upload new sketches over USB. You can still use Serial.print
to display information on the Serial Monitor window.
The SIM900 should be on pins 8 & 9, where you can use AltSoftSerial
. This is much more efficient than SoftwareSerial
. SoftwareSerial
disables interrupts for long periods of time, which can interfere with other parts of your sketch or with other libraries. Avoid it at all costs.
More detailed information in this answer and on my NeoGPS library Installation page: Choosing a Serial Port.
NeoGPS
and AltSoftSerial
are available from the Arduino IDE, under the menu Sketch -> Include Library -> Manage Libraries.