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.
-
2Is is an Arduino or an ESP? (Because the latter prints some data on boot, using a different baud rate, IIRC: 76800. This MAY result in nulls if you try to read them using a different baud rate than they're sent in, but usually results in garbage.)orithena– orithena2022年09月08日 11:00:51 +00:00Commented Sep 8, 2022 at 11:00
-
2The Arduino that I got is genuine not a clone.Robot12– Robot122022年09月09日 16:42:32 +00:00Commented Sep 9, 2022 at 16:42
-
1Provide versions numbers of everything involved like I did in explaining my attempt to replicate your problem.timemage– timemage2022年09月13日 17:03:32 +00:00Commented Sep 13, 2022 at 17:03
-
1versions are the same as yours. Arduino 1.8.19, Processing 4.0.1Robot12– Robot122022年09月13日 17:22:52 +00:00Commented Sep 13, 2022 at 17:22
-
1What happens if you connect a 10 kOhm resistor from D1 to 5V?timemage– timemage2022年09月13日 17:57:14 +00:00Commented Sep 13, 2022 at 17:57