This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

STRCAT not working when sending data from nrf52840 and no response in the apps.

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint8_t data_array[BLE_NUS_MAX_DATA_LEN];
//  static uint8_t data_array[]={"AABBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDEE"};
    static uint8_t index = 0;
    uint32_t       err_code;
    uint8_t string[]= "AABBCCDD";
   uint16_t header="A";
  uint8_t a[]="A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDEE";
   uint8_t test[]="XXX123";
   uint16_t length = (uint16_t)index;
   uint16_t length1 = sizeof(a);

    switch (p_event->evt_type)
    {
        case APP_UART_DATA_READY:
            UNUSED_VARIABLE(app_uart_get(&data_array[index]));
            index++;

            if ((data_array[index - 1] == '\n') ||
                (data_array[index - 1] == '\r') ||
                (index >= m_ble_nus_max_data_len))
            {
                if (index > 1)
                {

                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    do
                    {
                     
                        err_code = ble_nus_data_send(&m_nus, data_array, &length, m_conn_handle);
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);
      

                      }else{


                    

                    NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                    NRF_LOG_HEXDUMP_DEBUG(data_array, index);

                    do
                    {
                       

                       	for( uint8_t no=1;no<=20;no++){
		
                //fail to concate
                    strcat(a, test);
                     uint16_t length2 = sizeof(a);


                                err_code = ble_nus_data_send(&m_nus,a, &length2, m_conn_handle);
							
						}

                       
                        if ((err_code != NRF_ERROR_INVALID_STATE) &&
                            (err_code != NRF_ERROR_RESOURCES) &&
                            (err_code != NRF_ERROR_NOT_FOUND))
                        {
                            APP_ERROR_CHECK(err_code);
                        }
                    } while (err_code == NRF_ERROR_RESOURCES);



                             }

              

                index = 0;
            }
            break;

        case APP_UART_COMMUNICATION_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}

Why when I want to concatenate this string with other string it fails to send the combined string ?? So how do concatenate the string ??

Parents
  • This is because a[] is not large enough to contain the concatenated resulting string, see the documentation. It should be large enough to store the initial string in addition to the string to be appended.

    I tried to experiment and figured out that the following:

    uint8_t a[]="A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDEE";
    uint8_t test[]="XXX123";
    strcat(a, test);
    NRF_LOG_INFO("a[]: %s",a);

    Gave the following output:

    <info> app: a[]: A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDEEXXX123

    This is because the strcat operation will write outside a[]. However, you should not rely on this and you should set the size of a[] when you define it. Read why on this thread.

    Best regards,

    Simon

    Best regards,

    Simon

Reply
  • This is because a[] is not large enough to contain the concatenated resulting string, see the documentation. It should be large enough to store the initial string in addition to the string to be appended.

    I tried to experiment and figured out that the following:

    uint8_t a[]="A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDEE";
    uint8_t test[]="XXX123";
    strcat(a, test);
    NRF_LOG_INFO("a[]: %s",a);

    Gave the following output:

    <info> app: a[]: A1BBCCDDEEAABBCCDDEEAABBCCDDEEAABBCCDDEEXXX123

    This is because the strcat operation will write outside a[]. However, you should not rely on this and you should set the size of a[] when you define it. Read why on this thread.

    Best regards,

    Simon

    Best regards,

    Simon

Children
Related