在 Arduino 中连接两个非常量字符数组

问题描述 投票:0回答:2

我有两个非常量字符数组。一个是

buff
,另一个是
buffa
。 我通过其他 Arduino 的射频发射器获取
buffa
中的值,我想将这些数据附加到
buff
内的数据。 然后我将把所有数据发送到其他Arduino。所以我不想发送两个不同的
char arrays
。我想将它们作为一个数组一次性发送。

我尝试过

sprintf()
但不起作用。

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

}
arrays char arduino concatenation rfcomm
2个回答
0
投票

我改变了 for 循环来实现这个目的;

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

      }

这在我的情况下有效,但是,它并不总是连接。 会不会是接收器的问题?


0
投票

你在哪里定义 buflen 的长度?你有

  uint8_t buflen = VW_MAX_MESSAGE_LEN;

但是你还没有定义VW_MAX_MESSAGE_LEN;

尝试如下: int VW_MAX_MESSAGE_LEN = 10;

如果有效,那么您就知道问题所在了。

您可以尝试创建一个名为“string1”的新字符串,而不是使用 sprintf:

String string1(yourCharacterArray);
© www.soinside.com 2019 - 2024. All rights reserved.