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);
}
}
}
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.
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.
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.
// 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();
//******************************************
}
}
}