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

How can I compare __FlashStringHelper* with char* on Arduino?

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

__FlashStringHelper 只是一种特殊数据类型,用于确定 Flash 字符串的正确重载 function/method。

无论如何你不能使用 strcmp 因为它用于比较 RAM 中的两个字符串,但是在包含 <avr/pgmspace.h> 中有它的变体 strcmp_P 用于比较 const char *放在 RAM 中 const char * 放在 FLASH 存储器中(按此顺序)。

因此您可以使用:

strcmp_P(thestring, (const char*)F("knownstring"));
// or better:
strcmp_P(thestring, PSTR("knownstring"));

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