简体   繁体   English

用C ++读取原始字节数据

[英]Reading raw byte data in C++

I have a really basic question. 我有一个非常基本的问题。 I want to turn an LED on/off depending on the BLE data received by a Nrf52 BLE device. 我想根据Nrf52 BLE设备接收的BLE数据打开/关闭LED。 My problem is that the data (Received_Data) is in raw bytes data form (1 byte), and I don't know how to perform an if statement on that, or convert it into a form that can. 我的问题是数据(Received_Data)是原始字节数据格式(1字节),我不知道如何在其上执行if语句,或将其转换为可以的形式。 In the code below I have: 在下面的代码我有:

                if (Received_Data > 50)
                    {
                      nrf_gpio_pin_toggle(LED_2);
                    }
                 end

How can I let 'Received_Data' be used in an IF statement like this, so it can be read as an integer or a hex number? 如何让'Received_Data'在这样的IF语句中使用,所以它可以读成整数或十六进制数?

        case APP_UART_DATA_READY:
        UNUSED_VARIABLE(app_uart_get(&data_array[index]));
        index++;

        if ((data_array[index - 1] == '\n') ||
            (data_array[index - 1] == '\r') ||
            (index >= m_ble_nus_max_data_len))
        {
            if (index > 1)
            {
                NRF_LOG_DEBUG("Ready to send data over BLE NUS");
                NRF_LOG_HEXDUMP_DEBUG(Received_Data, index);

                 if (Received_Data > 50)
                    {
                      nrf_gpio_pin_toggle(LED_2);
                    }
                 end

This is doing my head in. I'm sure this can be answered by someone in 5 seconds. 这是我的头脑。我相信有人可以在5秒钟内回答这个问题。 and I am beyond the point where I can invest the time to dig through all the associated C++ documentation to find the solution. 我无法花时间挖掘所有相关的C ++文档来找到解决方案。

Since Received_Data is an uint8_t array, you can access individual bytes directly: 由于Received_Data是一个uint8_t数组,因此您可以直接访问各个字节:

if (Received_Data[0] > 50)
//or
if (Received_Data[index] > 50)

uint8_t is [0..255]. uint8_t是[0..255]。

From your question 从你的问题

How can I let 'Received_Data' be used in an IF statement like this, so it can be read as an integer or a hex number? 如何让'Received_Data'在这样的IF语句中使用,所以它可以读成整数或十六进制数?

From your comments 从你的评论

It is already defined as a uint8: uint8_t Received_Data[BLE_NUS_MAX_DATA_LEN]; 它已被定义为uint8:uint8_t Received_Data [BLE_NUS_MAX_DATA_LEN];

Just want to check that the byte within the array is above or below a certain threshold, such as 50. What would be the syntax to do that with an IF statement? 只是想检查数组中的字节是高于还是低于某个阈值,例如50.使用IF语句的语法是什么?

Received_Data is an array of unsigned 8-bit integers. Received_Data是无符号8位整数数组。 In the first piece of code you provided: 在您提供的第一段代码中:

if (Received_Data > 50){
    nrf_gpio_pin_toggle(LED_2);
}

Received_Data decays to a pointer to the first element of the array. Received_Data衰减到指向数组第一个元素的指针。 Therefore, you are actually comparing between a pointer and an integer (which is expressly forbidden by ISO C++). 因此,您实际上是在比较指针和整数(ISO C ++明确禁止)。 If you want to check the value of a specific element of that array, then you need to index it with the subscript operator like so: 如果要检查该数组的特定元素的值,则需要使用下标运算符对其进行索引,如下所示:

//byte_of_interest is some non-negative integer value that specifically represents
//the element in the array that you are interested in comparing with 50
if (Received_Data[byte_of_interest] > 50){
    nrf_gpio_pin_toggle(LED_2);
}

Likewise, you can also use pointer arithmetic: 同样,您也可以使用指针算法:

//byte_of_interest is an offset from the beginning of the array
//so its contents are located at the address to the beginning of the array + the offset
if (*(Received_Data + byte_of_interest) > 50){
    nrf_gpio_pin_toggle(LED_2);
}

Furthermore, I recommend that you initialize the array to 0 to prevent getting false positives before the array is filled in (eg uint8_t Received_Data[BLE_NUS_MAX_DATA_LEN] = {0}; ) 此外,我建议您将数组初始化为0以防止在填充数组之前得到误报(例如uint8_t Received_Data[BLE_NUS_MAX_DATA_LEN] = {0};

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

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