I'm seeing some unexpected delay in the Serial1.available() returning a value after serial data is present. This is using a Teensy 3.2 with Arduino 1.8.15 and TD 1.54. My minimal test to show the issue has the Serial1 TX and RX pins tied together for loopback with this test code:
Scope trace looks like this:
If I run the same code an an Arduino Mega I don't see the same delay when Serial1.available() gets called:
Finally, if I go back to the Teensy 3.2 and replace the Serial1.available() wait with Serial1.flush() (to ensure that the byte has made it out before calling Serial1.read() ), the delay is gone and the values are read back correctly (confirmed by printing them out via USB serial):
I'm wondering what might be causing the delay in Serial1.available() reporting that data is available when a Serial1.read() call seems to be showing that data is actually there. Is this normal/expected? I'm not completely sure, but I think it may be causing me some timeout issues in some other legacy code that I believe was working in the past, but possibly with a different Arduino/TD version.
Thanks,
Andy
Code:
#define LED_PIN 13
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_PIN,OUTPUT);
Serial1.begin(19200);
}
void loop() {
// put your main code here, to run repeatedly:
uint8_t temp_byte = 0;
digitalWrite(LED_PIN,HIGH);
Serial1.write(0x55);
while (!Serial1.available()){}
temp_byte = Serial1.read();
//Serial.println(temp_byte);
digitalWrite(LED_PIN,LOW);
Serial1.write(0x02);
while (!Serial1.available()){}
temp_byte = Serial1.read();
//Serial.println(temp_byte);
digitalWrite(LED_PIN,HIGH);
delay(100);
digitalWrite(LED_PIN,LOW);
delay(300);
}
Scope trace looks like this:
If I run the same code an an Arduino Mega I don't see the same delay when Serial1.available() gets called:
Finally, if I go back to the Teensy 3.2 and replace the Serial1.available() wait with Serial1.flush() (to ensure that the byte has made it out before calling Serial1.read() ), the delay is gone and the values are read back correctly (confirmed by printing them out via USB serial):
Code:
#define LED_PIN 13
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_PIN,OUTPUT);
Serial1.begin(19200);
}
void loop() {
// put your main code here, to run repeatedly:
uint8_t temp_byte = 0;
digitalWrite(LED_PIN,HIGH);
Serial1.write(0x55);
Serial1.flush();
//while (!Serial1.available()){}
temp_byte = Serial1.read();
Serial.println(temp_byte);
digitalWrite(LED_PIN,LOW);
Serial1.write(0x02);
Serial1.flush();
//while (!Serial1.available()){}
temp_byte = Serial1.read();
Serial.println(temp_byte);
digitalWrite(LED_PIN,HIGH);
delay(100);
digitalWrite(LED_PIN,LOW);
delay(300);
}
I'm wondering what might be causing the delay in Serial1.available() reporting that data is available when a Serial1.read() call seems to be showing that data is actually there. Is this normal/expected? I'm not completely sure, but I think it may be causing me some timeout issues in some other legacy code that I believe was working in the past, but possibly with a different Arduino/TD version.
Thanks,
Andy