在这里,我正在从与另一个正在发送字节的设备通信的 UART 中读取数据。如果我得到一个 3 个字节的整数值,如何最好地将它转换为我可以使用 cJSON 发送的东西。我可以获取该值并对其进行迭代,但是一旦将其放入 char 数组中,我什至无法再看到该值。想法?
{
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;
}
for 循环将向我显示每个值,例如 1、2、3(作为单个字节发送的单个整数,而不是字符串而不是 '1'、'2'、'3' - 但我不想将它组合起来进入 123 这就是我设置临时数组的原因。即使我没有向其中添加空字符,它也不会打印任何内容。如果我可以将其设置为单个值(123),甚至是 char(字符串类型),然后我可以将它添加到一个 cJSON 对象并发送它。