流量网站福建网站开发定制
Arduino示例代码讲解:Serial Event example 连续事件例子
- Serial Event example 连续事件例子
- 功能概述
- 硬件部分:
- 软件部分:
- 代码逐行解释
- 定义变量
- `setup()` 函数
- `loop()` 函数
- `serialEvent()` 函数
- 工作原理
Serial Event example 连续事件例子
这段代码是一个Arduino示例程序,用于处理串行通信中的事件。它通过serialEvent()
函数接收串行数据,并将接收到的数据存储到一个字符串中。当接收到换行符时,标记字符串为完整,并在loop()
函数中打印该字符串。它适用于需要处理串行通信中的完整字符串的场景,例如接收GPS数据或其他串行设备发送的数据。
/*Serial Event exampleWhen new serial data arrives, this sketch adds it to a String.When a newline is received, the loop prints the string andclears it.A good test for this is to try it with a GPS receiverthat sends out NMEA 0183 sentences.Created 9 May 2011by Tom IgoeThis example code is in the public domain.http://www.arduino.cc/en/Tutorial/SerialEvent*/String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is completevoid setup() {// initialize serial:Serial.begin(9600);// reserve 200 bytes for the inputString:inputString.reserve(200);
}void loop() {// print the string when a newline arrives:if (stringComplete) {Serial