Arduino 数据类型混淆。有字符串,需要const char?

Arduino data type confusion. Have string, need const char?

我正在使用 UNO,我正在使用 maniacbug 的 RF24 库和软 spi,以及他的 rf24network 库。 https://github.com/maniacbug/RF24Network

我想做的只是发送数据,但我遇到了错误的数据类型。

{
last_sent = now;

printf("Sending...\r\n");
const char* hello = "Hello, world!";
RF24NetworkHeader header(/*to node*/ other_node);
bool ok = network.write(header,hello,strlen(hello));
if (ok)
  printf("\tok.\r\n");
else
{
  printf("\tfailed.\r\n");
  delay(250); // extra delay on fail to keep light on longer
}
}

如果我把它改成

bool ok = network.write(header,myString,strlen(myString));

我收到数据类型错误。那么如何让我的字符串适合这种结构。我的想法是 myString.toCharArray 然后循环遍历它?

首先将字符串转换为字符数组:

size_t len = myString.length();
char buf[len+1]; // +1 for the trailing zero terminator
myString.toCharArray(buf, len);

然后你可以通过buf:

bool ok = network.write(header,buf,strlen(buf));