Contribute
EN / USD
Log in / Join
32
of 76
TEP , The Engineering Projects , Image

syedzainnasir

TEP , The Engineering Projects , Rating 7.5 7.5 / 10
TEP , The Engineering Projects , Icon Level: Moderator
TEP , The Engineering Projects , Icon Joined: 20 Mar 2022
TEP , The Engineering Projects , Icon Last Active: 2:21 PM
TEP , The Engineering Projects , Icon Location: TEP , The Engineering Projects , Flag
TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon
Serial.flush();
TEP , The Engineering Projects , Calender Question: 03-Mar-2017
TEP , The Engineering Projects , Category In: Arduino Projects
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]
TEP , The Engineering Projects , Icon Answer: 1 TEP , The Engineering Projects , Icon Views: 150 TEP , The Engineering Projects , Icon Followers: 85
Small Bio
TEP , The Engineering Projects , Image

adiono

TEP , The Engineering Projects , Rating 7.5 7.5 / 10
TEP , The Engineering Projects , Icon Level: Moderator
TEP , The Engineering Projects , Icon Joined: 20 Mar 2022
TEP , The Engineering Projects , Icon Last Active: 2:21 PM
TEP , The Engineering Projects , Icon Location: TEP , The Engineering Projects , Flag
TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon TEP , The ENgineering Projects , Icon

RE:

Serial.flush();
TEP , The Engineering Projects , Calender Comment: 03-Mar-2017
TEP , The Engineering Projects , Category In: Arduino Projects
[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 , The Engineering Projects , Tick Correct Answer
Comment Bio
TEP , The Engineering Projects , Tags
PLC
Robot
STM32
Arduino
AI
ESP32
Ladder Logic
PLC Projects
Programming
Communicates STM32
PLC Projects
Communicates PLC
Font Style
Alignment
Indenting and Lists
Insert Media
Insert Items

Want to leave an answer!

Word Count :0 Draft Saved at 12:42 am.