GPS and GSM communication problem

I have a problem trying to communicate between my Arduino and GSM Sim900 module. Sometimes the GSM module acknowledges receipt of my message but at times it doesn't. I think there is a problem with the SoftwareSerial object but I'm not sure. Has anybody experienced a similar problem before and found a solution? Any suggestion?

#include <TinyGPS.h>
#include <SoftwareSerial.h>

// The TinyGPS++ object
TinyGPS gps;

// The serial connection to the GPS device
SoftwareSerial Serial_GPS(4, 3);  //RX, TX
SoftwareSerial Serial_GSM(6, 5);

float latitude, longtitude;

void setup() {
  Serial_GPS.begin(9600);
  Serial_GSM.begin(9600);
  Serial_GSM.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
}

void loop() {
  Serial_GPS.listen();
  delay(100);
  while (Serial_GPS.available() > 0) {
    int c = Serial_GPS.read();
    if (gps.encode(c)) {
      gps.f_get_position(&latitude, &longtitude);
    }
  }

  Serial_GSM.listen();
  delay(100);
  if (Serial_GSM.available() >  0) {
    String message = Serial_GSM.readString();
    message.trim();
    if (message.indexOf("LOCATION") >= 0) {
      Serial_GSM.println("AT+CMGF=1\r");
      delay(1000);
      /*Replace XXXXXXXXXX to 10 digit mobile number &
        ZZ to 2 digit country code*/
      Serial_GSM.println("AT+CMGS=\"+60178758275\"\r");
      delay(1000);
      //The text of the message to be sent.
      Serial_GSM.print("https://www.google.com/maps/@");
      Serial_GSM.print(latitude, 6);
      Serial_GSM.print(',');
      Serial_GSM.print(longtitude, 6);
      Serial_GSM.println(",14z");
      delay(1000);
      Serial_GSM.write(0x1A);
      delay(1000);
      Serial_GSM.println("AT+CNMI=2,2,0,0,0");
      delay(1000);
    }
  }
}

@quantum2_0 I have moved your question to a more suitable area of the forum.

You're obviously aware of the need to use listen for multiple instances of software serial. I expect that what's happening is that after you send to the GSM, you go back and listen to the GPS and anything the GSM sends during that time is lost. If you catch the GPS between transmissions, you may get back to the GSM in time to get a response.

This will be hard to solve without something with more hardware serial ports.

The delays, 100 mS and even 1000 mS look bad to me. No surprice that data get lost.

I’m trying to read data from a GSM(SIM900A) and a GPS(NEO-6M) using Teensy 4.0 with HardwareSerial. I’ve managed to read data from them individually, but I’ve not been able to get them to work when both modules are connected to the Teensy. Can anybody help me find any faults in my coding? Here’s the code:

#include <TinyGPS.h>
#define Serial_GPS Serial1
#define Serial_GSM Serial2
// The TinyGPS object
TinyGPS gps;

// The serial connection to the GPS device

float latitude, longitude;

void setup() {
  Serial.begin(9600);
  Serial_GPS.begin(9600);
  Serial_GSM.begin(9600);
  Serial_GSM.println("AT+CNMI=2,2,0,0,0");
}

void loop() {
  while (Serial_GPS.available() > 0) {
    int c = Serial_GPS.read();
    if (gps.encode(c)) {
      gps.f_get_position(&latitude, &longitude);
    }
  }

  if (Serial_GSM.available() >  0) {
    String message = Serial_GSM.readString();
    message.trim();
    if (message.indexOf("LOCATION") >= 0) {
      Serial.println("Testing");
      Serial_GSM.println("AT+CMGF=1\r");
      /*Replace XXXXXXXXXX to 10 digit mobile number &
        ZZ to 2 digit country code*/
      Serial_GSM.println("AT+CMGS=\"+60XXXXXXXXXX\"\r");
      //The text of the message to be sent.
      Serial_GSM.print("https://www.google.com/maps/@");
      Serial_GSM.print(latitude, 6);
      Serial_GSM.print(',');
      Serial_GSM.print(longitude, 6);
      Serial_GSM.println(",14z");
      Serial_GSM.write(0x1A);
      Serial_GSM.println("AT+CNMI=2,2,0,0,0");
    }
  }
}