I have a GY-GPS6MV2 connected to a nano. (Also tried UNO same result). RX on GPS goes to TX of Nano PIN 3 and TX on GPS to PIN 4 on Nano.
When I run the code below all I get is a set of numbers. They do not appear to be hex numbers.
367180827767444486444444444444444444447842535113103671808684714444444444444444447842514813103671807171654444444444444844484844
The code I am using is
#include <SoftwareSerial.h>
String data = "";
// Define the Arduino pins for software serial communication
const int RXPin = 4;
const int TXPin = 3;
// Set the default baud rate for the NEO-6M GPS module
const int GPSBaud = 9600;
// Create a software serial object for GPS communication
SoftwareSerial gpsSerial(RXPin, TXPin);
void setup() {
// Start the hardware serial port for communication with the computer
Serial.begin(9600);
// Start the software serial port for GPS communication
gpsSerial.begin(GPSBaud);
}
void loop() {
data = "";
// Check if there is data available from the GPS module
while (gpsSerial.available()>0) {
// Read the incoming data and send it to the computer
data += gpsSerial.read();
}
// Read the incoming data and send it to the computer
Serial.println(data);
delay(30000);
}
I tried 2 different GPS modules and the result is the same. Seems that the GPS sends me data via the Arduino but not the correct data. I read about cold starts so I left the module running more than 30 hours but the result is the same. I know it may not see satellite and was expecting to see some NMEA from it even if gibberish NMEA.
What have I missed in making this work please?
1 Answer 1
When you write gpsSerial.read()
, you are calling the method
int SoftwareSerial::read();
Notice it returns an int
, not a char
. Why, you may ask. The reason
is that a char
can only hold 256 different values, whereas this
method can return 257 different values:
- values from 0 through 255 represent actual bytes read from the port
- the value −1 represents an error condition.
In your case, you don't have to worry about errors, as you are only
calling the method when you know for sure it has data to deliver (i.e.
when gpsSerial.available()>0
).
When you write data += gpsSerial.read();
, you are calling the method
String & String::operator += (int num);
which appends a number to a string. In the face of it, this seems meaningless. What could it even mean to "append a number to a string"? The Arduino library interprets it as meaning "write down the number in decimal, using ASCII digits, and append that to the string". Thus, if you
data += 36;
this will append the string "36"
to data
.
Now, if you instead do
data += (char) 36,
this will add to data
the character number 36, i.e. '$'
. And this is
what you actually want. Thus:
data += (char) gpsSerial.read();
,
ASCII values
3671808277674444...
is the ASCII code for"$GPRMC,,...
, so you are receiving data correctly but doing the wrong data cancatenation. When programming in C/C++, always need to aware what kind of data type you are dealing with.gpsSerial.read()
return an int, so you need to cast it to achar
before you can concatenate with a String withdata += (char) gpsSerial.read();
.