簡體   English   中英

如何通過BLE發送浮點數據並將其正確顯示在UART(nRF Toolbox)上

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

我正在通過BLE從中央(Android)向nRF52832發送命令,並以錯誤的格式接收回SPI數據。 我如何直接轉換/顯示這些數據。

當我向nRF52832發送'1'時,我希望收到[1.2 2.2 3.2] 到目前為止,我所得到的只是十六進制數據[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);
        }
}

閱讀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終止符左右。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM