简体   繁体   English

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

[英]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. 我正在通过BLE从中央(Android)向nRF52832发送命令,并以错误的格式接收回SPI数据。 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. 当我向nRF52832发送'1'时,我希望收到[1.2 2.2 3.2] All I get till now is hex data [FF?@] . 到目前为止,我所得到的只是十六进制数据[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: 阅读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.

What you do is to cast a float pointer to uint8, thus, you 您要做的是将浮点指针投射到uint8,因此,您

  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. 您可以使用sprintf将float转换为有效字符串。

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. 或者,您尝试违反API(很丑陋)并将您的float原始数据转换为uint8,这意味着一个float可能会导致4个字节。 Now hope that the underlying code does not interpret something as string, for example 0 terminator or so. 现在希望底层代码不会将某些内容解释为字符串,例如0终止符左右。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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