[Solved] I don't get why print is suddenly faster

I was trying to figure out approximately how long it takes to send data with my board via serial... and I found out something I do not manage to explain to myself. I used the following code :

long tini = 0;
int compt = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  //  if(compt==0){
  int value = 10100;
  tini = micros();
  // }
  uint8_t b2send[5]={10100};
  Serial.print("2000|2000|4333|4444|4844|nog|");
  long tf = micros() - tini;
  Serial.print("Time : ");
  Serial.println(tf);
  delay(10);
}

When I comment the delay(10) line I get around 50µs and with the line I get around 10µs???
(Also it crashes without the delay line but that I can understand :slight_smile: )

You're timing the period it takes to write the data to the print buffer, not the time it takes to print.
Try Serial.flush()

Serial.print() is very fast when there is space left in the output buffer. Once the buffer is full, the Serial.print() has to wait for characters to leave the buffer before it can put more characters in.

Thanks for your answers, that brings me to a few questions :

  1. Where can I find information on the buffer size? (So that I can optimize the size of what I send)

  2. I tried Serial.flush() , if I understood correctly the Arduino Reference it is supposed to act as a delay waiting for the buffer to be free. So how come my board manages to crash even though the buffer should be free after each loop iteration??

Google. Use terms like "Arduino", "serial", "buffer", "size" and maybe the model of Arduino you are using.

Do you mean when you include Serial.flush() it should be empty? Post your updated code and explain what you see when the 'crash' happens.

Do you mean when you include Serial.flush() it should be empty?

Yes that's what I understood. As for my code I just replaced the delay(10) line with a Serial.flush(), the 'crash' is my way of saying the serial communication stops writing to the COM port after a random number of loop iterations.

long tini = 0;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  tini = micros();
  Serial.print("2000|2000|4333|4444|4844|nog|");
  long tf = micros() - tini;
  Serial.print("Time : ");
  Serial.println(tf);
  Serial.flush();
}

You're still timing how long it takes to buffer the result, not to send it out.

Like that?

void loop() {
  // put your main code here, to run repeatedly:
  int value = 10100;
  tini = micros();
  Serial.print("2000|2000|4333|4444|4844|nog|");
  Serial.flush();
  long tf = micros() - tini;
  Serial.print("Time : ");
  Serial.println(tf);
  //Serial.flush();
}

Or like that?

void loop() {
  // put your main code here, to run repeatedly:
  int value = 10100;
  
  Serial.print("2000|2000|4333|4444|4844|nog|");
 tini = micros();
  Serial.flush();
  long tf = micros() - tini;
  Serial.print("Time : ");
  Serial.println(tf);
  //Serial.flush();
}

(it keeps crashing,for both codes I get that I should do around a 1000 iteration of the operation to be more accurate but since it crahes way before...)

Serial.availableForWrite() will tell you how much space is still availble in the software output buffer.

2 Likes

You must use 'unsigned long' for 'tini' and 'tf'.

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.