Serial.readStringUntil
时间: 2024-02-05 09:03:11 浏览: 136
Serial.readStringUntil() is a function in Arduino programming language that reads characters from the serial port and stores them in a string until a specified character is encountered. The function takes a single argument, which is the delimiter character that indicates the end of the string.
For example, the following code reads a string from the serial port until a newline character is encountered:
```
String inputString = "";
void setup() {
Serial.begin(9600);
}
void loop() {
if (Serial.available()) {
char c = Serial.read();
if (c == '\n') {
Serial.print("Input string: ");
Serial.println(inputString);
inputString = "";
} else {
inputString += c;
}
}
}
```
This code declares a string variable called `inputString` and initializes it to an empty string. In the `loop()` function, the code checks if there are any characters available on the serial port using the `Serial.available()` function. If there are, it reads the character using the `Serial.read()` function and checks if it is a newline character (`\n`). If it is, it prints the input string to the serial monitor and resets the string to an empty value. If it is not a newline character, it appends the character to the `inputString` variable using the `+=` operator.
阅读全文