首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在BLE上发送浮点数据并在UART (nRF工具箱)上正确显示

如何在BLE上发送浮点数据并在UART (nRF工具箱)上正确显示
EN

Stack Overflow用户
提问于 2019-04-02 09:04:50
回答 1查看 1.5K关注 0票数 2

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

当我向[1.2 2.2 3.2]发送'1'时,我希望收到nRF52832。到目前为止,我得到的只有十六进制数据[FF?@]

代码语言:javascript
复制
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);
        }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-02 10:36:08

阅读ble_nus_data_send文档有助于:

代码语言:javascript
复制
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将浮点数转换为有效的字符串。

或者您尝试违反API (丑陋)并将浮动原始数据转换为uint8,这意味着一个浮点数可能会产生4个字节。现在希望底层代码不会将某些内容解释为字符串,例如0终止符。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55470835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档