简体   繁体   English

从BLE接收到的数据无法从十六进制转换为字符串(北欧)

[英]Data received from BLE unable to convert from hex to string (Nordic)

In the on_ble_evt(ble_evt_t*) function from the ble_app_template, I added a case of 'BLE_GATTS_EVT_WRITE'. 在ble_app_template的on_ble_evt(ble_evt_t *)函数中,我添加了一个情况'BLE_GATTS_EVT_WRITE'。 In it has the following code: 其中包含以下代码:

    case BLE_GATTS_EVT_WRITE:
    { 
        ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
        SEGGER_RTT_printf(0, "Length = %d \n", p_evt_write->len);
        int n;
        char* buf2 = "";    
        char* buf3 = "";    
        count += p_evt_write->len;
        for(n=0; n<p_evt_write->len; n++)
        {
                   SEGGER_RTT_printf(0, "Received[%d] : %X \n", n, p_evt_write->data[n]);
                   SEGGER_RTT_printf(0, "Received[%d] : %d \n", n, p_evt_write->data[n]);
                   if(n>0){                                                     
                       sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));    
                       strcat(buf3, buf2);
                   }                                                    
                   else{                                                
                       sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));   
                       strcpy(buf3, buf2);
                   }
                   SEGGER_RTT_printf(0, "buf2 string: %s \n", buf2);
                   SEGGER_RTT_printf(0, "buf2 hex: %X \n", buf2[0]);
                   SEGGER_RTT_printf(0, "buf3 in string: %s \n", buf3);
                   SEGGER_RTT_printf(0, "count: %d \n", count);
        }
   }

I am receiving hex values from BLE stored in p_evt_write->data[n]. 我从存储在p_evt_write-> data [n]中的BLE接收十六进制值。 I want to concatenate all these received hex values into a string, storing it in 'buf2'. 我想将所有这些接收到的十六进制值连接到一个字符串中,并将其存储在“ buf2”中。

However I am receiving errors at the line of sprintf. 但是我在sprintf的行收到错误。 When I put as "%X" at sprintf, the values are not converted into a string as buf2/buf3 string does not print anything. 当我在sprintf上输入“%X”时,值不会转换为字符串,因为buf2 / buf3字符串不会打印任何内容。 The current code of 当前的代码

sprintf(buf2, "%s", ((char*)p_evt_write->data[n]));

returns an error of 'error: cast to pointer from integer of different size [-Werror=int-to-pointer-cast]'. 返回错误“错误:从不同大小的整数[-Werror = int-to-pointer-cast]转换为指针”。 If I removed the (char*) typecast, it returns an error of 'error: format '%s' expects argument of type 'char *''. 如果删除了(char *)类型转换,它将返回错误“错误:格式'%s'期望类型为'char *”的参数。

I saw that p_evt_write->data[n] where data is declared as 我看到p_evt_write-> data [n]数据声明为

uint8_t                     data[1];

I am currently using the nRF51DK of Nordic with Eclipse IDE and SEGGER RTT JLink for debugging. 我目前正在使用带有Eclipse IDE和SEGGER RTT JLink的Nordic的nRF51DK进行调试。 All printing is printed to SEGGER RTT. 所有打印都将打印到SEGGER RTT。 (printf does not work, using SEGGER_RTT_printf to print instead) (printf无效,改用SEGGER_RTT_printf进行打印)

How can I successfully concatenate all the hex values together to form a string? 如何成功将所有十六进制值连接在一起形成一个字符串? Thank you. 谢谢。

To append to a buffer, you can use the following: 要追加到缓冲区,可以使用以下命令:

size_t total_len = 0;
uint8_t buf = NULL;

while (receive()) {
    size_t len = p_evt_write->len;
    buf = realloc(buf, total_len + len);
    memcpy(buf + total_len, p_evt_write->data, len);
    total_len += len;
}

To convert an array of uint8_t into a NUL-terminated string, allocate enough space, copy the bytes and append a NUL. 要将uint8_t数组转换为NUL终止的字符串,请分配足够的空间,复制字节并附加NUL。

char* s = malloc(total_len + 1);
memcpy(s, buf, total_len);
s[total_len] = 0;

Of course, if any element of buf is a NUL ( 0 ), you will appear to have a truncated string. 当然,如果buf任何元素为NUL( 0 ),则您似乎会出现截断的字符串。 There's no getting around that (without getting rid of the NULs). 没有解决这个问题的方法(没有摆脱NUL)。


Note that ->data doesn't contain "hex values". 注意->data不包含“十六进制值”。 Hex is a text representation of a number. 十六进制是数字的文本表示形式。 You don't have hex; 您没有十六进制; you have numbers. 你有数字。 That's great, because that means no conversion is needed. 很好,因为这意味着不需要转换。 You just need to copy the bytes and add a NUL. 您只需要复制字节并添加一个NUL。

Note that the specific error you were getting is because you were casting a uint8_t (a number) into char* (a pointer). 请注意,您遇到的特定错误是因为您将uint8_t (数字)转换为char* (指针)。 That makes no sense. 这是没有意义的。 You were perhaps going for (char*)(&(p_evt_write->data[n])) , but that wouldn't work either because %s doesn't simply expect a pointer to a character but a pointer to the first character of a sequence ending with a NUL character. 您可能要使用(char*)(&(p_evt_write->data[n])) ,但这也不起作用,因为%s并不只是希望指向一个字符的指针,而是指向它的第一个字符的指针以NUL字符结尾的序列。

Assuming you are wanting to get a string of the hex representation of the data you received, try something like this... 假设您想要获取接收到的数据的十六进制表示形式的字符串,请尝试如下操作...

    ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    char* buf2 = malloc((p_evt_write->len * 2) + 1);
    memset(buf2, 0, (p_evt_write->len * 2) + 1);
    for(int n=0; n<p_evt_write->len; n++)
    {
        char buf3[10];
        sprintf(buf3, "%02X", (unsigned int)p_evt_write->data[n]);
        strcat(buf2, buf3);
    }
    SEGGER_RTT_printf(0, "buf2 string: %s \n", buf2);
    free(buf2);

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

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