na7abay
na7abay

Reputation: 19

Concatenate two non constant char arrays in Arduino

I have two non constant char arrays. One is buff and other is buffa. I get values in buffa via rf transmitter of other Arduino and I want to append those data to the data inside of buff. Then I will send all data to other Arduino. So I don't want to send two different char arrays. I want to send them all at once as just one array.

I tried sprintf() but it does not work.

char buffa[144]; 
char buff[1000];

void loop() {

    uint8_t buf[VW_MAX_MESSAGE_LEN];
    uint8_t buflen = VW_MAX_MESSAGE_LEN;

    sprintf(buff,"<status>\n");    

    if (vw_get_message(buf, &buflen)) { // check to see if anything has been received
        int i;
        for (i = 0; i < buflen; i++) {
            buffa[i] = (char) buf[i];  // the received data is stored in buffer

        }
    }


    distance1 = getDistance(initPin1, echoPin1);
    sendData(3, distance1);

    sprintf(buff, "%s", buffa);

    delay(5000);
    const char *msg0 = buff;

    vw_send((uint8_t *)msg0, strlen(msg0)); // Send control character 
    vw_wait_tx();

    Serial.print(msg0);

}

Upvotes: 0

Views: 8331

Answers (2)

Alan Scarpa
Alan Scarpa

Reputation: 3560

Where are you defining the length of buflen? You have

  uint8_t buflen = VW_MAX_MESSAGE_LEN;

But you haven't defined VW_MAX_MESSAGE_LEN;

Try something like: int VW_MAX_MESSAGE_LEN = 10;

If that works, then you know that's the problem.

And instead of using sprintf, you could try this which creates a new string called "string1":

String string1(yourCharacterArray);

Upvotes: 0

na7abay
na7abay

Reputation: 19

I changed the for loop to achieve this like this;

  for (i = 0, j=9; i < buflen; i++, j++) {
        buff[j] = (char) buf[i];  // the received data is stored in buffer

      }

This works in my case, However, It does not always concatenates. Could it be the problem of receiver?

Upvotes: 0

Related Questions