caramoan tour package

caramoan tour package

Author Topic: How to clear Serial.available()  (Read 406 times)

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
How to clear Serial.available()
« on: May 27, 2012, 11:27:15 AM »
Magtatanong lang po sana sir kung paano po i-clear yung laman ng Serial.available(). As I have read from thier site, yung Serial.available() daw po yung may hold ng string na pinasa from serial port. Gusto ko po sanang i-clear yung laman after receiving this type of character "~". Here was the code that I've made and tried for several times.

Code: [Select]
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

char txt;
int val;


void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("hello ASIMO");
  Serial.begin(9600);
}

void loop() {
 
  lcd.setCursor(16,0);
 
   
  while(Serial.available()>0)
  { 
     
      txt=Serial.read();
      val=txt;
     
      if (val==126)
      {
        break;
      }
     
      lcd.autoscroll();
      lcd.print(txt);
      delay(2000);
  }
 
  lcd.noAutoscroll();
  delay(5000);
  lcd.clear();
  lcd.setCursor(16,0);
}

Kung ipasa ko po 'to na string "hello elab~12345", yung makikita ko sa LCD ay hanggang hello elab lang which is TAMA. Gusto ko sanang hindi na ma-idisplay yung mga characters after "~" during second attempt. Yung nangyari po kasi, after ko i-try ulit magpadisplay ng string, lalabas muna yung "12345" which was the last 5 characters on the first attempt, bago ma-idisplay yung characters.

Salamat po.
Computer Engineering National Organization registration thread


Philippine Electronics Forum

How to clear Serial.available()
« on: May 27, 2012, 11:27:15 AM »

Offline RiDdLeR???

  • Hydroelectric
  • ***
  • Posts: 3032
  • Pogi/Ganda Points: 208
Re: How to clear Serial.available()
« Reply #1 on: May 28, 2012, 05:33:10 AM »
Hindi sa serial.avalable() yung kailangan i-clear,  serial.avalable() simply checks if there is new data in the serial buffer.  To clear the serial buffer, simply execute a serial.flush().

Philippine Electronics Forum

Re: How to clear Serial.available()
« Reply #1 on: May 28, 2012, 05:33:10 AM »

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
Re: How to clear Serial.available()
« Reply #2 on: May 28, 2012, 09:31:33 AM »
Okay na po sir. Salamat po.  173+1 po. :D
Computer Engineering National Organization registration thread


Philippine Electronics Forum

Re: How to clear Serial.available()
« Reply #2 on: May 28, 2012, 09:31:33 AM »

Offline glutnix_neo

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4162
  • Pogi/Ganda Points: 166
  • Gender: Female
  • A journey to a thousand lines begins w/ LED Blink
    • Underground Workbench
Re: How to clear Serial.available()
« Reply #3 on: May 28, 2012, 02:05:04 PM »
Hindi sa serial.avalable() yung kailangan i-clear,  serial.avalable() simply checks if there is new data in the serial buffer.  To clear the serial buffer, simply execute a serial.flush().


this works well sa unang versions ng IDE, unfortunately narepurpose ang Serial.flush() into something which will block succeeding instructions until all data are transmitted starting from Arduino v1.0 ...
If we hear,we forget;if we see, we remember;if we do,we understand.
Let's support the use of free and open source softwares...
http://UndergroundWorkbench.wordpress.com

Philippine Electronics Forum

Re: How to clear Serial.available()
« Reply #3 on: May 28, 2012, 02:05:04 PM »

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
Re: How to clear Serial.available()
« Reply #4 on: May 28, 2012, 05:45:07 PM »
Ahhh.. Oaky po sir. Bali, yung kukunin lang na data ng Serial.read() ay yung mga characters na naitransmit na before Serial.flush() is issued?
Computer Engineering National Organization registration thread


Philippine Electronics Forum

Re: How to clear Serial.available()
« Reply #4 on: May 28, 2012, 05:45:07 PM »

Offline glutnix_neo

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4162
  • Pogi/Ganda Points: 166
  • Gender: Female
  • A journey to a thousand lines begins w/ LED Blink
    • Underground Workbench
Re: How to clear Serial.available()
« Reply #5 on: May 28, 2012, 06:07:14 PM »
kung tama understanding ko before ang purpose ng flush is to clear the read buffer, ngayon eh magiintay sya ma maclear(maintransmit lahat) ang write buffer....


http://arduino.cc/en/Serial/Flush
Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)
If we hear,we forget;if we see, we remember;if we do,we understand.
Let's support the use of free and open source softwares...
http://UndergroundWorkbench.wordpress.com

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
Re: How to clear Serial.available()
« Reply #6 on: May 28, 2012, 06:28:33 PM »
kung tama understanding ko before ang purpose ng flush is to clear the read buffer, ngayon eh magiintay sya ma maclear(maintransmit lahat) ang write buffer....


http://arduino.cc/en/Serial/Flush
Waits for the transmission of outgoing serial data to complete. (Prior to Arduino 1.0, this instead removed any buffered incoming serial data.)

Eto na po ksi yung ginamit ko ngayon. ARDUINO 0022 po yung gamit ko and it's working fine po. Finlush na lahat lahat ng laman ng read.
Computer Engineering National Organization registration thread


Offline RiDdLeR???

  • Hydroelectric
  • ***
  • Posts: 3032
  • Pogi/Ganda Points: 208
Re: How to clear Serial.available()
« Reply #7 on: May 28, 2012, 07:13:12 PM »
 ??? ??? ???  medyo magulo na nga pagdating sa arduino 1.0!

Offline glutnix_neo

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4162
  • Pogi/Ganda Points: 166
  • Gender: Female
  • A journey to a thousand lines begins w/ LED Blink
    • Underground Workbench
Re: How to clear Serial.available()
« Reply #8 on: May 28, 2012, 09:00:49 PM »
hindi ko rin alam anong silbi ng flush sa arduino 1.0, blocking function pa syang maituturing....
If we hear,we forget;if we see, we remember;if we do,we understand.
Let's support the use of free and open source softwares...
http://UndergroundWorkbench.wordpress.com

Offline RiDdLeR???

  • Hydroelectric
  • ***
  • Posts: 3032
  • Pogi/Ganda Points: 208
Re: How to clear Serial.available()
« Reply #9 on: May 28, 2012, 09:34:21 PM »
Come to think of it, hindi na rin talaga kailangan i-flush ang serial receive buffer because serial.read will never read back the previous data.

Offline glutnix_neo

  • Technical People
  • Nuclear Reactor
  • *****
  • Posts: 4162
  • Pogi/Ganda Points: 166
  • Gender: Female
  • A journey to a thousand lines begins w/ LED Blink
    • Underground Workbench
Re: How to clear Serial.available()
« Reply #10 on: May 28, 2012, 09:39:23 PM »
usefull sana yun kung binavalidate yung mga byte, kapag nakadetect ng isang byte na mali, no need na basahin na lahat ng transmitted data, therefore pwede natin iflush to clear the buffers....
If we hear,we forget;if we see, we remember;if we do,we understand.
Let's support the use of free and open source softwares...
http://UndergroundWorkbench.wordpress.com

Offline ΔЅịMø

  • Gas Turbine
  • **
  • Posts: 2903
  • Pogi/Ganda Points: 140
  • Gender: Male
  • "Live Curious"
Re: How to clear Serial.available()
« Reply #11 on: May 28, 2012, 10:02:03 PM »
usefull sana yun kung binavalidate yung mga byte, kapag nakadetect ng isang byte na mali, no need na basahin na lahat ng transmitted data, therefore pwede natin iflush to clear the buffers....

Eto rin yung gusto kong mangyari sir. Meron kasing mga unwanted characters na na-didisplay sa serial monitor sa arduino. Kaya yung ginawa ko, naglagay ako nga extra character sa dulo ng string to signal the arduino na hanggang dun lang ang gusto ko ipa-display. Any character after that extra character will be flushed.
Computer Engineering National Organization registration thread


Philippine Electronics Forum

Re: How to clear Serial.available()
« Reply #11 on: May 28, 2012, 10:02:03 PM »

 

Privacy Policy

Contact Us: elabph@yahoo.com