Question about .available()

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,

 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.

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

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.

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.

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.

Grumpy_Mike:
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.

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.

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?

How can I clear the buffer?

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

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

pYro_65:

How can I clear the buffer?

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

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

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

flush is for the output buffer only.

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

Block until all write() characters are really sent

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).

DOH! :blush:

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. :slight_smile:
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:

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?

flush is for the output buffer only.

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

pylon:

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.

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.

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.

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.

PaulS:
Almost never is my recommendation.

I have to agree.

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.

Other things I program has a ClearBuffer() function which comes in handi if I only need the first 4 bytes out of 100 or something. I can stop reading and clear the rest. But other than that, not too often do I not need all the information.