Serial.setTimeout()

Description

Cette fonction fixe le délai maximum en millisecondes à attendre pour la lecture de flux de données. La valeur par défaut est 1000 millisecondes (=1 seconde).

En pratique, ce délai est utilisé par les fonctions de la classe Serial suivantes:

Syntaxe

Serial.setTimeout(time)

Paramètres

  • Serial: tout objet d'une classe utilisant la classe Serial
  • time: délai maximum en millisecondes à attendre pour la lecture de flux de données (long)

Valeurs Renvoyées

  • Aucune

Exemple

  • 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
    Send
    Timeout: 100
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • The result on Serial Monitor:
  • COM6
    Send
    Timeout: 100 I received: Arduino
    Autoscroll Show timestamp
    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
    Send
    Timeout: 100 I received: Arduino Timeout: 1
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  
  • The result on Serial Monitor:
  • COM6
    Send
    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 Show timestamp
    Clear output
    9600 baud  
    Newline  

When timeout is small, a single string is read multiple time.

※ Remarque:

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)

Voir Également

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

※ OUR MESSAGES

  • We are AVAILABLE for HIRE. See how to hire us to build your project
  • Any suggestion, correction, and translation? please email us at ArduinoGetStarted@gmail.com, We appreciate it
  • We mainly keep improving the references in English. See English version of this page for the latest update.