Difference when reading Hardware serial (0 and 1 ports) and SoftSerial?

Hi.. I am experiencing some issue here. maybe you guys could help me here.

When I red the serial by hardware serial(1 and 0) the my Gps Shield retrieves all(correct) information.. like this:

My Code:
void setup()
{
Serial.begin(38400);
}

void loop() // run over and over
{
if (Serial.available())
Serial.write(Serial.read());
}

$GPRMC,221638.00,A,2307.08112,S,04633.21294,W,0.041,,311013,,,A*75
$GPVTG,,T,,M,0.041,N,0.077,K,A*26
$GPGGA,221638.00,2307.08112,S,04633.21294,W,1,07,1.17,824.7,M,-5.6,M,,*4A
$GPGSA,A,3,02,24,25,29,10,04,12,,,,,,1.71,1.17,1.25*0A
$GPGSV,3,1,12,02,67,071,37,04,36,127,37,05,09,029,,10,08,080,31*7C
$GPGSV,3,2,12,12,42,202,43,14,02,229,31,15,15,331,,17,00,128,*79
$GPGSV,3,3,12,24,77,281,28,25,13,231,37,26,01,006,,29,22,287,25*7E
$GPGLL,2307.08112,S,04633.21294,W,221638.00,A,A*68
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58
$GPTXT,01,01,01,NMEA unknown msg*58

Note that the fist row is ($GPRMC,221638.00,A,2307.08112,S,04633.21294,W,0.041,,311013,,,A*75)

ok...

But when I just try to connect using serial port I just get the first row(the first row starts with $GPRMC)

Here's the code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4); // RX, TX

void setup()
{
Serial.begin(115200);
mySerial.begin(38400);
}

void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
if (Serial.available())
mySerial.write(Serial.read());
}

Here's the resut:

$GG$GPRMC,222221.01,A,2307.07587,S,04633.21542,W,0.037,,311013,,,AG$GPRMC,222222.00,A,2307.07583,S,04633.21531,W,0.195,,311013,,,A,

It is like when when first "row" ends... its stopping the info from serial.

Maybe some special character or something..

Any idea?

Thank you so much

Rodrigo

The hardware Serial port uses pins 0 and 1 so using hardware Serial and then also connecting those pins to another device is not a good idea. As well as having devices trying to drive the Tx pin in conflict with each other, everything you write to the serial port is going back to the GPS unit as well as to the serial monitor. Is the GPS output in the first case actually correct? That "NMEA unknown msg" stuff doesn't look very encouraging.

The SoftwareSerial version looks credible but the output suggests that it's either missing bytes, or getting bytes duplicated. SoftwareSerial isn't as consistent and reliable as the hardware UART especially when there are other interrupts going on, and it may be that it's just not accurate enough to work at the 38400 speed you're using. Is it possible to configure the GPS unit to use slower serial speed?

you don't need to send commands to the gps, do you ?
Then, what happens if you change your code so that you don't send datas back to "myserial" like this ? :

#include <SoftwareSerial.h>

SoftwareSerial mySerial(3, 4); // RX, TX

void setup()
{
Serial.begin(115200);
mySerial.begin(38400);
}

void loop() // run over and over
{
if (mySerial.available())
Serial.write(mySerial.read());
}

You may have a conflict between how the end of line is processed on different devices. Check which ascii character the GPS device is actually sending you at the end of a line. You may have a variant of the CR/LF disease.

hi, michinyon. thank you vor your help.

How could I check which ascii is beng sent at the end?

i am sorry. I am new to arduino..

thank u so much..

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
}

It occurs to me that this "obvious" loop may not be a good way to read a GPS.
When the output buffer of the hardware serial port fills up, Serial.write() will take 'substantial' time, perhaps long enough that the next character will not be received correctly by softwareserial.

Hi,,

Do you have any idea about how to solve this.?

Any help will be apreciared.

thank you very much

Well, you could try something like:

char gpsbuffer[100];
char *gps_p = gpsbuffer;
char buflen = 99;
void loop() {
  if (mySerial.read() > 0) {
    int c = mySerial.read();
    *gps_p++ = c;
    if (--buflen == 0 || c == '\n' || c == '\r') {
      // out of space or end of line.
      *gps_p = 0;  //terminate string
      Serial.print(gpsbuffer);
      gps_p = gpsbuffer;
      buflen = 99;
    }
  }
}

This takes a lot of space (100 chars might not be enough), so it's obviously not ideal, but it might serve to indicate whether this is the problem or not. (it DOES assume that there will be a pause after each newline, which might not be true.)

westfw:
This takes a lot of space (100 chars might not be enough), so it's obviously not ideal, but it might serve to indicate whether this is the problem or not. (it DOES assume that there will be a pause after each newline, which might not be true.)

NMEA sentences can be no longer than 80 visible characters + CR + LF -> 82 bytes max.
they must begin with '$' and end with a CR/LF sequence

hi, Alnath.

make sense but why Am I getting just the first sentence?

any idea?

When I read the. Hardware Serial it reads all infos, but when I use softwareserial it returns just the. First sentence.

did you try the modified code in answer #2 ? still the same ?

Westw code in answer #7 should work too

I've just tried your code and the modified code I posted in answer #2 and...... both work for me !!
the only differences are in the bauds settings :
my gps runs at 9600 bauds, so I set myserial at 9600 and serial was set at 38400 .
The sentences were all correctly printed on the monitor

Hi, Alnath.

When you mean "all sentences" you mean a lot of diferent sentences? like

$GPGSA....
$GPGSV,....
or only $GPRMC,?

tks

westfw:
Well, you could try something like:

char gpsbuffer[100];

char *gps_p = gpsbuffer;
char buflen = 99;
void loop() {
  if (mySerial.read() > 0) {
    int c = mySerial.read();
    *gps_p++ = c;
    if (--buflen == 0 || c == '\n' || c == '\r') {
      // out of space or end of line.
      *gps_p = 0;  //terminate string
      Serial.print(gpsbuffer);
      gps_p = gpsbuffer;
      buflen = 99;
    }
  }
}




This takes a lot of space (100 chars might not be enough), so it's obviously not ideal, but it might serve to indicate whether this is the problem or not. (it DOES assume that there will be a pause after each newline, which might not be true.)

westfw:
Well, you could try something like:

char gpsbuffer[100];

char *gps_p = gpsbuffer;
char buflen = 99;
void loop() {
  if (mySerial.read() > 0) {
    int c = mySerial.read();
    *gps_p++ = c;
    if (--buflen == 0 || c == '\n' || c == '\r') {
      // out of space or end of line.
      *gps_p = 0;  //terminate string
      Serial.print(gpsbuffer);
      gps_p = gpsbuffer;
      buflen = 99;
    }
  }
}




This takes a lot of space (100 chars might not be enough), so it's obviously not ideal, but it might serve to indicate whether this is the problem or not. (it DOES assume that there will be a pause after each newline, which might not be true.)

Hi!

I did the this above and now I am receiving some characters that doesn't seem to be correct(or at least doesn't look like an GPS Sentence)

On I'm getting info like this:

DGRC154.0A20.87,,43.17,,.0,011,,*0,2CAGRC154.0A20.86,,43.18,,.2,011,,*EG8,4ÿGRC154.0A20.84,,43.10,,.2,011,,*G3341ÿGRC154.0A20.84,,43.11,,.1,011,,*G,1,2GRC154.0A20.83,,43.12,,.1,011,,*2,,

6GRC154.0A20.82,,43.13,,.6,011,,*2,3

7GRC154.0A20.81,,43.15,,.6,011,,*90344GRC155.0A20.81,,43.16,,.1,011,,*1,5GÿGRC155.0A20.80,,43.17,,.0,011,,*$A292ÿGRC155.0A20.88,,43.18,,.5,011,,*1,,$

GRC155.0A20.87,,43.19,,.5,011,,*PA,22ÿGRC155.0A20.86,,43.10,,.4,011,,*,2048GRC155.0A20.85,,43.11,,.0,011,,*A,,,,GRC155.0A20.84,,43.12,,.6,011,,*GA,22ÿGRC155.0A20.83,,43.12,,.3,011,,*5,,,1GRC155.0A20.83,,43.13,,.2,011,,*52,
09GRC155.0A20.83,,43.13,,.7,011,,*215,,GRC100.0A20.82,,43.13,,.2,011,,*A,,,3GRC100.0A20.82,,43.14,,.1,011,,*2G293ÿGRC100.0A20.81,,43.15,,.2,011,,*03,B*GRC100.0A20.80,,43.15,,.1,011,,*7,1A*GRC100.0A20.89,,43.16,,.7,011,,*23
4,4ÿGRC100.0A20.89,,43.17,,.9,011,,*.,,42ÿGRC100.0A20.89,,43.17,,.3,011,,*,4,,,GRC100.0A20.89,,43.18,,.2,011,,*.,,42ÿGRC100.0A20.89,,43.18,,.4,011,,*,2,,,ÿGRC100.0A20.89,,43.18,,.6,011,,*2117WGRC101.0A20.80,,43.18,,.1,011,
,*

A321ÿGRC101.0A20.80,,43.18,,.2,011,,*PA045GRC101.0A20.80,,43.18,,.6,011,,*,.*3ÿGRC101.0A20.80,,43.18,,.6,011,,*,,,20GRC101.0A20.89,,43.19,,.4,011,,*007,ÿGRC101.0A20.89,,43.10,,.2,011,,*129,.GRC101.0A20.89,,43.10,,.6,011,,*
0G218ÿGRC101.0A20.89,,43.10,,.1,011,,*9,2G1GRC101.0A20.89,,43.10,,.4,011,,*9,,G8GRC101.0A20.80,,43.10,,.3,011,,*331*,GRC102.0A20.81,,43.10,,.2,011,,*.,150GRC102.0A20.81,,43.10,,.3,011,,*6,,G0GRC102.0A20.82,,43.10,,.6,011,,
*.,0

0GRC102.0A20.82,,43.10,,.3,011,,*PA102ÿGRC102.0A20.83,,43.19,,.4,011,,*S44S0GRC102.0A20.83,,43.19,,.9,011,,*7,27,GRC102.0A20.83,,43.19,,.2,011,,*2G,,0ÿGRC102.0A20.83,,43.19,,.3,011,,*311*,GRC102.0A20.83,,43.19,,.3,011,,*21
176GRC102.0A20.83,,43.19,,.7,011,,*3,3G2GRC103.0A20.84,,43.10,,.3,011,,*.,,02ÿGRC103.0A20.84,,43.10,,.4,011,,*G,04,ÿGRC103.0A20.85,,43.10,,.4,011,,*5G23.ÿGRC103.0A20.86,,43.10,,.2,011,,*$A,78ÿGRC103.0A20.87,,43.10,,.3,011,
,*.,,02ÿGRC103.0A20.87,,43.10,,.6,011,,*321,3ÿGRC103.0A20.87,,43.10,,.6,011,,*0,,

It had to be like this (this is how the HardwareSerial reads)
$GPRMC,052613.172,A,3959.1983,N,11619.6639,E,,,290609,,01
$GPGGA,052614.266,3959.2084,N,11619.6691,E,1,03,4.1,74.0,M,-6.5,M,,0000
7F
$GPGSA,A,2,30,14,29,,,,,,,,,,4.2,4.1,1.03C
$GPRMC,052614.266,A,3959.2084,N,11619.6691,E,1.24,10.61,290609,,3E
$GPGGA,052615.269,3959.2074,N,11619.6690,E,1,03,4.1,74.0,M,-6.5,M,,0000
7F
$GPGSA,A,2,30,14,29,,,,,,,,,,4.2,4.1,1.0
3C
$GPRMC,052615.269,A,3959.2074,N,11619.6690,E,2.09,90.96,290609,,32
$GPGGA,052616.269,3959.2080,N,11619.6676,E,1,04,3.9,48.4,M,-6.5,M,,0000
7C

I am starting to thing that MY GPS Shield from IteadStudio doesn't like to use SoftwareSerial

Could it be possible?

I mean all sentences gps sends
$GPGSA....
$GPGSV,....
$GPGGA,....
$GPRMC....
....
etc...

alnath:
I mean all sentences gps sends
$GPGSA....
$GPGSV,....
$GPGGA,....
$GPRMC....
....
etc...

You are using SoftwareSerial, right?

I think MY gps shield doesn't like it

cabecinhas:

alnath:
I mean all sentences gps sends
$GPGSA....
$GPGSV,....
$GPGGA,....
$GPRMC....
....
etc...

You are using SoftwareSerial, right?

I think MY gps shield doesn't like it

yep, I tried to make my config as close as yours as I could : SoftwareSerial, same pins etc... except the bauds rate I couldn't change easily .
I've read elsewhere that SoftwareSerial library doesn't like too high bauds settings . Maybe that's why it works for me at 9600 bauds and not for you at 38400 bauds .
Why do you want to use SoftwareSerial ? Do you need the hardware serial for another device ? And if yes, maybe that other device's baud rate is easier to change, then you could connect it to softwareserial at a lower speed ?
If you really want to use SoftwareSerial with your GPS shield, you can probably change the baud settings and make it lower .

Using SoftwareSerial and hardware serial interface may be tricky, because SoftwareSerial disables interrupts .

Hi Alnath. First of all, thank you very much for your help!

Well. I'm not sure about advantages in use Hardware Serial. etc. You could maybe teach me about it. ( I would like to :))

Welll. Let's go back to the facts!

"Why do you want to use SoftwareSerial ?"
I am using SoftSerial because if not I need to remove the shield every time I need to upload a sketch to the arduino.(Am I right)

"Do you need the hardware serial" for another device ?"
Actually no. I don't pretend to connece another device in the Hardware Serial.
Here goes a question. Since I have only une Hardware serial on my Arduino Uno, could I use other shields using SoftweSerial instead?

"If you really want to use SoftwareSerial with your GPS shield, you can probably change the baud settings and make it low"
Actually I have tested lower values in my GPS but didn't work since on datasheet it is 38400(Could I use a different values in shields that are a cartain value on their datasheets???

"Using SoftwareSerial and hardware serial interface may be tricky, because SoftwareSerial disables interrupts ."
What do you mean exactlly by "because SoftwareSerial disables interrupts"?

Could you tell me a litlle abotuout it?

Once again...

Thank you very much for your help. :slight_smile:

Well. I'm not sure about advantages in use Hardware Serial. etc. You could maybe teach me about it. ( I would like to smiley)

Welll. Let's go back to the facts!

"Why do you want to use SoftwareSerial ?"
I am using SoftSerial because if not I need to remove the shield every time I need to upload a sketch to the arduino.(Am I right)

The hardware interface has a hardware support (UART) that drives the communications, and takes care of everything (timing... etc...) , as the name tells you, SoftwareSerial has to do all the stuff by soft .
Could you post a link to your shield ? If it has a power switch, you could turn it of while you upload the sketch

Could you tell me a litlle abotuout it?

DC42's page explains it better than I would :wink:

alnath:

Well. I'm not sure about advantages in use Hardware Serial. etc. You could maybe teach me about it. ( I would like to smiley)

Welll. Let's go back to the facts!

"Why do you want to use SoftwareSerial ?"
I am using SoftSerial because if not I need to remove the shield every time I need to upload a sketch to the arduino.(Am I right)

The hardware interface has a hardware support that drives the communications, and takes care of everything (timing... etc...) , as the name tells you, SoftwareSerial has to do all the stuff by soft .
Could you post a link to your shield ? If it has a power switch, you could turn it of while you upload the sketch

Could you tell me a litlle abotuout it?

DC42's page explains it better than I would :wink:
Five things I never use in Arduino projects | David Crocker's Solutions blog

Hi.. I will take a look and learn more..

Thank you very very much.

The website of mu gps is
http://blog.iteadstudio.com/play-arduino-with-global-positioning-system-gps/

My Gps doesnt have a power switch so I will make a wire and put a push connector and then when I go upload I will just press the button..

:slight_smile:

Tks for your help