Pages: [1] 2   Go Down
Author Topic: Question about .available()  (Read 1571 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From what I understand the serial.available() reports how many characters are available in the buffer, so checking if(serial.available()) will return TRUE is 1 or more characters are in the buffer.

It is also my understanding that reading the character from the buffer, serial.read() will remove that character from the buffer so once the character is read, the if statement should return false.

Why then does the serial.available() always report > 0 once it is triggered?

Maybe this is just an issue with software serial but basically,
Code:
while(LCDserial.available() > 0)
    {
    c = LCDserial.read();
    if(c == 13) // CR
      {
      ready = true;
      break;
      }
This little block ALWAYS reports true once the data is available on the buffer, reading the data in does not seem to remove it from the buffer and using the .flush() has no effect. So once the data is read and no more is coming in, the statement should no longer be true.

I'm obviously misunderstanding how this works.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 627
Posts: 34246
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is LCDserial a software serial library? Or is it a way of sending data to a serial LCD as the name would imply.
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What version of which library do you use? In my version of the SoftwareSerial libary (IDE 1.0) the byte read is removed from the buffer.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 617
Posts: 49453
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
From what I understand the serial.available() reports how many characters are available in the buffer, so checking if(serial.available()) will return TRUE is 1 or more characters are in the buffer.
No. Serial.available() returns the number of bytes available to be read. It does not return TRUE or FALSE.

Quote
It is also my understanding that reading the character from the buffer, serial.read() will remove that character from the buffer so once the character is read, the if statement should return false.
No. Reading a character will reduce, by one, the number of bytes available to be read.

We do, of course, need to know about what LCDserial is an instance of.

Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is LCDserial a software serial library? Or is it a way of sending data to a serial LCD as the name would imply.


Arduino 1.0, using Software Serial on pins 7 and 8.

Quote
No. Serial.available() returns the number of bytes available to be read. It does not return TRUE or FALSE.
Re-read. I was stating that the IF statement would return TRUE, if 1 or more characters were available.

Quote
No. Reading a character will reduce, by one, the number of bytes available to be read.
Think thats what I said, "serial.read() will remove that character from the buffer..."


So, that being said, I must be missing a piece of data that I am not reading out ???
How can I clear the buffer?
Logged

North Queensland, Australia
Online Online
Edison Member
*
Karma: 70
Posts: 2172
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can I clear the buffer?

The same way you clear the data you want, by reading.

Code:
while( Serial.available() ) Serial.Read();
Logged


0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can I clear the buffer?

The same way you clear the data you want, by reading.

Code:
while( Serial.available() ) Serial.Read();

But shouldn't serial.flush() clear it? if not, then what is it for?
Logged

North Queensland, Australia
Online Online
Edison Member
*
Karma: 70
Posts: 2172
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

flush is for the output buffer only.
Logged


Germany
Offline Offline
Faraday Member
**
Karma: 59
Posts: 3060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But shouldn't serial.flush() clear it? if not, then what is it for?

Block until all write() characters are really sent
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
void SoftwareSerial::flush()
{
  if (!isListening())
    return;

  uint8_t oldSREG = SREG;
  cli();
  _receive_buffer_head = _receive_buffer_tail = 0;
  SREG = oldSREG;
}

So, yes, it clears the buffer (if it's the active one).
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

DOH!  smiley-red

Just found it.
It appears that the LCD panel I am talking to sends a CR out every second. Why I don't know. Nothing in the documentation about it.
So there is always Serial data available. smiley
It also sends a CR after each command, Which is what I was listening for to know that all the data was in. Then I would do something with the data. so I will need to figure out how to tell the difference so ready is not true each time a CR comes in.

But thanks anyways, it was inspiring smiley-grin

Quote
flush is for the output buffer only.
Aha. OK. But that raises the question if it waits until all data is sent, again, whats  it for? Once all data is sent, is it not clear?




Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5152
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
flush is for the output buffer only.

This is not true as the posted code from SoftwareSerial show. It clear the input buffer only.
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
flush is for the output buffer only.

This is not true as the posted code from SoftwareSerial show. It clear the input buffer only.

OK, can I get a good explanation of what the flush() does and when it is good to use it?

if it clears the input only, then why must it wait until all outgoing data is sent. Is it the same buffer?

It almost seems that .flush() is one of those things that really isn't needed, but it's there.

Logged

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It depends on whether you are using hardware or software serial. In software serial, it clears the input buffer, as shown earlier. In hardware serial, it depends on which version of the IDE you have. From IDE 1.0 onward, it blocks until the output buffer is empty, this is because hardware serial output became interrupt driven, so serial output calls no longer block. Prior to IDE 1.0, it cleared the input buffer.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 617
Posts: 49453
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
OK, can I get a good explanation of what the flush() does
Depends on which class implements the method, on which version of the IDE.

SoftwareSerial and Serial do very different things in the flush() method.

Quote
and when it is good to use it?
Almost never is my recommendation.

For the situation where flush() waits for the output buffer to be empty, there may be times when you want to wait until all data has been sent, so that you can time how long it takes to get a response. But, that's a pretty rare case, and the timing depends on too many factors to be reliable.

For the situations where flush() dumps random amounts of unread data, I can't think of a single case where NOT reading the data is appropriate.
Logged

Pages: [1] 2   Go Up
Jump to: