arduino Serial.read
时间: 2024-02-05 08:03:15 浏览: 78
serial_arduino
The Serial.read() function in Arduino is used to read incoming serial data from the serial port. It returns the first byte of incoming serial data available (or -1 if no data is available).
Syntax:
int Serial.read();
Example:
```
void setup() {
Serial.begin(9600); // initialize serial communication
}
void loop() {
if (Serial.available() > 0) { // check if there is incoming serial data
int incomingByte = Serial.read(); // read the incoming byte
Serial.print("Received: ");
Serial.println(incomingByte); // print the received byte
}
}
```
In this example, the Arduino board waits for incoming serial data and reads the first byte of the incoming data using the Serial.read() function. It then prints the received byte to the serial monitor.
阅读全文