0

This code is a part of a long code for understanding serial communication and the functions related to it.

char buffer[18];
int red, green, blue;
int RedPin = 11;
int GreenPin = 10;
int BluePin = 9;

void setup()
{
Serial.begin(9600);
Serial.flush();
pinMode(RedPin, OUTPUT);
pinMode(GreenPin, OUTPUT);
pinMode(BluePin, OUTPUT);
}

void loop()
{
if (Serial.available() > 0) {
int index=0;
delay(100); // let the buffer fill up
int numChar = Serial.available();
if (numChar>15) {
numChar=15;
}
while (numChar--) {
buffer[index++] = Serial.read();
}
splitString(buffer);
}
}

In void setup(), Serial.flush() is used. On internet, I have read that it flush out any character present in a serial line. After calling it we can be sure that all the data has been sent before moving on to the next line of the program.

In void loop(), in the first if condition, delay(100) ensures to fill the buffer. Basically, buffer is the place in memory where the received serial data is stored prior to processing.

The question is that is there any need of using delay(100), if we call the Serial.flush() initially because it ensures that all the serial data has been transmitted (as in serial transmission one bit is transmitted at a time)? Does it takes further time to store the transmitted data in the memory (buffer) and does this is the reason to use delay(100)? Please clarify why delay(100) is used after calling Serial.flush(), I am confused.

  • flush() changed usage in Arduino many years ago. the first use was to 'eat-up' all input. now it is flush the TX buffer. from change log for Arduino 1.0 "Serial.flush() now waits for transmission of outgoing data rather than discarding received incoming data." – Juraj Jul 6 at 16:26
  • Serial.flush() waits for transmission of outgoing data. But does this waiting time includes the storage of data in the buffer? As we use delay() in the further part of code. – Manu Jul 6 at 17:08
  • No. The use of flush here is in appropriate. It is not needed there. This was written by someone who doesn’t understand what it does. You can just remove that flush line. – Delta_G Jul 6 at 17:40
  • 1
    The delay in loop is unrelated. That’s waiting for the sender to finish sending. This is a poor blocking method. There are far better ways to write this code. You should probably put this one in the rubbish bin and find something written by someone who knows what they’re doing. – Delta_G Jul 6 at 17:42
  • flush() does nothing here. no data to flush are in transmit buffer at start. and how would it help with receiving? – Juraj Jul 6 at 17:50
2

The Serial.flush() is doing nothing. It delays until all bytes have been sent out of the UART. Since nothing has been sent it's not got anything to wait for.

The delay after the available call is very bad practice. It's a really really bad way of dealing with serial reading.

You should read this for a better understanding of serial communication.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.