繁体   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