简体   繁体   English

如何将uint8_t从uart_read_bytes转换为char或可以添加到cJSON对象的内容?

[英]How to convert uint8_t from uart_read_bytes to a char or something that can be added to cJSON object?

Here I am reading from a UART that is communicating with another device that is sending it bytes. 在这里,我从与另一个正在发送字节的设备进行通信的UART中读取数据。 If I am getting an integer value in 3 bytes, how best to convert it to something I can send using cJSON. 如果我得到3字节的整数值,如何最好地将其转换为可以使用cJSON发送的值。 I can get the value and iterate over it, but once I put it in a char array, i cannot even see the value any longer. 我可以获取该值并对其进行迭代,但是一旦将其放入char数组中,我什至无法再看到该值。 Thoughts? 思考?

{
  uint8_t buf[BUF_SIZE];
  memset(buf, 0, sizeof(buf));
  // Read data from the UART
  int checkIt = uart_write_bytes(UART_NUM_2, (const char *)hex, 
  strlen(hex));
  ESP_LOGI(LOG_TAG, "this is the length of transmit: %i: ", checkIt);
   int len2 = uart_read_bytes(UART_NUM_2, buf, BUF_SIZE - 1, 1000 
  portTICK_RATE_MS);

  ParseData(buf);

}

 ParseData(char * data)
{
 //initialize char array to zeros
 char temporary[4] = {0};

 for(int i=0; i<3; i++)
 {
  //first two bytes are not needed, so skip them for now
   temporary[i] = data[i+2];
   ESP_LOGI(LOG_TAG, " temporary # %i %i ", i, temporary[i]);
 }
temporary[3] = '\0';
ESP_LOGI(LOG_TAG, " temp char contents update %s ", temporary;

}

The for loop will show me each of the values, like 1, 2, 3 (individual integers sent as individual bytes, not a string and not '1', '2', '3' - but I wan't to combine it into 123 which is why I setup the temporary array. It is printing out nothing, even if I don't add the null character to it. If I can get it to a single value (123), or even a char (string type), then I can add it to a cJSON object and send it. for循环将向我显示每个值,例如1、2、3(作为整数发送的单个整数,不是字符串,也不是'1','2','3'-但是我不想将其组合到123中,这就是我设置临时数组的原因。即使我不向其中添加空字符,它也不会打印任何内容。如果我可以将其设置为单个值(123),甚至可以是一个char(字符串类型) ),然后可以将其添加到cJSON对象并发送。

If the line: 如果行:

ESP_LOGI(LOG_TAG, " temporary # %i %i ", i, temporary[i]);

is indicating integer values 1, 2, 3 then the data is integer data not character data. 如果表示数值1、2、3,则数据是整数数据,而不是字符数据。 To represent the data as a string of decimal digit characters change: 要将数据表示为一串十进制数字字符,请进行以下更改:

temporary[i] = data[i+2];

to

temporary[i] = '0' + data[i+2];

However it seems unlikely that the data is intended to be interpreted in that manner - it is a very inefficient and wasteful method of transferring integer data. 但是,似乎不太可能以这种方式解释数据-这是一种传输整数数据的非常低效且浪费的方法。 Are you sure that he data is not in fact a single 24 bit integer, where rather then the loop and array, you in fact need: 您确定他的数据实际上不是单个24位整数,而是循环和数组,实际上您需要:

int value = data[2] << 16 | data[3] << 8 | data[4] ;

or 要么

int value = data[4] << 16 | data[3] << 8 | data[2] ;

depending on the byte order of the data? 取决于数据的字节顺序?

So, it appears that for my situation, I am converting individual bytes read from a UART to an integer 因此,看来我的情况是,我将从UART读取的单个字节转换为整数

char data[3]  = {0};
data[0] = 0x01
data[1] = 0x02
data[2] = 0x03

ParseData(data);

void ParseData(uint8_t * data)
{
    uint32_t val = (data[0] * 100) + (data[1] * 10) + data[2];
    ESP_LOGI(LOG_TAG, " val # %i ", val);
} 

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

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