【发布时间】:2014-11-06 13:29:40
【问题描述】:
这是 C++ 新手问题。我有一个带有以下代码的 Arduino 草图来传输蓝牙低功耗 UART 通知。
当command 是 5 个字符时,我在蓝牙接收器端得到 5 个字符。但是,当我使用单个字符命令跟随 5 个字符 command 时,收到的是单个字符,后跟前一个 command 的最后 3 个字符。
例子,
command 是 -1,0t。我收到的是-1,0t。但下一个command 只是r。我收到的是r,0t。
这里发生了什么?我怎样才能得到“r”?
int BLEsend(String command){
byte length = sizeof(command);
unsigned char* notification = (unsigned char*) command.c_str(); // cast from string to unsigned char*
Serial.print(length);
BLE.sendData(UART_SEND, notification, length);
Serial.print("Notification Sent: "); Serial.println(command);
delay(100);
return 1;
}
【问题讨论】:
-
请注意,
sizeof(command)将不会为您提供字符串的长度。您需要使用length成员函数。 -
这就是问题所在!谢谢。你能写一个答案吗,我会接受的。 (我觉得自己像个白痴)。
标签: c++ casting arduino unsigned-char