I have coded arduino for DHT11 which used to print humidity in first and temperature in second line. But some times when I open SerialPort I get absurd data.enter image description here
-
Possible duplicate of stackoverflow.com/questions/10105666/…v7d8dpo4– v7d8dpo42016年10月18日 02:18:54 +00:00Commented Oct 18, 2016 at 2:18
-
I am sending this data from arduino to pi serially and from pi to cloud. So some times I used to receive humidity as 6767.0 on the cloud. I am printing on console just for showing an example.Shubham Rajput– Shubham Rajput2016年10月19日 09:31:55 +00:00Commented Oct 19, 2016 at 9:31
2 Answers 2
You can't.
The data you are seeing has already been sent by the previous execution of the sketch before the main MCU was reset. That data resides in a small buffer within the USB interface chip (or even within the USB CDC/ACM / USB UART driver itself, depending on your OS) to which you have no access.
You just have to ignore that data. The simplest way is to put a marker at the start of your program to indicate that whatever is receiving the data should start its receiving from this point on. Something like:
Serial.println();
Serial.println("START");
-
You could disable reset, by adding a capacitor between GND and RESET. Though this make uploading new code less convenient.Gerben– Gerben2016年10月13日 12:27:14 +00:00Commented Oct 13, 2016 at 12:27
-
1@Gerben It also means that you can start reading partway through a line of digits giving you similar garbage at the start of your reception. Doesn't really cure the problem. Better is to use a proper protocol rather than plain ASCII text.Majenko– Majenko2016年10月13日 12:28:23 +00:00Commented Oct 13, 2016 at 12:28
-
Good point. Didn't think of that.Gerben– Gerben2016年10月13日 14:14:42 +00:00Commented Oct 13, 2016 at 14:14
-
I am sending this data from arduino to pi serially and from pi to cloud. So some times I used to receive humidity as 6767.0 on cloud. I am printing on console just for showing an example. So is there no way to clean output buffer of arduino when ever it starts.?Shubham Rajput– Shubham Rajput2016年10月19日 09:34:05 +00:00Commented Oct 19, 2016 at 9:34
I've circumvented this issue by sending a byte to the Arduino whenever I'm ready to receive data using this quick-and-dirty method:
if(Serial.available() > 0) { // At least one byte is available
Serial.println(millis());
Serial.read(); // Read the (one) character to empty the buffer
}