C ++字符串数组在20字节后拆分

时间:2019-06-07 15:03:05

标签: c++ bluetooth-lowenergy arduino-ide notify mtu

我有这个代码示例,该示例用于制作20字节大小的数据包,这些数据包最终将通过BLE发送。我想更改代码,以便可以输入数据数组(加速度计读数)。

我的数据:

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中做到了,但我想不出如何按顺序连接数据包并将它们附加到.csv文件中的想法。(每列=不同数据)。

谢谢!

1 个答案:

答案 0 :(得分:0)

String类提供了一个接受const char *输入的构造函数,因此您可以使用它来(隐式或显式)将名为data的变量转换为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_s更改sprintf,更安全

string reference