The role of version Serial.flush () prior to arduino1.0 is to empty the serial cache (dropping received incoming data). But after 1.0 the function of version Serial.flush () is adjusted in order to wait for the serial port data to be transmitted (Waits for the transmission of outgoing Serial data to complete.) (Referenced from http:// Arduino.cc/en/serial/flush).
Because I am in the process of using Wifibee, if both want to use serial.print through the serial port to the WiFi module to send instructions, and want to use Serial.print to show the module through the serial port value, for only a set of serial port Arduino Uno, The contents of these two serial.print will be sent to the module, that is, I will send the instruction and module return to the module respectively. If the content returned by the module is printed out, although we can see it on the serial monitor, it will be sent to the module at the same time, which is a wrong instruction for the module and certainly cannot be returned correctly. At the same time, the wrong instruction will get the wrong return and continue to send the module.
The answer I found on Baidu is this:
If you still need to clear the serial port cache, you can use: while (Serial.read () >= 0) {} instead.
This is a wait loop, Serial.read () returns 1 when the buffer has no data, and Serial.read () returns the corresponding character when the cache has data (the first byte of incoming serial data available (or -1 If no data is available)). Therefore, when there is no data in the buffer, do nothing to ensure that the buffer does not enter the data. function serial.available () is the function that returns the number of characters currently remaining in the serial buffer, which can buffer up to 128 bytes in the serial buffer according to the description of the function provided by Arduino.
Reprint: http://blog.csdn.net/opalefire/article/details/10066781
About Arduino emptying the serial cache (GO)