Serial.getTimeout() returns the timeout value set by Serial.setTimeout().
Serial.getTimeout() inherits from the Stream utility class.
Use the following function to get the timeout value previously set:
Serial.getTimeout()
The function admits the following object:
Serial: serial port object. See the list of available serial ports for each board on the Serial main page.
The function returns the timeout value set by Serial.setTimeout(). Data type: unsigned long.
The following code modifies the default serial timeout value and asks you to write something in the given time:
void setup() {
Serial.begin(9600);
while (!Serial); // Wait for Serial to be ready (USB boards)
Serial.print("Current timeout (ms): ");
Serial.println(Serial.getTimeout()); // Print default timeout (should be 1000)
Serial.setTimeout(5000); // Set a custom timeout (5 seconds)
Serial.print("New timeout set to: ");
Serial.println(Serial.getTimeout()); // Now should be 5000
Serial.println("Type something within 5 seconds:");
}
void loop() {
char buffer[20];
int bytesRead = Serial.readBytes(buffer, sizeof(buffer) - 1); // Waits up to timeout
if (bytesRead > 0) {
buffer[bytesRead] = '\0'; // Null-terminate the string
Serial.print("Received: ");
Serial.println(buffer);
} else {
Serial.println("Timeout! No data received.");
}
delay(3000);
}