如何在 Arduino 上比较 __FlashStringHelper* 和 char*?

DHe*_*hot 4 c++ arduino string-comparison

我有一个将文本行输出到串行的板。我需要将这些文本行与我所知道的文本进行比较。本质上,我想做strcmp(thestring,F("knownstring")),但是似乎没有一个采用 FlashStringHelper* 类型的 strcmp 版本。有 strcmp_P 使用const PROGMEM char *,但这似乎完全是不同的事情。我在 Arduino 论坛帖子上看到有人建议通过使用 遍历 flash-string 来编写一个函数progmem_read_byte (b, i),但该函数实际上并不存在,并且最接近的等效项 ( pgm_read_byte(b+i)) 似乎不适用于 FlashStringHelper* - 我收到类似error: invalid use of incomplete type 'class __FlashStringHelper'和 的错误error: forward declaration of 'class __FlashStringHelper',这意味着我做了严重错误的事情!我几乎要放弃并将字符串放入 RAM 中,但 arduino 没有太多,所以我想如果可能的话避免这种情况。有人可以帮忙吗?

KII*_*IIV 6

__FlashStringHelper只是用于确定 Flash 字符串的正确重载函数/方法的特殊数据类型。

无论如何,您不能使用strcmp它来比较 RAM 中的两个字符串,但在包含文件中,<avr/pgmspace.h>有一个变体strcmp_P用于比较const char *放置在 RAM 中与const char *放置在闪存中(按此顺序)。

所以你可以使用:

strcmp_P(thestring, (const char*)F("knownstring"));
// or better:
strcmp_P(thestring, PSTR("knownstring"));
Run Code Online (Sandbox Code Playgroud)

F宏基本上是: 所以在第一种情况(__FlashStringHelper *)PSTR("...")下将其转换回 有点多余。const char*