質問するログイン新規登録

回答編集履歴

1

追記

2021/03/30 08:43

投稿

thkana
thkana

スコア7769

answer CHANGED
@@ -18,4 +18,37 @@
18
18
  では、ちゃんと200msでタイムアウトしているようです。
19
19
  症状の出るミニマムコードを提示できますか?
20
20
 
21
- ライブラリは1.0.6です (あぁ、[issue](https://teratail.com/questions/326026)まだやってなかった)
21
+ ライブラリは1.0.6です (あぁ、[issue](https://teratail.com/questions/326026)まだやってなかった)
22
+
23
+ ---
24
+
25
+ あ、もしかして...
26
+ 上記はSerial2は無入力で試したのですが、
27
+ HardwareSerialはStreamを継承していて、
28
+ ```Arduino
29
+ String Stream::readStringUntil(char terminator)
30
+ {
31
+ String ret;
32
+ int c = timedRead();
33
+ while(c >= 0 && c != terminator) {
34
+ ret += (char) c;
35
+ c = timedRead();
36
+ }
37
+ return ret;
38
+ }
39
+ ```
40
+ ```Arduino
41
+ int Stream::timedRead()
42
+ {
43
+ int c;
44
+ _startMillis = millis();
45
+ do {
46
+ c = read();
47
+ if(c >= 0) {
48
+ return c;
49
+ }
50
+ } while(millis() - _startMillis < _timeout);
51
+ return -1; // -1 indicates timeout
52
+ }
53
+ ```
54
+ だから、データが流れ込んでいる限りreadStringUntil()はタイムアウトしないのでは?