Is Serial.println() (using hardware serial port) is a blocking function?

I am using hardware serial port for sending text streams to a BLE device. Assume that the serial buffer is totally empty, and I sent some text stream of size 48 bytes to the hardware serial port. Now, is the program execution stops at Serial.println("text stream of 48 bytes"); until all the bytes have been sent? The responses I received from chat GPT & Gemini (for the above question) are quite opposite.. I have attached the images of responses.

Chat GPT response:

Gemini response:

Those tools usually produce very well articulated nonsense.

Using the hardware serial port (USART) is not blocking.

You better tell which Arduino you are using and post your sketch, so people can try it for you.

it's very common for serial transmission (e.g. rs-232-, ethernet, bluetooth, WiFi) to buffer data and use interrupts to transmit data as required. the HardwareSerial interface is similar with, my understanding, a 64 byte buffer

when you queue 48 bytes for transmission, those 48 bytes are copied into the 64 byte output buffer, the print() returns and foreground processing continues while interrupts move bytes from the buffer to the UART output registers as each byte is transmitted

of course the above depends on if the 64 byte buffer is free. if it is not, the print() will wait until it is able to copy the last byte being queued to the buffer.

3 Likes

Thanks for the answer!!! I am using Atmega328 standalone with external 8mhz crystal.. as my project is a battery powered one...

You could have simply tested this yourself; your phrasing of the question to the AIs, and to us, clearly indicates you have the knowledge to do so. However, it is amusing that you came to the human forum only after getting conflicting AI answers - had they answered the same way, instead of conflicting, how would you have known if they were both wrong, or both right?

3 Likes

No.

1 Like

For the 328 the serial buffer is defined as 64 bytes

I suspect you have another error in your code that is stopping execution.

You don't show your sketch. If you repeatedly send your bytes in loop, are you sure the buffer is empty when you send the second block? And what baud rate are you using?

@MicroBahner
I think the question was more of a "what if I do this...", not a "I have this problem...".

1 Like

Chat GPT was fairly close, apparently it does not know about Serial.flush() or Serial.availableForWrite().

Gemini is utter nonsense.

2 Likes

As you suggested, I have tested the same with the following code:

unsigned long startTime, difference;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  char str1[] = "This is a sample text for transmission via serial port";

  Serial.print("length of string: "); Serial.println(strlen(str1));
  Serial.flush();

  startTime = micros();
  Serial.println(str1);
  difference = micros() - startTime;
  Serial.flush();
  Serial.print("difference micro sec: "); Serial.println(difference);
  Serial.flush();

  startTime = micros();
  Serial.println(str1); Serial.flush();
  difference = micros() - startTime;
  Serial.print("difference micro sec (after flushing): "); Serial.println(difference);

  while(1){
    // stop here
  }

}

The output I got on serial monitor:

14:04:53.137 -> length of string: 54

14:04:53.137 -> This is a sample text for transmission via serial port

14:04:53.217 -> difference micro sec: 396

14:04:53.252 -> This is a sample text for transmission via serial port

14:04:53.286 -> difference micro sec (after flushing): 58280

Conclusion: Serial.println() is a non-blocking function... using this, in conjunction with Serial.flush() in a loop allows us to achieve multi tasking as shown in below example:

for(int i=0; i<1000; i++){
// few lines of code
Serial.flush();
Serial.println("text stream to be sent out via serial port");
}

I have implemented the above loop in my project & it is working perfectly...

1 Like

@venkat_yalamati, as can be seen in your output: Serial.flush() is a blocking function.

At 9600 baud, transmission rate is roughly 1 byte per millisecond. So your flush will block for 54 approximately milliseconds.

Understood... thank you!!

Gee... In the Gemini version, it probably takes a long time to "convert the text data into individual bytes".

What a load of BS!

Yes... Serial.flush() is a blocking function..

Yes... I am using Serial.flush() before sending data through Serial.println(), for ensuring that my serial buffer is empty...

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