这是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(命令)'将*不*给你的字符串的长度。您需要使用['length'](http://arduino.cc/en/Reference/StringLength)成员函数。 – 2014-11-06 13:31:52
这就是问题所在!谢谢。你能否写一个答案,我会接受。 (我觉得自己像个白痴)。 – Chirantan 2014-11-06 13:36:28