# Arduino filling the serial Buffer with nulls

**URL:** <https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [September 7, 2022, 7:39pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674 "2022-09-07T19:39:23Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 10, 2022, 12:57pm UTC](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/14 "2022-09-10T12:57:43Z")

</div>

Hello @maro,

> **Arduino Code**
>
> ```auto
> void setup() {
> Serial.begin(9600);
> Serial.println();
> Serial.println("0123456789ABCDEF");
> }
> 
> void loop() 
> {
> }
> 
> ```

> **Processing Code**
>
> ```auto
> // Example by Tom Igoe
> 
> import processing.serial.*;
> 
> int lf = 10; // Linefeed in ASCII
> String myString = null;
> Serial myPort; // The serial port
> 
> void setup() {
> // List all the available serial ports
> printArray(Serial.list());
> // Open the port you are using at the rate you want:
> myPort = new Serial(this, Serial.list()[4], 9600);
> myPort.clear();
> // Throw out the first reading, in case we started reading 
> // in the middle of a string from the sender.
> //myString = myPort.readStringUntil(lf); // glv - I commented this
> myString = null;
> }
> 
> void draw() {
> while (myPort.available() > 0) {
> myString = myPort.readStringUntil(lf);
> if (myString != null) {
> //println(myString); // glv - I commented this
>       
> // ******************************************
> // glv - I added this:
> int sl = myString.length();
> println(sl); 
> for(int i = 0; i < sl; i++)
> print(hex(myString.charAt(i), 2));
> println(); 
> // ******************************************  
>     
> }
> }
> }
> 
> ```

Console output directly after:

- Uploading Arduino code to Arduino Uno R3
- Running Processing 4.0.1 sketch the first time

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/3/4/348f768655c9e556574b686d81a04aef75e761ae.png)

The _zeroes_ are not there after an upload followed by a USB disconnect\connect (power off\on) of the board and then running the Processing sketch.

The _zeroes_ are not there on subsequent runs of of the Processing sketch which resets the Arduino.

Related:

> **[Serial Monitor contains garbage after upload](https://forum.arduino.cc/t/serial-monitor-contains-garbage-after-upload/972312)**
>
> This is what I see: This only seems to happen right after an upload, only on a UNO, using IDE 2.0 RC5. But not after a reset or when I unplug and replug the USB. While it means little functionally, it is cosmetically ugly.

> <https://github.com/arduino/arduino-ide/issues/927>
>
> \### Describe the problem
> 
> The first output printed to Serial Monitor after an …upload is some "garbage" characters.
> 
> !\[image\](https://user-images.githubusercontent.com/8572152/159872636-d8e0c920-e628-4c91-9c91-65611fcfcaaa.png)
> 
> \### To reproduce
> 
> \#### Equipment
> 
> \- Arduino board that uses the ATmega16U2 USB chip:
> - Arduino Uno
> - Arduino Uno Mini
> - Arduino Mega
> 
> \#### Steps
> 
> 1. Upload a sketch to your board that prints to \`Serial\`:
> \`\`\`cpp
> void setup() {
> Serial.begin(9600);
> Serial.println("hello world");
> }
> void loop() {}
> \`\`\`
> 1. Open the "\*\*Serial Monitor\*\*" view.
> 
> 🐛 The output starts with some unexpected content:
> 
> \`\`\`text
> □□□□□□□□□□□hello world
> \`\`\`
> 
> \### Expected behavior
> 
> Serial monitor output always reflects the data sent by the board.
> 
> \### Arduino IDE version
> 
> 2.0.0-rc5-snapshot-4de7737
> 
> \### Operating system
> 
> Windows
> 
> \### Operating system version
> 
> 10
> 
> \### Additional context
> 
> The number of \`□\` in the demonstration matches the number of characters that are printed by the sketch.
> 
> \---
> 
> The spurious output does not occur when the output is triggered by resetting the board, so it is specific to the upload operation.
> 
> \---
> 
> The spurious output still occurs even if you add a delay before the print, so it is not a matter of timing from initialization as is sometimes the cause of corrupted serial output.
> 
> \---
> 
> The issue does not occur when using Arduino IDE 1.x
> 
> \---
> 
> I have only been able to reproduce this issue with the Arduino boards that use an ATmega16U2 USB to serial adapter chip.
> 
> I could not reproduce it when using boards with other USB interfaces:
> 
> \- Native USB (Leonardo, Nano 33 IoT)
> \- FTDI FT232R (Nano, Pro Mini)
> \- WCH CH340 (3rd party boards)
> 
> \---
> 
> Originally reported at https://forum.arduino.cc/t/serial-monitor-contains-garbage-after-upload/972312
> 
> \### Issue checklist
> 
> \- \[X\] I searched for previous reports in \[the issue tracker\](https://github.com/arduino/arduino-ide/issues?q=)
> \- \[X\] I verified the problem still occurs when using the latest \[nightly build\](https://github.com/arduino/arduino-ide#nightly-builds)
> \- \[X\] My report contains all necessary details

Related but not exactly ? the same issue…  
Consider opening an issue that is specific to your observations.

`:)`

---

_[View the full topic](https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674)._
