Serial.flush();

You can post your problem related to Arduino Projects here. We will try our best to help you out.
Post Reply
Brown
Posts:8
Joined:Fri Mar 03, 2017 1:10 pm
Serial.flush();

Post by Brown » Fri Mar 03, 2017 2:08 pm

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: Select all

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




Junaid_Shahid
Posts:110
Joined:Tue Feb 14, 2017 9:00 am

Re: Serial.flush();

Post by Junaid_Shahid » Fri Mar 03, 2017 2:08 pm

Brown wrote:
Fri Mar 03, 2017 2:08 pm
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: Select all

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



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:
HOW TO USE ARDUINO SERIAL FLUSH ?

Post Reply