I am testing my GPS module with an ESP32 using the Arduino IDE. I'm asking because I usually don't fully understand the difference between Serial.write() and Serial.print().
This code is working perfectly with Serial.write() and the output is meaningful:
void loop() {
while (Serial2.available() > 0) {
Serial.write(Serial2.read());
}
Serial.println("-------------------------------------");
}
Output:
13:28:05.173 -> -------------------------------------
13:28:05.206 -> $GPRMC,,V,,,,,,,,,,N*53
13:28:05.238 -> $GPVTG,,,,,,,,,N*30
13:28:05.238 -> $GPGGA,,,,,,0,00,99.99,,,,,,*48
13:28:05.270 -> $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
13:28:05.334 -> $GPGSV,1,1,00*79
13:28:05.334 -> $GPGLL,,,,,,V,N*64
13:28:05.366 -> -------------------------------------
When I convert this code to Serial.print() the output looks strange:
void loop() {
while (Serial2.available() > 0) {
Serial.print(String(Serial2.read()));
}
Serial.println("-------------------------------------");
}
Output:
13:26:43.118 -> -------------------------------------
13:26:43.150 -> -------------------------------------
13:26:43.214 -> 367180827767444486444444444444444444447842535113103671808684714444444444444444447842514813103671807171654444444444444844484844575746575744444444444442525613103671807183654465444944444444444444444444444444575746575744575746575744575746575742514813103671807183864449444944484842555713103671807176764444444444448644784254521310-------------------------------------
13:26:43.566 -> -------------------------------------
13:26:43.629 -> -------------------------------------
Why does Serial.write work, but Serial.print doesn't? Or: how to write the same code with Serial.print()?