-1

In order to send a long payload I've created the following function:

void splitMSG(const char *msg, const int arraySize, const int len)
{
  Serial.print("Array size: ");
  Serial.println(arraySize);
  byte numPackets = (int)(arraySize / len);
  byte P_iterator = 0;
  if (arraySize % len > 0)
  {
    numPackets++;
  }
  Serial.print("num_Packets: ");
  Serial.println(numPackets);

  while (P_iterator < numPackets)
  {
    char t[len + 1];
    char *ptr1 = msg + P_iterator * (len);
    strncpy(t, ptr1, len);
    t[len + 1] = '\0';
    Serial.println(t);
    radio.write(&t, sizeof(t));
    P_iterator++;
  }
}

The split message is sent to the serial monitor to verify it is as expected.

**On the sending device :

void loop()
{
  radio.stopListening();

  char send_msg[] = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ";  // <----- input msg
  splitMSG(send_msg, sizeof(send_msg), 10);                  // <----- split to 10 bytes a packet
  delay(1000);
  // Serial.println("±±±±±±±±");
}

On the serial monitor I get:

08:06:17.569 -> Array size: 37
08:06:17.569 -> num_Packets: 4
08:06:17.569 -> 1234567890
08:06:17.569 -> ABCDEFGHIJ
08:06:17.569 -> KLMNOPQRST
08:06:17.569 -> UVWXYZ

**On the receiving device:

void loop()
{
  if (radio.available())
  {
    while (radio.available())
    {
      char buf[32];
      radio.read(&buf, sizeof(buf));
      Serial.print("got msg: ");
      Serial.println(buf);
      radio.flush_rx();
    }
  }

  delay(100);
}

On the serial monitor I get:

08:08:18.462 -> got msg: 1234567890⸮⸮⸮⸮
08:08:18.561 -> got msg: ABCDEFGHIJKLMNOPQRSTUVWXYZ
08:08:19.555 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:20.549 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:21.576 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:22.570 -> got msg: 1234567890ABCDEFGHIJKLMNOPQRST⸮⸮⸮⸮
08:08:22.669 -> got msg: UVWXYZ

Does the problem with radio.write ' buffer , or the way I split the message ?

1 Answer 1

2
t[len + 1] = '\0';

Your array is declared as having len+1 bytes of storage. So the last index is len, not len+1. You're writing the zero one past the end of the array, thus you're not transmitting it (and sending one byte of "garbage" instead).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.