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.
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.
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?
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?
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
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...