Arduino Projects DISCUSSION

Serial.flush();

Started by syedzainnasir Serial.flush();
1 replies 248 views 2 participants
Latest activity · 03 Mar 2017

Serial.flush();

syedzainnasir Arduino Projects Forum
#1
hi there

I'm working with a bluegiga wt32 bluetooth adapter at the moment. I never worked with a serial interface so naturally, I do have some questions. generally, it works quite well, but there seem to be some problems with the serial buffer (or so I guess).

Initially I sent the serial commands just like this:
Code: [Select]
Serial.println("INQUIRY 1");


The problem with this was that somehow, when I read out the serial buffer, this command was sent/read out repeatedly.

I then modified the code like this:
Code: [Select]
Serial.println("INQUIRY 1");
delay(500);
Serial.flush();


This works better, but not in every case. And even more, I don't know why it works. If I for example remove the delay, I have the same problems again. Also, I was once told that delay should not be used...

Also, I don't know why it works with Serial.flush(). Doesn't this command delete the serial buffer? Shouldn't I then not be able to read what I have printed before?

Any help with this is appreciated...

By the way, I read out the serial buffer like this:[code] while(Serial.available() > 0){ message_part = Serial.read(); message[message_part_count] = message_part; message_part_count++; if (message_part == char(10)){ // only send out message if it's complete (with line feed) Serial.print(message); delay(500); Serial.flush(); reset_message(); } // end if } // end while [/code]

Community replies 1

Re: Serial.flush();

#2
[quote=Brown post_id=258 time=1488550082 user_id=84] hi there

I'm working with a bluegiga wt32 bluetooth adapter at the moment. I never worked with a serial interface so naturally, I do have some questions. generally, it works quite well, but there seem to be some problems with the serial buffer (or so I guess).

Initially I sent the serial commands just like this:
Code: [Select]
Serial.println("INQUIRY 1");


The problem with this was that somehow, when I read out the serial buffer, this command was sent/read out repeatedly.

I then modified the code like this:
Code: [Select]
Serial.println("INQUIRY 1");
delay(500);
Serial.flush();


This works better, but not in every case. And even more, I don't know why it works. If I for example remove the delay, I have the same problems again. Also, I was once told that delay should not be used...

Also, I don't know why it works with Serial.flush(). Doesn't this command delete the serial buffer? Shouldn't I then not be able to read what I have printed before?

Any help with this is appreciated...

By the way, I read out the serial buffer like this:[code] while(Serial.available() > 0){ message_part = Serial.read(); message[message_part_count] = message_part; message_part_count++; if (message_part == char(10)){ // only send out message if it's complete (with line feed) Serial.print(message); delay(500); Serial.flush(); reset_message(); } // end if } // end while [/code] [/quote]
yves--

One problem I can see in your code is that your "message" string is not terminated when you send it to Serial.println().

Try adding something like this:

Code: [Select]
if (message_part == char(10)){ // only send out message if it's complete (with line feed)
message[message_part_count - 1] = 0;
Serial.print(message);
...


Serial.print(string) sends characters until it reaches a 0 byte, so you need to make sure to put one there.

I don't think an application like this will really need to use flush().
Recommended:
[url=http://www.theengineeringprojects.com/2017/01/use-arduino-serial-flush.html]HOW TO USE ARDUINO SERIAL FLUSH ?[/url]
TEP COMMUNITY