Arduino filling the serial Buffer with nulls

I just wrote some code that sends a String from Arduino to Processing. Processing prints the String and display the number of characters in the buffer…
After uploading the sketch to the Arduino and starting Processing for the first time, The serial Buffer is intially filled with few things(it turns out that these are nulls; 0 in ASCII). Can anyone explain why this is happening?
I noticed that if the String is smaller then everything works as expected and the Buffer will not be filled with nulls.

Arduino Code

void setup() {
  Serial.begin(9600);
//delay(1000)
 Serial.println("the quick brown fox jumps over the lazy dog");
 delay(1000);
}

void loop() {
}

Processing Code

import processing.serial.*;
Serial myPort;

void setup() {
  myPort= new Serial(this, "COM3", 9600);
}

void draw() {
  if (myPort.available()>0) {
    println("Buffer:"+myPort.available());
    String string = myPort.readStringUntil('\n');
    if (string!=null) {
      println("String:"+string);
    }
  }
}

Hi @maro, when I run your sketches I get:


Buffer:3
Buffer:18
Buffer:34
Buffer:45
String:the quick brown fox jumps over the lazy dog

I think what we are seeing is this. The characters are arriving over a period of time. At 9600 baud approximately 1 char/mS. myPort.available() is correctly showing how many have arrived. At the same time readStringUntil is giving you null because it doesn’t find a \n yet.

You can alter the situation for demonstration. Delay before reading, you won’t see the nulls because the \n has arrived. readStringUntil(‘q’) as it arrives sooner. Try a longer string, more null values waiting for the \n.

In most programs I prefer a frameRate(20) e.g., so I know how often I’m reading the port. It’s quite normal to readStringUntil many times, it returns null until what you want has arrived.

Hello @maro,

The buffer does not contain nulls.

See the reference:

Try this example to see what is received:
https://processing.org/reference/libraries/serial/Serial_readChar_.html

The ODOA (in hex) at the ends if from this:
https://www.arduino.cc/reference/en/language/functions/communication/serial/println/

Which states:

UPDATE after further examination:

The buffer does not contain nulls.

See updated post below:
https://discourse.processing.org/t/arduino-filling-the-serial-buffer-with-nulls/38674/14

:)

I am using an Ardiuno Uno genuine

Hello @glv,

here you can see that the buffer contains nulls. It is so confusing because sometimes nulls are throwen sometimes not. I had to upload and run the code couple of times until this case happened.

1 Like

IMO better to post code (and ouptut) as text not image. That way people e.g. me, can copy it with one click.

Hello @maro,

That visual helps to see the output.

Arduino Uno genuine or clone? We had issues with some clones one year…

I have experienced “garbage” in the buffers in the past:

I will explore this further when I have time… 12 hr days for me this time of year.

:)

With your code I don’t get any nulls, just the codes for the text as sent.

,C: 116,C: 104,C: 101,C: 32,C: 113,C: 117,C:...
      t           h         e       space   q         u

The same on Nano with FTDI chip, Nano or Mega with CH340 chip.

Re-loading the code into the Ard should not make any difference. In Ard sketch, please try that 1 Sec delay after Serial.begin. Do you have more than one Ard? Please try each that you have.

Hello @RichardDL
thanks for trying to help. I also tried with the delay after Serial.begin and got same problem.
It is wierd that this is only happening to my Arduino.

1 Like

Try using Serial.write() instead of Serial.println() and see of the null are illiminated.

I got also the same problem with Serial.write()

Try other PCs/Laptops/Mac/Pi.

Hello @maro,

Arduino Code
void setup() {
  Serial.begin(9600);
  Serial.println();
  Serial.println("0123456789ABCDEF");
  }

void loop() 
  {
  }
Processing Code
// 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

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:

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

:)

1 Like