Serial.setTimeout()
설명
Serial.setTimeout()직렬 데이터를 기다리는 최대 밀리 초를 설정합니다. 기본값은 1000 밀리초(= 1초)입니다.
Serial.setTimeout()은 Stream 유틸리티 클래스에서 상속합니다.
문법
Serial.setTimeout(time)
매개변수
- time : 대기 시간 (밀리 초, 1000밀리초 = 1초). 허용되는 데이터 유형 : long.
반환값
- 없음
예제 코드
- 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
- 시리얼 모니터에 결과:
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
- 시리얼 모니터에 결과:
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.
※ 주의 및 경고:
- 다음에 오는 함수들의 이 함수의 영향을 받습니다:
- 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)
더보기
- 언어 : Arduino - Serial
- 언어 : Serial.available()
- 언어 : Serial.begin()
- 언어 : Serial.end()
- 언어 : Serial.find()
- 언어 : Serial.findUntil()
- 언어 : Serial.flush()
- 언어 : if(Serial)
- 언어 : Serial.parseFloat()
- 언어 : Serial.parseInt()
- 언어 : Serial.peek()
- 언어 : Serial.print()
- 언어 : Serial.println()
- 언어 : Serial.read()
- 언어 : Serial.readBytes()
- 언어 : Serial.readString()
- 언어 : serialEvent()
- 언어 : Serial.write()
- 언어 : stream
- 언어 : stream.setTimeout()
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.