Pointers 将cJSON中的valuestring或valueint转换为使用uart_write_字节发送失败

Pointers 将cJSON中的valuestring或valueint转换为使用uart_write_字节发送失败,pointers,uart,esp32,Pointers,Uart,Esp32,因此,目前,我正在发送一条JSON消息,其中包含一个需要从ESP32 UART UART_write_字节发送的值,但我不确定在转换过程中哪里出错。 目前,如果我发送234,它将作为50、51、52而不是234发送到UART。 思想? 我使用的是带有GCC编译器的esp idf,而不是Arduino char hex[8] = {0xff, 0xff, 0xff, 0xff}; cJSON* message' int intValue = 0; char *stringValue= "999";

因此,目前,我正在发送一条JSON消息,其中包含一个需要从ESP32 UART UART_write_字节发送的值,但我不确定在转换过程中哪里出错。 目前,如果我发送234,它将作为50、51、52而不是234发送到UART。 思想? 我使用的是带有GCC编译器的esp idf,而不是Arduino

char hex[8] = {0xff, 0xff, 0xff, 0xff};
cJSON* message'
int intValue = 0;
char *stringValue= "999";

if ((message = cJSON_GetObjectItem(root->child, "msg")) != NULL)
{
    if((intValue = cJSON_GetObjectItem(message, "Number")->valueint) != NULL)
    {
        ESP_LOGI(LOG_TAG, " this is the Number %i ", intValue);
    }
    if((stringValue = cJSON_GetObjectItem(message, "Number")->valuestring) != NULL)
    {
       ESP_LOGI(LOG_TAG, " This is NumberString %s ", stringValue);
    }   
}

char numStrStr[3];
sprintf(numStrStr, "%s", stringValue );
for(int j = 0; j < sizeof(str); j++)
{
    hex[j] = numStrStr[j];
}

int checkIt = uart_write_bytes(UART_NUM_2, (const char *)hex, strlen(hex));
charhex[8]={0xff,0xff,0xff,0xff};
cJSON*信息'
int值=0;
char*stringValue=“999”;
if((message=cJSON\u GetObjectItem(root->child,“msg”)!=NULL)
{
if((intValue=cJSON_GetObjectItem(消息,“数字”)->valueint)!=NULL)
{
ESP_LOGI(日志标签,“这是数字%i”,intValue);
}
if((stringValue=cJSON_GetObjectItem(消息,“数字”)->valuestring)!=NULL)
{
ESP_LOGI(日志标签,“这是数字字符串%s”,字符串值);
}   
}
char numstrstrstr[3];
sprintf(numstrstrstr,“%s”,stringValue);
对于(int j=0;j这是因为字符2('2')的ASCII码是50,字符3('3')的ASCII码是51,字符4('4')的ASCII码是52

int checkIt=uart\u write\u字节(uart\u NUM\u 2,(const char*)十六进制, 斯特伦(十六进制)

如果您观察到上面这一行,那么您已经显式地键入castedhex to const char*,基本上是用ASCII(假设是gcc编译器)编码数据,这是通过UART传输的

在UART接收设备或程序中,您需要将传入数据视为ASCII编码数据。您可以使用CoolTerm这样的终端程序来查看ASCII编码和/或原始数据


若要发送原始数据,可以使用const char*避免显式类型转换,前提是存储在十六进制中的数据是[0x02、0x03、0x04],而不是[2'、'3'、'4']

我将其用于以下更改: 我忘了提到我想一次发送一个字符,所以这意味着将'234'作为字符发送,然后将其转换为int,然后进行有趣的数学运算,将其分解为1、10和100以发送到串行

cJSON *message;
int intValue = 0;
const char *buffValue = "999";

hex[0] = 0xfe;
hex[1] = 0x01;
hex[2] = 0x01;
hex[3] = 0x00;

if((buffValue = cJSON_GetObjectItem(message, "NUmber")->valuestring) != NULL)
{
   ESP_LOGI(LOG_TAG, " This is Value String %s ", buffValue);
   ESP_LOGI(LOG_TAG, "strtol %ld ", strtol(buffValue, NULL, 10));
   intValue = strtol(buffValue, NULL, 10);

    int valueArray[3] = {0};
    int increment = 0;
    while(intValue > 0)
    {
        int digitValue = intValue % 10;
        valueArray[increment] = digitValue;
        intValue /= 10;
        increment ++;
    }
    hex[3] = valueArray[2];
    hex[4] = valueArray[1];
    hex[5] = valueArray[0];
}   


int checkIt = uart_write_bytes(UART_NUM_2, (const char *)hex, strlen(hex)); 

请指定缓冲区名称十六进制定义。还指定numStrStr缓冲区的内容。我已编辑以包含缺少的详细信息。很晚了,很抱歉。