简体   繁体   English

Arduino:Serial.find(char)无法正常工作

[英]Arduino: Serial.find(char) not working

Firstly, setup: Arduino IDE 1.5.7 beta, Nano v3.0 首先,设置:Arduino IDE 1.5.7 beta,Nano v3.0

In short, my goal: use Serial.find() to wait for both standard EOL characters (both ASCII 13, CR, and ASCII 10, NL) to be found in the serial buffer, before proceeding with the following code. 简而言之,我的目标是:在继续以下代码之前,使用Serial.find()等待在串行缓冲区中找到两个标准EOL字符(ASCII 13,CR和ASCII 10,NL)。

my (problematic/shortened) code: 我的(问题/缩短)代码:

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
      }; 
}

OK, so two separate problems with what goes in bar1 and bar2 好的,所以bar1bar2出现了两个单独的问题

If the bars are respectively charCr and charNl then the code doesn't compile whilst complaining: 如果bars分别为charCrcharNl则代码在抱怨时不会编译:

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

finds a near match, which, I believe is the correct definition of find, as Serial inhereits it from Stream 找到一个近距离匹配,我相信这是对find的正确定义,因为SerialStream继承了它

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

But then also complains the char input should be a pointer (char*): 但随后还抱怨char输入应该是一个指针(char *):

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*'

The documentation I've read on Serial.find() and Stream.find() suggests that char should not be a pointer, just to pass the char value. 我对Serial.find()和Stream.find()读取的文件表明,焦炭应该是一个指针,只是为了打发char值。 Regardless, if bar1 and bar2 are referenced as &charCr and &charNl the code compiles fine, but the conditional is never met, and I know that I am sending both EOL characters, as confirmed by different means and debugging code. 无论如何,如果将bar1bar2分别引用为&charCr&charNl则代码可以正常编译,但条件始终未得到满足,并且我知道我同时发送了两个EOL字符,这已通过不同的方式和调试代码进行了确认。

So... what's going wrong with my code? 所以...我的代码出了什么问题?

The documentation on the website is misleading because they say string but the function prototype is (char). 网站上的文档具有误导性,因为他们说的是字符串,但函数原型为(char)。 A string is a variable length character array. 字符串是可变长度的字符数组。 A char is a single character. 字符是单个字符。 When in doubt, always believe the function declaration in the header file (.H). 如有疑问,请始终相信头文件(.H)中的函数声明。 From Stream.h: 从Stream.h:

bool find(char *target);   // reads data from the stream until the target string is found
// returns true if target string is found, false if timed out (see setTimeout)

bool find(char *target, size_t length);   // reads data from the stream until the target string of given length is found
// returns true if target string is found, false if timed out

With those in mind, there are two ways forward. 考虑到这些,有两种方法。 Search single characters: 搜索单个字符:

// method as you started - accepts terminators in either order
char charCr = 13;
char charNl = 10;

if (Serial.find(&charCr, 1) && Serial.find(&charNl, 1))

or string form: 或字符串形式:

char termseq1[] = {13, 10, 0};
char termseq2[] = {10, 13, 0};

if (Serial.find(termseq1) || Serial.find(termseq2))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM