C++ 字符串数组在 20 字节后拆分
C++ string array split after 20 Bytes
我有这个代码示例,用于制作最终将通过 BLE 发送的 20 字节大小的数据包。我想更改代码,以便输入数据数组(加速度计读数)。
我的数据:
float ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll;
char myConcatenation[80];
sprintf(myConcatenation, "%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f", ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll);
函数定义:
std::vector<std::string> buildPackets(std::string data, size_t packetSize) {
// split up the packet
size_t partialPacketSize = packetSize - 4;
// calculate how many packets
size_t packetCount = data.length() / partialPacketSize;
packetCount = data.length() % partialPacketSize == 0 ? packetCount : packetCount + 1;
std::vector<std::string> packets;
// construct each packet
for (int i = 0; i < packetCount; i++) {
// start of packet
std::string packet = "##";
packet += (char)packetCount;
packet += (char)(i + 1);
std::string part = i == packetCount - 1 ? data.substr(i * partialPacketSize) : data.substr(i * partialPacketSize, partialPacketSize);
packet.append(part);
// add to vector of packets
packets.push_back(packet);
}
函数调用:
变量 data
这里是字符串,我只需要把我的 myConcatenation[]
放在那里
我应该对代码进行哪些更改才能使其正常工作?
std::string data = "averylongstringofdatathatislargerthantwentycharacters";
size_t chars = data.length();
size_t packetSize = 20;
// assume I have this characteristic already setup
if (chars > packetSize) {
auto packets = buildPackets(data, packetSize);
for (auto packet : packets) {
txCharacteristic->setValue(packet);
txCharacteristic->notify();
}
}
else {
txCharacteristic->setValue(data);
txCharacteristic->notify();
}
感谢您的帮助。
编辑:
this is what I have now:
std::vector<std::string> buildPackets(std::string data, size_t packetSize) {
// split up the packet
size_t partialPacketSize = packetSize - 4;
// calculate how many packets
size_t packetCount = data.length() / partialPacketSize;
packetCount = data.length() % partialPacketSize == 0 ? packetCount : packetCount + 1;
std::vector<std::string> packets;
// construct each packet
for (int i = 0; i < packetCount; i++) {
// start of packet
std::string packet = "@";
packet += (char)packetCount;
packet += (char)(i + 1);
std::string part = i == packetCount - 1 ? data.substr(i * partialPacketSize) : data.substr(i * partialPacketSize, partialPacketSize);
packet.append(part);
// add to vector of packets
packets.push_back(packet);
}
return packets;
}
--------
**call function**
sprintf(myConcatenation, "#%3.2f,%3.2f,%3.2f,#%3.2f,%3.2f,%3.2f,#%3.2f,%3.2f,%3.2f,#%3.2f,%3.2f,%3.2f", ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll);
size_t packetSize = 20;
auto packets = buildPackets(myConcatenation, packetSize);
for (auto packet : packets) {
pCharacteristic->setValue(packet);
pCharacteristic->notify();
}
Serial.println(myConcatenation);
可以问一下收货方吗?我的字符串在每个不同的传感器读数(#ax、ay、az、#gx、gy ...)的开头都有 #
,在每个数据包的开头都有 @
。你知道接收方应该如何实现吗(我在 MITAppInventor 中这样做,但我想不出如何按顺序连接 packetst 并将它们附加到 .csv 文件中。(每列 = 不同的数据)。
谢谢!
String class 提供了一个接受输入 const char* 的构造函数,因此您可以使用它来将您的变量命名数据(隐式或显式)转换为 std::string.
所以最后,把它传递给函数
float ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll;
char myConcatenation[80];
sprintf(myConcatenation, "%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f", ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll);
auto packets = buildPackets(myConcatenation, packetSize);
就这些了。
快速补充建议:
- 把sprintf换成sprintf_s,更安全
我有这个代码示例,用于制作最终将通过 BLE 发送的 20 字节大小的数据包。我想更改代码,以便输入数据数组(加速度计读数)。
我的数据:
float ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll;
char myConcatenation[80];
sprintf(myConcatenation, "%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f", ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll);
函数定义:
std::vector<std::string> buildPackets(std::string data, size_t packetSize) {
// split up the packet
size_t partialPacketSize = packetSize - 4;
// calculate how many packets
size_t packetCount = data.length() / partialPacketSize;
packetCount = data.length() % partialPacketSize == 0 ? packetCount : packetCount + 1;
std::vector<std::string> packets;
// construct each packet
for (int i = 0; i < packetCount; i++) {
// start of packet
std::string packet = "##";
packet += (char)packetCount;
packet += (char)(i + 1);
std::string part = i == packetCount - 1 ? data.substr(i * partialPacketSize) : data.substr(i * partialPacketSize, partialPacketSize);
packet.append(part);
// add to vector of packets
packets.push_back(packet);
}
函数调用:
变量 data
这里是字符串,我只需要把我的 myConcatenation[]
我应该对代码进行哪些更改才能使其正常工作?
std::string data = "averylongstringofdatathatislargerthantwentycharacters";
size_t chars = data.length();
size_t packetSize = 20;
// assume I have this characteristic already setup
if (chars > packetSize) {
auto packets = buildPackets(data, packetSize);
for (auto packet : packets) {
txCharacteristic->setValue(packet);
txCharacteristic->notify();
}
}
else {
txCharacteristic->setValue(data);
txCharacteristic->notify();
}
感谢您的帮助。
编辑:
this is what I have now:
std::vector<std::string> buildPackets(std::string data, size_t packetSize) {
// split up the packet
size_t partialPacketSize = packetSize - 4;
// calculate how many packets
size_t packetCount = data.length() / partialPacketSize;
packetCount = data.length() % partialPacketSize == 0 ? packetCount : packetCount + 1;
std::vector<std::string> packets;
// construct each packet
for (int i = 0; i < packetCount; i++) {
// start of packet
std::string packet = "@";
packet += (char)packetCount;
packet += (char)(i + 1);
std::string part = i == packetCount - 1 ? data.substr(i * partialPacketSize) : data.substr(i * partialPacketSize, partialPacketSize);
packet.append(part);
// add to vector of packets
packets.push_back(packet);
}
return packets;
}
--------
**call function**
sprintf(myConcatenation, "#%3.2f,%3.2f,%3.2f,#%3.2f,%3.2f,%3.2f,#%3.2f,%3.2f,%3.2f,#%3.2f,%3.2f,%3.2f", ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll);
size_t packetSize = 20;
auto packets = buildPackets(myConcatenation, packetSize);
for (auto packet : packets) {
pCharacteristic->setValue(packet);
pCharacteristic->notify();
}
Serial.println(myConcatenation);
可以问一下收货方吗?我的字符串在每个不同的传感器读数(#ax、ay、az、#gx、gy ...)的开头都有 #
,在每个数据包的开头都有 @
。你知道接收方应该如何实现吗(我在 MITAppInventor 中这样做,但我想不出如何按顺序连接 packetst 并将它们附加到 .csv 文件中。(每列 = 不同的数据)。
谢谢!
String class 提供了一个接受输入 const char* 的构造函数,因此您可以使用它来将您的变量命名数据(隐式或显式)转换为 std::string.
所以最后,把它传递给函数
float ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll;
char myConcatenation[80];
sprintf(myConcatenation, "%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f,%3.2f", ax, ay, az, gx, gy, gz, mx, my, mz, yaw, pitch, roll);
auto packets = buildPackets(myConcatenation, packetSize);
就这些了。
快速补充建议:
- 把sprintf换成sprintf_s,更安全