简体   繁体   中英

C++ string array split after 20 Bytes

I have this code example that is used for making a 20 Byte size packets of data that will eventually will be sent via BLE. I want to change the code so I can input array of my data (accelerometer readings).

My data:

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);

The function definition:

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);
  }

Function call: variable data here is string, all I need is put there my myConcatenation[]

What changes should I make in code for having it working?

   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();
}

Thank you for any help.


EDIT:

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);

Can I ask you about the receiving side? my string has # at the beginning of every different sensor reading (#ax,ay,az,#gx,gy...) and @ on the start of every packet. Do you know how should be the receiving side implemented (I do that in MITAppInventor but I can't think of an idea how to connect the packetst in order and append them into .csv file. (each column = different data).

Thank you!

String class provides a constructor that takes in input a const char*, so you can use it for converting (implicitly or explicitly) your variable named data into a std::string.

So at the end, just pass it to the function

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);

That's all.

A quick additional suggestion:

  • change sprintf with sprintf_s, it's safer

string reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM