OK, I really hope I'm doing something dumb, but I'm having all sorts of trouble with some of the serial functions under Teensy 4.
The issue I'm seeing is that the Serial.available() stops updating after the first call to it, unless there is a read performed. Here's an example sketch:
Basically the sketch should print out the number of bytes on the serial RX buffer. If the number on the buffer is greater than 10, it'll read all the values back out from the buffer.
However, if I run that sketch I start seeing 0 on the monitor as expected. If I send a byte of data, the value goes up to 1, again as you'd expect. But if I send another byte, it still stays as 1. Send another byte and its still 1 etc.
If I reset the board and this time instead send say 5 bytes the first time, it'll start showing 5, but just as before if I send more bytes, it still shows as 5.
If I send 11 bytes initially, then it'll print 11, then back to 0, which is correct.
Am I missing something obvious about how the Serial.available() function works or should it keep going up with each new byte received (Even if there isn't a Serial.read() call)?
The issue I'm seeing is that the Serial.available() stops updating after the first call to it, unless there is a read performed. Here's an example sketch:
Code:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
Serial.println(Serial.available());
if(Serial.available() > 10)
{
while(Serial.available())
{
Serial.read();
}
}
delay(1000); // delay in between reads for stability
}
Basically the sketch should print out the number of bytes on the serial RX buffer. If the number on the buffer is greater than 10, it'll read all the values back out from the buffer.
However, if I run that sketch I start seeing 0 on the monitor as expected. If I send a byte of data, the value goes up to 1, again as you'd expect. But if I send another byte, it still stays as 1. Send another byte and its still 1 etc.
If I reset the board and this time instead send say 5 bytes the first time, it'll start showing 5, but just as before if I send more bytes, it still shows as 5.
If I send 11 bytes initially, then it'll print 11, then back to 0, which is correct.
Am I missing something obvious about how the Serial.available() function works or should it keep going up with each new byte received (Even if there isn't a Serial.read() call)?