Serial.setTimeout()
Descrição
Serial.setTimeout() configura o número máximo de milissegundos a se esperar por dados seriais. O valor padrão é de 1000 milisegundos.
A função Serial.setTimeout() é herdada da classe Stream.
Sintaxe
Serial.setTimeout(tempo)
Parâmetros
- Serial: objeto porta serial. Veja a lista de portas seriais disponíveis em cada placa no Serial - Página principal
- tempo : tempo limite em milisegundos (long).
Retorna
Nada
Código de Exemplo
- Compile and upload the below code to Arduino
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.setTimeout(100); // set new value to 100 milliseconds
Serial.print("Timeout: ");
Serial.println(Serial.getTimeout()); // print the new value
}
void loop() {
// check if data is available
if (Serial.available() > 0) {
// read the incoming string:
String incomingString = Serial.readString();
// prints the received data
Serial.print("I received: ");
Serial.println(incomingString);
}
}
- Type "Arduino" on Serial Monitor and click Send button:
COM6
Timeout: 100
Autoscroll
Clear output
9600 baud
Newline
- The result on Serial Monitor:
COM6
Timeout: 100
I received: Arduino
Autoscroll
Clear output
9600 baud
Newline
- Modify the code to change timeout from 100ms to 1ms
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
Serial.setTimeout(1); // set new value to 1 milliseconds
Serial.print("Timeout: ");
Serial.println(Serial.getTimeout()); // print the new value
}
void loop() {
// check if data is available
if (Serial.available() > 0) {
// read the incoming string:
String incomingString = Serial.readString();
// prints the received data
Serial.print("I received: ");
Serial.println(incomingString);
}
}
- Compile and upload the above code to Arduino
- Type "Arduino" on Serial Monitor and click Send button:
COM6
Timeout: 100
I received: Arduino
Timeout: 1
Autoscroll
Clear output
9600 baud
Newline
- The result on Serial Monitor:
COM6
Timeout: 100
I received: Arduino
Timeout: 1
I received: A
I received: r
I received: d
I received: u
I received: i
I received: no
Autoscroll
Clear output
9600 baud
Newline
When timeout is small, a single string is read multiple time.
※ Notas e Advertências:
- Funções da classe Serial que usam o valor de tempo limite configurado via Serial.setTimeout():
- Serial functions are not only used for the communication between an Arduino board and Serial Monitor of Arduino IDE but also used for the communication between:
- An Arduino board and other Arduino board
- An Arduino board and other sensors/devices
- An Arduino board and computer (any Serial software on computer)
Ver Também
- Linguagem : Arduino - Serial
- Linguagem : Serial.available()
- Linguagem : Serial.availableForWrite()
- Linguagem : Serial.begin()
- Linguagem : Serial.end()
- Linguagem : Serial.find()
- Linguagem : Serial.findUntil()
- Linguagem : Serial.flush()
- Linguagem : Serial.getTimeout()
- Linguagem : if(Serial)
- Linguagem : Serial.parseFloat()
- Linguagem : Serial.parseInt()
- Linguagem : Serial.peek()
- Linguagem : Serial.print()
- Linguagem : Serial.println()
- Linguagem : Serial.read()
- Linguagem : Serial.readBytes()
- Linguagem : Serial.readBytesUntil()
- Linguagem : Serial.readString()
- Linguagem : Serial.readStringUntil()
- Linguagem : serialEvent()
- Linguagem : Serial.write()
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.
Additionally, some links direct to products from our own brand, DIYables.