Serial.setTimeout()

Beschreibung

Serial.setTimeout() legt die maximalen Millisekunden für das Warten auf serielle Daten fest. Der Standardwert ist 1000 Millisekunden.

Serial.setTimeout() erbt von der Stream-Dienstklasse.

Syntax

Serial.setTimeout(time)

Parameter

  • Serial: Serielles Port-Objekt. Die Liste der verfügbaren seriellen Anschlüsse für jedes Board auf der Serial-Hauptseite.
  • time: Timeout-Dauer in Millisekunden. Erlaubte Datentypen: long.

Rückgabewert

Nichts.

Beispielcode

  • 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:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Timeout: 100
Ln 11, Col 1
Arduino Uno on COM15
2
  • Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Timeout: 100 I received: Arduino
Ln 11, Col 1
Arduino Uno on COM15
2
  • 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:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Timeout: 100 I received: Arduino Timeout: 1
Ln 11, Col 1
Arduino Uno on COM15
2
  • Das ergebnis am seriellen monitor:
Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
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
Ln 11, Col 1
Arduino Uno on COM15
2

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

※ Anmerkungen und Warnungen:

Siehe Auch

ARDUINO KAUFEMPFEHLUNG

Arduino UNO R3
Arduino Starter Kit
Bitte Beachten: Dies sind Partnerschaftslinks. Wenn Sie die Komponenten über diese Links Kaufen, können wir eine Provision erhalten, ohne weitere Kosten für Sie. Wir Schätzen es.

※ OUR MESSAGES