2

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()?

3 Answers 3

2

Why does Serial.write work, but Serial.print doesn't?

Serial.read() returns an integer for each byte from the serial port.

String converts each integer to its decimal ASCII representation (e.g. $ = "36", G = "71" etc), so Serial.print() receives an ASCII string with the decimal values of each input byte, which were already ASCII. This is probably not what you want.

-1

Well, @devnull gave a good and helpfull answer. I'll present another method of using Arduino to test the GPS module.

Here is an example of test code that you can use to test a GPS module with an ESP32 using the Arduino IDE and the Serial.print() function:

#include <TinyGPS++.h>
#include <HardwareSerial.h>

// Set up a serial connection for the GPS module
HardwareSerial GPSSerial(2);
TinyGPSPlus gps;

void setup() {
  // Initialize serial communication with the computer
  Serial.begin(115200);

  // Initialize serial communication with the GPS module
  GPSSerial.begin(9600, SERIAL_8N1, 16, 17);
}

void loop() {
  // Read data from the GPS module
  while (GPSSerial.available() > 0) {
    gps.encode(GPSSerial.read());
  }

  // Print the GPS data to the serial monitor
  Serial.print("Latitude: ");
  Serial.print(gps.location.lat(), 6);
  Serial.print(" Longitude: ");
  Serial.println(gps.location.lng(), 6);
  Serial.print("Altitude: ");
  Serial.print(gps.altitude.meters());
  Serial.println(" meters");
  Serial.print("Date: ");
  Serial.print(gps.date.month());
  Serial.print("/");
  Serial.print(gps.date.day());
  Serial.print("/");
  Serial.println(gps.date.year());
  Serial.print("Time: ");
  Serial.print(gps.time.hour());
  Serial.print(":");
  Serial.print(gps.time.minute());
  Serial.print(":");
  Serial.println(gps.time.second());
  Serial.println();

  // Wait a second before printing the data again
  delay(1000);
}

This code initializes a serial connection with the GPS module using the HardwareSerial library and the TinyGPS++ library to parse the GPS data. The loop() function reads data from the GPS module and prints the latitude, longitude, altitude, date, and time to the serial monitor using the Serial.print() function.

You may need to adjust the serial port and baud rate settings to match your specific GPS module. You may also want to add additional code to handle errors or check for valid GPS data.

1
  • 1
    This isn't really answering the question, as asked.
    – Nick Gammon
    Commented Dec 30, 2022 at 8:00
-1

i only use this to get data from the GPS module, no need for library you need to declare an array, say gps_rawData[200] = {0};

while (SerialGPS.available()) { size_t byteCount = SerialGPS.readBytesUntil('\n', gps_rawData, sizeof(gps_rawData) - 1); gps_rawData[byteCount] = NULL; String gps_rawDataString = String(gps_rawData); SerialMon.println(gps_rawDataString); }

this way you have the data in an array to be manipulated whenever or wherever required.

1
  • They're asking about why Serial.write behaves one way and Serial.print behaves a different way, which is answered by devnull.
    – timemage
    Commented Jan 16, 2023 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.