Serial1 - Atmega2560

Hi guys,

I have a very strange problem.
I made a class to encapsulate the Adafruit_GPS (https://github.com/adafruit/Adafruit-GPS-Library/blob/master/Adafruit_GPS.cpp) library to abstract from other parts of the code to a possible change of GPS.

So I make the call to class, which creates the object using the class of Adafruit_GPS Serial1.
Until then OK, it works. The problem happens when I turn off the board completely and turn on again.
The Code is considering the Serial1 is a SW, not HW. (Line 252-269 of their library.)

teste.ino

#include <GPSClass.h>
#include <Adafruit_GPS.h>

#include <SoftwareSerial.h>


GPSClass *gps;

void setup()  
{
  Serial.begin(9600);
  delay(200);
  Serial.println("Comecando!!!!");

  gps = new GPSClass(5,20);
  gps->on();
  gps->begin(&Serial1);
  gps->start();
}

void loop() {
  gps->getData();
  gps->show();
}

GPSClass.h

#ifndef _GPSCLASS_H
#define _GPSCLASS_H

#include <Adafruit_GPS.h>

#define MAXWAITSENTENCE 5

#include "Arduino.h"

class GPSClass {
  public:
    GPSClass(int , int);
    ~GPSClass();
    void start();
    void on();
    void off();
    
    void getData();
    
    void show();
    
    void begin(HardwareSerial *serIn);

   private:
    void readData();
    void parseData();
    int _gpsEnablePin;
    int _gpsFixPin;
    uint32_t timer;
    Adafruit_GPS *_gpsModule;
    HardwareSerial *_Serial;
};
#endif

GPSClass.cpp

#include "GPSClass.h"
#include <Adafruit_GPS.h>

#define GPSECHO  true

GPSClass::GPSClass(int gpsEnablePin, int gpsFixPin)  {
_Serial = NULL;

  timer = millis();
  _gpsEnablePin = gpsEnablePin;
  _gpsFixPin = gpsFixPin;
  
  
  pinMode(_gpsEnablePin, OUTPUT);
  pinMode(_gpsFixPin, INPUT);

};

GPSClass::~GPSClass() {
  delete _gpsModule;
};

void GPSClass::begin(HardwareSerial *serIn)
{
   _Serial = serIn;
   _Serial->begin(9600);
   _Serial->println("Ready to Rip!");
}

void GPSClass::start() {
  _gpsModule = new Adafruit_GPS(_Serial);
  
  _gpsModule->begin(9600);

  _gpsModule->sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

  _gpsModule->sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

  _gpsModule->sendCommand(PGCMD_ANTENNA);

  delay(1000);
}

void GPSClass::on() {
  digitalWrite(_gpsEnablePin, HIGH);
}

void GPSClass::off() {
  digitalWrite(_gpsEnablePin, LOW);
}



void GPSClass::getData() {
  this->readData();
  this->parseData();
}

void GPSClass::show() {
 // if millis() or timer wraps around, we'll just reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) { 
    timer = millis(); // reset the timer
    
//this->getLatitude();
//this->getLongitude();
    
    Serial.print("\nTime: ");
    Serial.print(_gpsModule->hour, DEC); Serial.print(':');
    Serial.print(_gpsModule->minute, DEC); Serial.print(':');
    Serial.print(_gpsModule->seconds, DEC); Serial.print('.');
    Serial.println(_gpsModule->milliseconds);
    Serial.print("Date: ");
    Serial.print(_gpsModule->day, DEC); Serial.print('/');
    Serial.print(_gpsModule->month, DEC); Serial.print("/20");
    Serial.println(_gpsModule->year, DEC);
    Serial.print("Fix: "); Serial.print((int)_gpsModule->fix);
    Serial.print(" quality: "); Serial.println((int)_gpsModule->fixquality); 
    if (_gpsModule->fix) {
      Serial.print("Location: ");
      Serial.print(_gpsModule->latitude, 4); Serial.print(_gpsModule->lat);
      Serial.print(", "); 
      Serial.print(_gpsModule->longitude, 4); Serial.println(_gpsModule->lon);
      
      Serial.print("Speed (knots): "); Serial.println(_gpsModule->speed);
      Serial.print("Angle: "); Serial.println(_gpsModule->angle);
      Serial.print("Altitude: "); Serial.println(_gpsModule->altitude);
      Serial.print("Satellites: "); Serial.println((int)_gpsModule->satellites);
    }
  }
}

void GPSClass::readData() {
  //char c = _gpsModule->read();
  // if you want to debug, this is a good time to do it!
  // if ((c) && (GPSECHO))
    // Serial.write(c); 
  
  _gpsModule->read();
}

void GPSClass::parseData() {
   // if a sentence is received, we can check the checksum, parse it...
  if (_gpsModule->newNMEAreceived()) {
    if (!_gpsModule->parse(_gpsModule->lastNMEA())) // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }
}

I removed some parts of the code. Maybe I have deleted a funcção or other unintentionally. If you feel a lack of something, let me know.

But as I said the problem is happening when I unplug the board and again I reconnect. I never saw it and I have not found much help on the internet …

I thank anyone who can help.

Thank you

Sorry for my poor english …

what sort of arduino do you have ?

On a mega you use serial1.

For any other kind, you use software serial.

I'm using ATMEGA2560 directly.
But using the Arduino IDE and its libraries

The problem happens after I upload the program, first it works.
When I turn off the circuit and turn on, it no longer works, it confuses Serial1 with a SoftwareSerial

When I turn off the circuit and turn on, it no longer works, it confuses Serial1 with a SoftwareSerial

Not possible. The hex file that was uploaded shouldn't know anything about SoftwareSerial, so it can't possibly be confusing an instance of HardwareSerial for an instance of SoftwareSerial.

There seems to be something wrong with YOUR hardware setup.

I suggest that you try the code on a real Mega.

I really want believe in you.

I tried use directly the Adafruit_GPS library and works normally, so probably is a problem with the code and not the hardware.

I was thinking about the order that the references were created in the bootloader. Does anyone that knows it better could help?

I was thinking about the order that the references were created in the bootloader.

The bootloader knows nothing about the code that the chip is trying to run. There are no "references" in the bootloader.

And about the start of Serial Module?
I believe that they start the register with a default configuration. Do you know if this is configured before the program start?

I think nobody is believing in me, next week if this is not resolved, I will record a video to show this.

Hi fnoel

When I turn off the circuit and turn on, it no longer works, it confuses Serial1 with a SoftwareSerial

When the problem happens, exactly what symptoms do you see? I guess hardware Serial1 is not working as you expect it to. But what makes you say that it changes to use SoftwareSerial?

Regards

Ray

Exactly in the lines 252-269 of https://github.com/adafruit/Adafruit-GPS-Library/blob/master/Adafruit_GPS.cpp.

So I put 2 Serial.println("HereX")...

When I upload, it goes to "Adafruit_GPS::Adafruit_GPS(HardwareSerial *ser) "
After that if I turn off, When I turn ON, it goes to "Adafruit_GPS::Adafruit_GPS(SoftwareSerial *ser)"

To complete this weird problem, if I use the reset pin instead of turn off, it works normally

Sorry, I don't understand. I'm probably being dumb :slight_smile:

it goes to "Adafruit_GPS::Adafruit_GPS(SoftwareSerial *ser)"

What do you mean exactly by "it goes"?

That the program has gone to this point instead the other

Sorry for bad english... I need to improve on that too