Closed GoogleCodeExporter closed 9 years ago
Here' another example, testing Stream.find() against the older TextFinder
library find():
Given the following JSON:
{"messages":177,"Kbytes":56047,"unread":43}
The following TextFinder example finds all three values:
while (client.available()) {
if (finder.find("{")) {
long messages = finder.getValue();
long kilobytes = finder.getValue();
long unread = finder.getValue();
Serial.print("messages: ");
Serial.println(messages);
Serial.print("kilobytes: ");
Serial.println(kilobytes);
Serial.print("unread: ");
Serial.println(unread);
}
}
Result:
messages: 177
kilobytes: 56047
unread: 43
The following Stream.find() example hangs at the first find():
if (client.find("{")) {
long messages = client.parseInt();
long kilobytes = client.parseInt();
long unread = client.parseInt();
Serial.print("messages: ");
Serial.println(messages);
Serial.print("kilobytes: ");
Serial.println(kilobytes);
Serial.print("unread: ");
Serial.println(unread);
} else {
Serial.println("I failed");
}
}
Original comment by tom.i...@gmail.com
on 2 May 2012 at 5:31
In the first example, the problem isn't that findUntil() is consuming extra
characters, but that the remaining characters haven't yet arrived over the
serial stream when Serial.available() is called. That is, Serial.findUntil()
returns immediately upon receipt of the ',' character, at which point there are
0 characters available. Then, Serial.findUntil() runs again, this time on
"345\n", returning 0 and consuming the remaining characters. If you add a
delay (e.g. 200 ms) after the Serial.print("\t"); you'll see the expected
output of "1 4".
The second example works for me, if I change "client" to "Serial", although
it's possible there's some issues with the interaction between the Ethernet
client and the find function.
Original comment by dmel...@gmail.com
on 9 May 2012 at 12:52
It doesn't work on Ethernet or Wifi. Please try it there, as described. Don't
mark an error "invalid" until you've tested it properly, at least as reported.
The problem was not with Serial, it was with Ethernet and Wifi.
The delay is not necessary with the TextFinder version of these commands.
Perhaps we could go with their algorithms, since they are better?
Original comment by tom.i...@gmail.com
on 9 May 2012 at 1:36
Can you post the full code for the Ethernet example? How did you test it?
Original comment by dmel...@gmail.com
on 9 May 2012 at 2:12
I can close this, right?
Original comment by dmel...@gmail.com
on 23 May 2012 at 2:59
Yep,
t.
Original comment by tom.i...@gmail.com
on 23 May 2012 at 6:19
Original issue reported on code.google.com by
tom.i...@gmail.com
on 24 Apr 2012 at 10:28