Sending data array over ble nus

Hi, 

I'm trying to send an data array over ble nus. 

static void app_timer_handler(void * p_context)
{
  if(m_conn_handle != BLE_CONN_HANDLE_INVALID)
  {
    uint8_t data_array[4];

    float val0;
    float val1;
    float val2;
    float val3;

    uint8_t data0[20];
    uint8_t data1[20];
    uint8_t data2[20];
    uint8_t data3[20];

    ret_code_t err_code;

    err_code = nrfx_saadc_sample_convert(0, &m_sample);
    APP_ERROR_CHECK(err_code);

    val0 = m_sample * 3.3 / 4096;
    sprintf((char*)data0, "%.2f", val0);
    data_array[0] = data0; 

    err_code = nrfx_saadc_sample_convert(1, &m_sample);
    APP_ERROR_CHECK(err_code);

    val1 = m_sample * 3.3 / 4096;
    sprintf((char*)data1, "%.2f", val1);
    data_array[1] = data1; 

    err_code = nrfx_saadc_sample_convert(2, &m_sample);
    APP_ERROR_CHECK(err_code);

    val2 = m_sample * 3.3 / 4096;
    sprintf((char*)data2, "%.2f", val2);
    data_array[2] = data2;

    err_code = nrfx_saadc_sample_convert(3, &m_sample);
    APP_ERROR_CHECK(err_code);

    val3 = m_sample * 3.3 / 4096;
    sprintf((char*)data3, "%.2f", val3);
    data_array[3] = data3;
  
    uint16_t d_len = sizeof(data_array)-1;
    
    err_code = ble_nus_data_send(&m_nus, data_array, &d_len, m_conn_handle); 
    APP_ERROR_CHECK(err_code);
  }
}

This didn't work. 

I'm also getting this warning.

I'm doing something wrong, but I don't know what. Do I also have to change the receiving part of the central device to receive the data array?

Parents
  • Hello,

    I think you need to look at the pointer - array side of this application.

    Try to print the content of the data_array[] array in your log, or look at it in a debug session.

    What do you expect the data_array to look like? What should be in data_array[0], data_array[1], data_array[2] and data_array[3]? Hint: It only holds a single uint8_t value in each of the elements, since it is a uint8_t array.

    Then you try to set each of these uin8_t parameters equal to the address (pointer) of each of the 20 byte arrays, so what the data_array[] values actually are after e.g.

    data_array[3] = data3;

    I am not sure. data_array[3} has 8 bits, and the data3 array pointer has 32 bits. So I guess it will be either the first 8 or last 8 bits. It is not really specified in C and hence, you get the warnings that you see.

    So how to actually send the data you want to send:

    I suggest you either create one large array, data[80], and then you sprintf to the 0th, 20th, 40th and 60th element, and then you just pass the entire array through ble_nus_data_send(). Remember to set the length to 80, and not 4. 

    Best regards,

    Edvin

  • Hi,

    Thank you I will try this.

    Is it possible to send an array of size data[80] over ble_nus without any change?

Reply Children
No Data
Related