首先,設置:Arduino的IDE 1.5.7測試版,納米V3.0的Arduino:Serial.find(炭)不工作
總之,我的目標是:用Serial.find()等爲標準EOL在繼續下面的代碼之前,在串行緩衝區中找到字符(ASCII 13,CR和ASCII 10,NL)。
我的(問題/縮短)代碼:
char charCr = 13;
char charNl = 10;
void loop(){
do_stuff;
foo();
do_other_stuff;}
void foo()
{
while (true)
{
if (Serial.find(bar1) && Serial.find(bar2))
{
break; // EOL characters found
}
delay(1); // wait to receive EOL
};
}
好,所以兩次與在發生什麼bar1
和bar2
如果bars
分別爲charCr
和charNl
然後代碼不開的問題編譯時抱怨:
error: call of overloaded 'find(char&)' is ambiguous
note: candidates are:
In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/HardwareSerial.h:29:0,
from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:221
找到一個近似匹配,其中,我從Stream
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:59:8: note: bool Stream::find(char*) <near match>
bool find(char *target); // reads data from the stream until the target string is found
相信的是找到正確的定義,爲Serial
inhereits它但隨後也抱怨焦炭輸入應該是一個指針(字符*):
C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Stream.h:59:8: note: no known conversion for argument 1 from 'char' to 'char*'
文檔我讀過在Serial.find()和Stream.find()表明char應該是而不是是一個指針,只是爲了傳遞char值。無論如何,如果bar1
和bar2
被引用爲&charCr
和&charNl
代碼編譯得很好,但條件從未滿足,並且我知道我發送了兩個EOL字符,如通過不同方法和調試代碼所證實的。
那麼......我的代碼出了什麼問題?
從評論:即「直到目標串中發現從流中讀取數據」。不是字符串,這就是爲什麼它要求指針。 – 2014-09-21 23:30:40
是的,但它也說輸入數據類型「目標:搜索字符串(char)」...所以什麼纔是正確的語法?爲什麼當我使用參考操作符&它沒有找到它? – brneuro 2014-09-21 23:32:57
嘗試一下 - 搜索以空字符結尾的CR/LF字符串,看看是否有效。如果有疑問,懷疑文檔,(特別是如果我寫了:)。 – 2014-09-21 23:34:30