我在ArduinoYún桥上进行了实验,并注意到将client.readStringUntil('/');
放入专用函数会使桥丢失剩余数据。
我在Mac上使用Arduino IDE 1.8.9
显示该错误的代码下方
//include + setup function skipped
void loop() {
delay(50); // Poll every 50ms
// Get clients coming from server
BridgeClient client = server.accept();
// Skip if there is no client
if (!client) {
return;
}
// Process request
processRequest(client);
// Close connection and free resources.
client.stop();
}
void processRequest(BridgeClient client) {
String someText = getString(client);
client.print(F("Text read: "));
client.println(someText);
int someNumber = client.parseInt();
client.print(F("Number read: "));
client.println(someNumber);
}
String getString(BridgeClient client){
return client.readStringUntil('/');
}
在Web浏览器上,我使用以下URL http://arduino.local/arduino/fade/300
。结果,浏览器显示
Text read: fade
Number read: 0
调查后,调用client.available()
函数后,可用字节数getString()
降为0。
奇怪的是,将getString()
调用替换为client.readStringUntil('/')
会使错误消失。并显示正确的数字。