About Arduino emptying the serial cache

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.