Go Down

Topic: Question about .available() (Read 1 time) previous topic - next topic

Jassper

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: [Select]

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.

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.

pylon

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.

PaulS

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.


Jassper


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?

pYro_65

Quote
How can I clear the buffer?


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

Code: [Select]
while( Serial.available() ) Serial.Read();

Jassper


Quote
How can I clear the buffer?


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

Code: [Select]
while( Serial.available() ) Serial.Read();


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

pYro_65

flush is for the output buffer only.

michael_x

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


Block until all write() characters are really sent

pylon

Code: [Select]
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).

Jassper

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

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?





pylon

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.

Jassper


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.


dxw00d

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.

PaulS

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.

Go Up