Serial1 - Atmega2560


hi guys,

i have strange problem.
i made class encapsulate adafruit_gps (https://github.com/adafruit/adafruit-gps-library/blob/master/adafruit_gps.cpp) library abstract other parts of code possible change of gps.

so make call class, creates object using class of adafruit_gps serial1.
until ok, works. problem happens when turn off board , turn on again.
the code considering serial1 sw, not hw. (line 252-269 of library.)

teste.ino
code: [select]
#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
code: [select]
#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
code: [select]

#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 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 reset it
  if (timer > millis())  timer = millis();

  // approximately every 2 seconds or so, print out current stats
  if (millis() - timer > 2000) {
    timer = millis(); // reset 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 want debug, time it!
  // if ((c) && (gpsecho))
    // serial.write(c);
 
  _gpsmodule->read();
}

void gpsclass::parsedata() {
   // if sentence received, can check checksum, parse it...
  if (_gpsmodule->newnmeareceived()) {
    if (!_gpsmodule->parse(_gpsmodule->lastnmea())) // sets newnmeareceived() flag false
      return;  // can fail parse sentence in case should wait another
  }
}




i removed parts of code. maybe have deleted funcção or other unintentionally. if feel lack of something, let me know.

but said problem happening when unplug board , again reconnect. never saw , have not found on internet ...

i thank can help.

thank

sorry poor english ...

what sort of arduino have ?

on mega use serial1.

for other kind,  use software serial.


Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Serial1 - Atmega2560


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support