简体   繁体   中英

How to send float data over BLE and display it properly on the UART (nRF Toolbox)

I'm sending a command over BLE from the central (Android) to the nRF52832, and receive SPI data back, but in wrong format. How can I convert/display this data as it is.

I expect to receive [1.2 2.2 3.2] when I send '1' to the nRF52832. All I get till now is hex data [FF?@] .

if (p_evt->params.rx_data.p_data[0] == '1') // If the Rx_Central input is '1', ble_nus_data_send the accelerometer data.
{ 
    spim_rx_buffer.AccX = 1.2; // Random dummy data
    spim_rx_buffer.AccY = 2.2; // Random dummy data
    spim_rx_buffer.AccZ = 3.2; // Random dummy data

      for(int i = 0; i < 3; i++)
        {
            uint16_t len = sizeof(float);
            float* spi_p = (float*) &spim_rx_buffer;
            err_code = ble_nus_data_send (&m_nus, (uint8_t*)spi_p+i*sizeof(float), &len, m_conn_handle);
        }
}

Reading the documentation of ble_nus_data_send helps:

Function for sending a data to the peer.
This function sends the input string as an RX characteristic notification to the peer.

Parameters
[in]    p_nus   Pointer to the Nordic UART Service structure.
[in]    p_data  String to be sent.
[in,out]    p_length    Pointer Length of the string. Amount of sent bytes.
[in]    conn_handle Connection Handle of the destination client.

What you do is to cast a float pointer to uint8, thus, you

  1. transfer only one byte from the float bit representation
  2. transfer raw data which might be interpreted as string somewhere

You could use sprintf to convert your float to a valid string.

Or you try to violate the API (ugly) and convert your float raw data to uint8, meaning that one float results in probably 4 bytes. Now hope that the underlying code does not interpret something as string, for example 0 terminator or so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM