I just wrote some code that just sends a string from Arduino to Processing.
Processing prints the string and displays 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 initially filled with a few things (it turns out that these are nulls; 0 in ASCII). Why is this happening?
And this mostly happens after uploading and starting Processing for the first time. When I start Processing for the second time, the buffer is initially free as it should be. I am using a genuine Arduino Uno. I also tested on another Arduino and another laptop, but there is still the same problem. So it should not be a hardware problem.
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);
}
}
}
In the image is other code just to show that the buffer contains nulls.