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,
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.
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.
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
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?
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.
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.