Arduino 精要
来自Jack's Lab
(版本间的差异)
(→糖豆) |
(→AT CMD) |
||
第69行: | 第69行: | ||
void loop() { | void loop() { | ||
− | |||
if (str_over) { | if (str_over) { | ||
− | + | /* Identify the CMD and processing */ | |
− | input_str = ""; // clear the string | + | |
+ | input_str = ""; // clear the string buffer | ||
str_over = false; | str_over = false; | ||
} | } | ||
第78行: | 第78行: | ||
/* | /* | ||
− | + | * SerialEvent occurs whenever a new data comes in the hardware serial RX. | |
− | + | * This routine is run between each time loop() runs, so using delay inside | |
− | + | * loop can delay response. Multiple bytes of data may be available. | |
− | + | */ | |
− | + | ||
void serialEvent() { | void serialEvent() { | ||
while (Serial.available()) { | while (Serial.available()) { | ||
− | |||
char in = (char)Serial.read(); | char in = (char)Serial.read(); | ||
input_str += in; | input_str += in; | ||
− | + | ||
− | + | if (in == '\n') { // read a line into string buffer | |
− | if (in == '\n') { | + | |
str_over = true; | str_over = true; | ||
} | } |
2014年11月9日 (日) 01:27的版本
目录 |
1 总览
2 扩展
3 应用
4 精华
5 糖豆
5.1 float2str
String(NUMBER, SCALE); String(3.141592,5); # "3.14159"
5.2 Software Serial
5.3 AT CMD
SerialEvent() is called after a loop(), if there is serial data in the buffer.
String input_str = ""; // a string to hold incoming data boolean str_over = false; // whether the string is complete void setup() { Serial.begin(9600); input_str.reserve(200); // reserve 200 bytes for the inputString: } void loop() { if (str_over) { /* Identify the CMD and processing */ input_str = ""; // clear the string buffer str_over = false; } } /* * SerialEvent occurs whenever a new data comes in the hardware serial RX. * This routine is run between each time loop() runs, so using delay inside * loop can delay response. Multiple bytes of data may be available. */ void serialEvent() { while (Serial.available()) { char in = (char)Serial.read(); input_str += in; if (in == '\n') { // read a line into string buffer str_over = true; } } }