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

user57586: 
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.

53790: <p>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 <code>Serial.write()</code> and <code>Serial.print()</code>.</p>
<p>This code is working perfectly with <code>Serial.write()</code> and the output is meaningful:</p>
<pre><code>void loop() {
  while (Serial2.available() &gt; 0) {
    Serial.write(Serial2.read());
  }

  Serial.println(&quot;-------------------------------------&quot;);
}
</code></pre>
<p>Output:</p>
<pre><code>13:28:05.173 -&gt; -------------------------------------
13:28:05.206 -&gt; $GPRMC,,V,,,,,,,,,,N*53
13:28:05.238 -&gt; $GPVTG,,,,,,,,,N*30
13:28:05.238 -&gt; $GPGGA,,,,,,0,00,99.99,,,,,,*48
13:28:05.270 -&gt; $GPGSA,A,1,,,,,,,,,,,,,99.99,99.99,99.99*30
13:28:05.334 -&gt; $GPGSV,1,1,00*79
13:28:05.334 -&gt; $GPGLL,,,,,,V,N*64
13:28:05.366 -&gt; -------------------------------------
</code></pre>
<p>When I convert this code to <code>Serial.print()</code> the output looks strange:</p>
<pre><code>void loop() {
  while (Serial2.available() &gt; 0) {
    Serial.print(String(Serial2.read()));
  }

  Serial.println(&quot;-------------------------------------&quot;);
}
</code></pre>
<p>Output:</p>
<pre><code>13:26:43.118 -&gt; -------------------------------------
13:26:43.150 -&gt; -------------------------------------
13:26:43.214 -&gt; 367180827767444486444444444444444444447842535113103671808684714444444444444444447842514813103671807171654444444444444844484844575746575744444444444442525613103671807183654465444944444444444444444444444444575746575744575746575744575746575742514813103671807183864449444944484842555713103671807176764444444444448644784254521310-------------------------------------
13:26:43.566 -&gt; -------------------------------------
13:26:43.629 -&gt; -------------------------------------
</code></pre>
<p>Why does <code>Serial.write</code> work, but <code>Serial.print</code> doesn't? Or: how to write the same code with <code>Serial.print()</code>?</p>
 57586: <blockquote>
<p>Why does Serial.write work, but Serial.print doesn't?</p>
</blockquote>
<p><code>Serial.read()</code> returns an integer for each byte from the serial port.</p>
<p><code>String</code> converts each integer to its decimal ASCII representation (e.g. $ = &quot;36&quot;, G = &quot;71&quot; etc), so <code>Serial.print()</code> receives an ASCII string with the decimal values of each input byte, which were already ASCII. This is probably not what you want.</p>
