Unable to work with NEOGPS with Hardware Serial

Currently, I've tried NeoSWserial or hardware serial with my gps module.
i can run the example succeed, but when i apply it in my code, the arduino can't detect the serial input.

and reference the code similar to:
No data received from two different Serial Port Arduino Uno - Stack Overflow

#include <NMEAGPS.h>
#include <GPSport.h>

NMEAGPS gps;
gps_fix fix;

int L = 13;

void setup()
{
    Serial.begin(9600);
    gpsPort.begin(9600);
}

void loop()
{
	int sendData;
	if (gps.available(gpsPort))
	{
		fix = gpsPort.read();

		if (fix.valid.location){
			Serial.println();
			Serial.println(F("GPS Signal found\n\n"));

			Serial.print("Latitude= "); 
			Serial.print(fix.latitude(), 6);
			
			Serial.print("Longitude= "); 
			Serial.println(fix.longitude(), 6);
			
			Serial.print("Speed= "); 
			Serial.println(fix.speed_kph(), 6);
			sendData = 1;
		}else
			Serial.println(F("GPS Signal not found"));
			sendData = 0;
	} else 
		Serial.println(F("GPS module not working"));
	if( sendData == 1 ){
		//do stuffs
		Serial.println(F("Test completed"));
		delay(500); 
    }
	delay(3000);
}

Welcome to the Forum!

So that someone can help you, please show exactly your code which doesn't work rather than somebody's "similar" one.

It seems to me that the link is a completely different case and a different problem

thank you for replied,

to specify my problem is that the gps.available(gpsPort) always return 0,
which is not supposed to be.
it is because the example run flawlessly, but when i use the code posted above it's not working.

the full code here.

#include<EEPROM.h>
#include<SPIMemory.h>
#include <NMEAGPS.h>
#include <GPSport.h>
#include <AltSoftSerial.h> // this library default pin 8,9 as Tx and Rx, pin 10 would be disabled

AltSoftSerial nbiot;  //set the pin connection

NMEAGPS gps; // use hardware seriakl for gps module, for further config, please check NeoGPS library online
gps_fix fix;

String DataToSend="";

SPIFlash flash;
int L = 13; //LED pin for indication

#define latest_addr 12                  //eeprom addr to store last flash addr
//#define gpsPort Serial

String result = ""; //Variable to decided loop continue or not, check NBIoT serial input
//String SN=""; //SN to define the device, useless, 15 numbers

void gpsRead() {
	Serial.println(F("GPS data operation"));
	if (gpsPort.available()){
		fix = gps.read();
		if (fix.valid.location){
			Serial.println();
			Serial.println(F("GPS Signal found\n"));
		
			Serial.println(F("Latitude= ")); 
			Serial.println(fix.latitude(), 6);
			Serial.println(F("Longitude= ")); 
			Serial.println(fix.longitude(), 6);
			Serial.println(F("Speed= ")); 
			Serial.println(fix.speed_kph(), 6);
			delay(1500);
			Status = 0;
			DataToSend = "Status: " + String(Status) + "\nLatitude:" + String(fix.latitude(), 6) + "\nLongitude:" + String(fix.longitude(), 6) + "\nSpeed:" + String(fix.speed_kph(), 6);
		}else{
			Serial.println(F("GPS Signal not found"));
			delay(1500);
			Data = "Status: " + String(Status) + "\nGPS Signal not found";
			Status = 1;
		}
	}else{
			Serial.println(F("GPS Module not working"));
			delay(1500);
			Data = "Status: " + String(Status) + "\nGPS Module not working";
			Status = 1;
	}
	Serial.println(Data);
 return Data;
}

void setup()  //初始化内容
{
	Serial.begin(9600);
	delay(500);
	
	gpsPort.begin(9600);
	delay(500);
	
	nbiot.begin(9600);
	delay(500);
	
	nbiot_initial(); //AT command to confirm module working
	delay(500);

	flash.begin();                      //auto detect by library
	delay(100);
	if (flash.error()) {
		Serial.println(flash.error(VERBOSE));
    //  flash init fail upload
		Status = 3;
    } else {
		Serial.println(F("Flash is ready!"));
    }

    EEPROM.get(latest_addr, current_addr);
    Serial.print(F("lt addr")); Serial.print(F(": ")); Serial.println(current_addr);
    flash.eraseSector(current_addr);
    while(!Serial){
    }
}

void loop()   //主循环
{
	int socket_no = 0;//keep the return socket value
	Status = 0;
	gpsRead();  //获取GPS数据
	Serial.println(DataToSend);
	delay(500);
	if ( Status == 0 )
		//nbiot operation
	}
	delay(2500);
}

What is arduino board you are using?

arduino pro mini, have 1 serial port only, version 3.3V 8MHz,
two module parts, gps and nbiot.
tried with altsoftserial and hardware serial, or using NeoSWSerial with altsoftserial,
having same result:

gps.available(gpsPort)always return 0,
if i changed it to gpsPort.available(), it work, but the NEOGPS not parsing the data.

edit: both, work at baud rate 9600

please, let's talk about the only one code... for example about the code from post#3

First, the line

#include <AltSoftSerial.h>

must be in the code before this one:

#include <GPSport.h>

Next, the AltSoftSerial is works on predefined pins - 8 & 9 as default. Are you connected your GPS to these pins?

no, altsoftserial used for the NB-IoT module, and I connected the Gps to the Hardware Serial,

From the file GPSport_h:

If you include <SoftwareSerial.h>, <NeoSWSerial.h>, <AltSoftSerial.h>,
// <NeoICSerial.h> OR <NeoHWSerial.h> before this file (in the INO)

You include the serial port file after GPSport.h.

Please read the manual....
From the comments at beginning of gpsPort.h file:

// If you know what port you want to use,
// *DELETE* the rest of this file and declare your own GPS port variable,
//  GPS port name string, and debug print port variable (see above).

Thus you must to remove all contents from file gpsPort.h file and replace it with three lines below:

#define gpsPort Serial
#define GPS_PORT_NAME "Serial"
#define DEBUG_PORT Serial 

I've tried configured in both method, but not working

What are the "both methods"?
Please show your code, how do you configured it.

method 1:

#ifndef GPSport_h
#define GPSport_h

//  Copyright (C) 2014-2017, SlashDevin
//
//  This file is part of NeoGPS
//
//  NeoGPS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  NeoGPS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with NeoGPS.  If not, see <http://www.gnu.org/licenses/>.

//-----------------------------------------------------------
//  There are 2 ways this file can be used:
//
//  I.  AS IS, which tries to *guess* which port a beginner should use.
//      If you include <SoftwareSerial.h>, <NeoSWSerial.h>, <AltSoftSerial.h>, 
//      <NeoICSerial.h>  *OR*  <NeoHWSerial.h> before this file (in the INO) 
//      or on line 152 below, one of those ports will be used for the GPS.  
//      This file cannot always guess what you want, so you may need to use it 
//      in the 2nd way...
//
//  II. *REPLACE EVERYTHING* in this file to *specify* which port and 
//      which library you want to use for that port.  Just declare the
//      port here.  You must declare three things: 
//
//         1) the "gpsPort" variable (used by all example programs),
//         2) the string for the GPS_PORT_NAME (displayed by all example programs), and
//         3) the DEBUG_PORT to use for Serial Monitor print messages (usually Serial).
//
//      The examples will display the device you have selected during setup():
//
//         DEBUG_PORT.println( F("Looking for GPS device on " GPS_PORT_NAME) );
//
//      Choosing the serial port connection for your GPS device is probably
//      the most important design decision, because it has a tremendous impact
//      on the processor load.  Here are the possible configurations, 
//      from BEST to WORST:
//
// ---  The BEST choice is to use a HardwareSerial port. You could use Serial 
//      on any board, but you will have to disconnect the Arduino RX pin 0 
//      from the GPS TX pin to upload a new sketch over USB.  This is a very 
//      reliable connection:
//
//             #define gpsPort Serial
//             #define GPS_PORT_NAME "Serial"
//             #define DEBUG_PORT Serial
//
//      If you have a Mega, Leo or Due board, you could use Serial1, 
//      Serial2 or Serial3:
//
//             #define gpsPort Serial1
//             #define GPS_PORT_NAME "Serial1"
//             #define DEBUG_PORT Serial
//
//      These extra ports do not have to be disconnected to upload new
//      sketches over USB.
//
//      Use NeoHWSerial if you want to handle GPS characters
//      in an Interrupt Service Routine (see NMEA_isr.INO).
//      Also uncomment NMEAGPS_INTERRUPT_PROCESSING in NMEAGPS_cfg.h.
//
//             #include <NeoHWSerial.h>
//             #define gpsPort NeoSerial
//             #define GPS_PORT_NAME "NeoSerial"
//             #define DEBUG_PORT NeoSerial
//
//      Note that the DEBUG_PORT must be NeoSerial, not Serial
//      (see NeoHWSerial documentation for additional requirements).
//
// ---  The SECOND BEST choice is AltSoftSerial.  It uses 10x to 500x
//      more processor time than a HardwareSerial port, but it is the BEST
//      *software* serial port library.  It only works on two specific pins
//      (see the AltSoftSerial documentation).
//
//             #include <AltSoftSerial.h>
//             AltSoftSerial gpsPort; // 8 & 9 for an UNO
//             #define GPS_PORT_NAME "AltSoftSerial"
//             #define DEBUG_PORT Serial
//
//      Use NeoICSerial if you want to handle GPS characters
//      in an Interrupt Service Routine (see NMEA_isr.INO).
//      Also uncomment NMEAGPS_INTERRUPT_PROCESSING in NMEAGPS_cfg.h.
//
//             #include <NeoICSerial.h>
//             NeoICSerial gpsPort; // 8 & 9 for an UNO
//             #define GPS_PORT_NAME "NeoICSerial"
//             #define DEBUG_PORT Serial
//
// ---  The THIRD BEST choice is NeoSWSerial.  It is almost as efficient as
//      AltSoftSerial, but it only supports baud rates 9600, 19200 or 38400.
//
//             #include <NeoSWSerial.h>
//             NeoSWSerial gpsPort(3, 2);
//             #define GPS_PORT_NAME "NeoSWSerial(3,2)"
//             #define DEBUG_PORT Serial
//
//      NeoSWSerial supports handling GPS characters in an Interrupt Service
//      Routine (see NMEA_isr.INO).  If you want to do that, also uncomment 
//      NMEAGPS_INTERRUPT_PROCESSING in NMEAGPS_cfg.h.
//
// ---  The WORST choice is SoftwareSerial.  IT IS NOT RECOMMENDED because
//      it disables interrupts for the entire time a character is sent OR
//      received.  This interferes with other parts of your sketch or with
//      other libraries.  It prevents receiving or sending data on any other 
//      port.  It cannot transmit and receive at the same time.  95% of the
//      CPU time will be spent twiddling its thumbs.  It may support 57600,
//      but 115200 is rarely reliable.
//
//             #include <SoftwareSerial.h>
//             SoftwareSerial gpsPort(2,3);
//             #define GPS_PORT_NAME "SoftwareSerial(8,9)"
//             #define DEBUG_PORT Serial
//
//      You should *seriously* consider using other pins or lower baud rates, 
//      so that you could use one of the other recommended ports.
//-----------------------------------------------------------

// DEFAULT file contents:
//   *GUESS* which port should be used.  If you know what port you want to use,
//             *DELETE* the rest of this file and declare your own GPS port variable,
//             GPS port name string, and debug print port variable (see above).

#if defined(SERIAL_PORT_HARDWARE_OPEN)

  #if SERIAL_PORT_HARDWARE_OPEN == Serial1

    //  This Arduino has more than one hardware serial port, 
    //      use Serial1 (or NeoSerial1).
    #define NEOGPS_USE_SERIAL1

  #endif

#endif

#ifdef NEOGPS_USE_SERIAL1

  #if defined( NMEAGPS_INTERRUPT_PROCESSING )
    #include <NeoHWSerial.h>
    #define gpsPort NeoSerial1
    #define GPS_PORT_NAME "NeoSerial1"
  #else
    #define gpsPort Serial1
    #define GPS_PORT_NAME "Serial1"
  #endif

#else
  //      Uncomment *zero* or *one* of the serial port library includes.
  //      If none of these includes are uncommented, Serial will be used.
  //
  //#include <NeoHWSerial.h>    // NeoSerial or NeoSerial1 Interrupt-style processing
  //#include <AltSoftSerial.h>    // <-- DEFAULT.  Two specific pins required
  //#include <NeoICSerial.h>    // AltSoftSerial with Interrupt-style processing (see docs)
  //#include <NeoSWSerial.h>    // Any pins, only @ 9600, 19200 or 38400 baud
  //#include <SoftwareSerial.h> // NOT RECOMMENDED!

  #if defined( SoftwareSerial_h )
    #define SS_TYPE SoftwareSerial
    #warning SoftwareSerial is NOT RECOMMENDED.  Use AltSoftSerial or NeoSWSerial instead.

    #if defined( NMEAGPS_INTERRUPT_PROCESSING )
      #error You cannot use SoftwareSerial with INTERRUPT_PROCESSING.  Use NeoSWSerial or NeoICSerial instead.
    #endif

  #elif defined( NeoSWSerial_h )
    #define SS_TYPE NeoSWSerial

  #elif defined( AltSoftSerial_h )
    #define SS_TYPE AltSoftSerial
    #define RX_PIN -1  // doesn't matter because it only works...
    #define TX_PIN -1  //    ...on two specific pins

    #if defined( NMEAGPS_INTERRUPT_PROCESSING )
      #error You cannot use AltSoftSerial with INTERRUPT_PROCESSING.  Use NeoICSerial instead.
    #endif

  #elif defined( NeoICSerial_h )
    #define SS_TYPE NeoICSerial
    #define RX_PIN -1  // doesn't matter because it only works...
    #define TX_PIN -1  //    ...on two specific pins

  #elif defined( NeoHWSerial_h )
    #define gpsPort NeoSerial
    #define GPS_PORT_NAME "NeoSerial"
    #warning Using Serial (actually NeoSerial) for GPS connection.

  #else
    // No serial library files were included before this file, just use Serial.
    #define gpsPort Serial
    #define GPS_PORT_NAME "Serial"
    #warning Using Serial for GPS connection.
    
    // You will see this warning if you want to use Serial for the GPS, because no
    // software serial port libraries were included.  That is a good choice.
    //
    // To use a different serial port for GPS device, you must include a serial header 
    // before "#include GPSport.h" in the INO.  It may be simpler to replace the 
    // entire contents of this file with your own declarations.  Follow the
    // instructions above for declaring your own "gpsPort" variable. Everything else
    // in this file should be deleted. See Installation instructions for more
    // information.
  #endif
#endif


#ifdef SS_TYPE

  //---------------------------------------------------------------
  // The current Board (an Uno?) does not have an extra serial port.
  // Use a software serial library to listen to the GPS device.
  //   You should expect to get some RX errors, which may
  //   prevent getting fix data every second.  YMMV.

  // Default Arduino RX pin number that is connected to the GPS TX pin
  #ifndef RX_PIN
    #define RX_PIN 4
  #endif

  // Default Arduino TX pin number that is connected to the GPS RX pin
  #ifndef TX_PIN
    #define TX_PIN 3
  #endif

  SS_TYPE gpsPort( RX_PIN, TX_PIN );

  //  A little preprocessor magic to get a nice string
  #define xstr(x) str(x)
  #define str(x) #x
  #define GPS_PORT_NAME \
    xstr(SS_TYPE) "( RX pin " xstr(RX_PIN) \
                  ", TX pin " xstr(TX_PIN) " )"

  #ifdef NEOGPS_USE_SERIAL1
    //  If you *really* want to do this, or you just happened to include 
    //  a software serial library header for something else, you're
    //  better off *not* including this file.  Just declare
    //  your own gpsPort in your INO file.

    #error You should be using Serial1 for the GPS device.  \
        Software serial libraries are very inefficient and unreliable when \
        used for GPS communications!
  #endif

#endif

//------------------------------------------------------------
// When NeoHWSerial is used, none of the built-in HardwareSerial
//   variables can be used: Serial, Serial1, Serial2 and Serial3
//   *cannot* be used.  Instead, you must use the corresponding
//   NeoSerial, NeoSerial1, NeoSerial2 or NeoSerial3.  This define
//   is used to substitute the appropriate Serial variable in
//   all debug prints.

#ifdef NeoHWSerial_h
  #define DEBUG_PORT NeoSerial
#else
  #define DEBUG_PORT Serial // default for most sketches
#endif

// End of guessing game.
//------------------------

#endif

method 2:

#ifndef GPSport_h
#define GPSport_h

//  Copyright (C) 2014-2017, SlashDevin
//
//  This file is part of NeoGPS
//
//  NeoGPS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  NeoGPS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with NeoGPS.  If not, see <http://www.gnu.org/licenses/>.

//-----------------------------------------------------------
//  There are 2 ways this file can be used:
//
//  I.  AS IS, which tries to *guess* which port a beginner should use.
//      If you include <SoftwareSerial.h>, <NeoSWSerial.h>, <AltSoftSerial.h>, 
//      <NeoICSerial.h>  *OR*  <NeoHWSerial.h> before this file (in the INO) 
//      or on line 152 below, one of those ports will be used for the GPS.  
//      This file cannot always guess what you want, so you may need to use it 
//      in the 2nd way...
//
//  II. *REPLACE EVERYTHING* in this file to *specify* which port and 
//      which library you want to use for that port.  Just declare the
//      port here.  You must declare three things: 
//
//         1) the "gpsPort" variable (used by all example programs),
//         2) the string for the GPS_PORT_NAME (displayed by all example programs), and
//         3) the DEBUG_PORT to use for Serial Monitor print messages (usually Serial).
//
//      The examples will display the device you have selected during setup():
//
//         DEBUG_PORT.println( F("Looking for GPS device on " GPS_PORT_NAME) );
//
//      Choosing the serial port connection for your GPS device is probably
//      the most important design decision, because it has a tremendous impact
//      on the processor load.  Here are the possible configurations, 
//      from BEST to WORST:
//

#define gpsPort Serial
#define GPS_PORT_NAME "Serial"
#define DEBUG_PORT Serial

#endif

so, right now i'm using NeoSWSerial.
the gpsPort.h is configured to:

#ifndef GPSport_h
#define GPSport_h

//  Copyright (C) 2014-2017, SlashDevin
//
//  This file is part of NeoGPS
//
//  NeoGPS is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  NeoGPS is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with NeoGPS.  If not, see <http://www.gnu.org/licenses/>.

//-----------------------------------------------------------

#include <NeoSWSerial.h>
NeoSWSerial gpsPort(4, 3);
#define GPS_PORT_NAME "NeoSWSerial(4,3)"
#define DEBUG_PORT Serial

#endif

the gps section testing Program:

#include <NeoSWSerial.h>
#include <NMEAGPS.h>
#include <GPSport.h>

NMEAGPS  gps; // This parses the GPS characters
gps_fix  fix;

void setup()
{
  Serial.begin(9600);
  gpsPort.begin(9600);
  delay(500);
}

void loop()
{
  int sendData;
  while (gps.available(gpsPort))
  {
    Serial.println(F("Test"));
    fix = gps.read();

   if (fix.valid.location)
    {
      Serial.println(F("Succeed"));
      Serial.println();

      Serial.println(F("Latitude= ")); 
      Serial.println(fix.latitude(), 6);
      Serial.println(F(" Longitude= ")); 
      Serial.println(fix.longitude(), 6);
      sendData = 1;
      
    }
    else
    {
      Serial.println(F("Not valid result"));
      sendData = 0;
    }
    delay(2500);
  }
  if( gpsPort.available() <= 0)
  {
    Serial.println(F("Fail"));
  }
  delay(2500);
}

the result from comport:

20:09:01.947 -> Fail
20:09:06.942 -> Fail
20:09:09.483 -> Fail
20:09:11.983 -> Fail
20:09:14.519 -> Fail
20:09:17.017 -> Fail
20:09:19.545 -> Fail
20:09:22.045 -> Test
20:09:22.045 -> Not valid result
20:09:24.550 -> Fail
20:09:27.058 -> Fail
20:09:29.585 -> Fail
20:09:34.595 -> Fail
20:09:37.102 -> Fail
20:09:39.620 -> Fail
20:09:42.146 -> Fail
20:09:44.642 -> Fail
20:09:49.685 -> Fail
20:09:52.205 -> Fail
20:09:54.681 -> Fail
20:09:59.734 -> Fail
20:10:02.244 -> Fail
20:10:04.754 -> Fail
20:10:07.252 -> Fail
20:10:09.764 -> Fail
20:10:12.285 -> Fail
20:10:14.780 -> Fail
20:10:17.288 -> Fail
20:10:19.833 -> Fail
20:10:22.328 -> Fail
20:10:24.838 -> Fail
20:10:27.341 -> Fail
20:10:29.851 -> Fail
20:10:32.376 -> Fail
20:10:34.893 -> Fail
20:10:37.410 -> Fail
20:10:39.906 -> Fail
20:10:42.422 -> Fail
20:10:44.935 -> Fail
20:10:47.438 -> Fail
20:10:49.984 -> Fail
20:10:52.484 -> Fail
20:10:55.017 -> Fail
20:10:57.489 -> Fail
20:10:59.999 -> Fail
20:11:02.534 -> Fail
20:11:05.028 -> Test
20:11:05.076 -> Not valid result
20:11:07.546 -> Fail
20:11:10.052 -> Fail
20:11:12.559 -> Fail
20:11:15.081 -> Fail
20:11:17.577 -> Fail
20:11:20.136 -> Fail
20:11:22.654 -> Fail
20:11:25.122 -> Fail
20:11:27.644 -> Fail
20:11:30.162 -> Fail

First - a delay of 2.5 seconds after the reception of each character leaves no chance of receiving something reasonable from the gps.
Next - do not print "Fail" or "Not valid..." if you just did not receive the next character from the gps, there is no error in this. This "if"s should be removed.

try this
May be you should wait few minutes until the GPS receive your coordinates.

#include <NeoSWSerial.h>
#include <NMEAGPS.h>
#include <GPSport.h>

NMEAGPS  gps; // This parses the GPS characters
gps_fix  fix;

void setup()
{
  Serial.begin(9600);
  gpsPort.begin(9600);
  delay(500);
}

void loop()
{
  int sendData;
  while (gps.available(gpsPort))
  {
    Serial.println(F("1"));
    fix = gps.read();

   if (fix.valid.location)
    {
      Serial.println(F("Succeed"));
      Serial.println();

      Serial.println(F("Latitude= ")); 
      Serial.println(fix.latitude(), 6);
      Serial.println(F(" Longitude= ")); 
      Serial.println(fix.longitude(), 6);
      sendData = 1;
      
    }
    else
    {
      Serial.println(F("2"));
      sendData = 0;
    }
    //delay(2500);
  }
 // if( gpsPort.available() <= 0)
 // {
 //   Serial.println(F("Fail"));
//  }
 // delay(2500);
}

it's work now,
but for running NB-IoT operation, i should just add code inside or use break to run the other function

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.