如何通过BLE发送浮点数据并将其正确显示在UART(nRF Toolbox)上

时间:2019-04-02 09:04:51

标签: c bluetooth-lowenergy keil nrf52

我正在通过BLE从中央(Android)向nRF52832发送命令,并以错误的格式接收回SPI数据。如何直接转换/显示这些数据。

我希望在向nRF52832发送[1.2 2.2 3.2]时收到'1'。到目前为止,我所得到的只是十六进制数据[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);
        }
}

1 个答案:

答案 0 :(得分:0)

阅读ble_nus_data_send的文档有帮助:

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.

您要做的是将浮点指针转换为uint8,因此,您

  1. 从浮动位表示形式仅传输一个字节
  2. 在某些地方传输可能解释为字符串的原始数据

您可以使用sprintf将float转换为有效字符串。

或者您尝试违反API(很丑陋)并将float原始数据转换为uint8,这意味着一个float可能会导致4个字节。现在希望基础代码不要将某些内容解释为字符串,例如0终止符左右。