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

wegunterjr
Posts: 34
Joined: Thu Jun 07, 2018 3:05 am

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

Postby wegunterjr » Mon Dec 31, 2018 6:56 am

Here I am reading from a UART that is communicating with another device that is sending it bytes.
If I am getting an integer value in 3 bytes, how best to convert it to something I can send using 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.
Thoughts?

Code: Select all

	
{
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 - 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.

fivdiAtESP32
Posts: 47
Joined: Thu Dec 20, 2018 9:47 am

Re: How best to convert uint8_t from uart_read_bytes to a char or something that can be added to cJSON object?

Postby fivdiAtESP32 » Mon Dec 31, 2018 11:52 am

It's not fully clear if the protocol is a binary protocol or plain text protocol. It appears to be a binary protocol. If so, try with one of the following implementations of ParseData to see if it works.

Code: Select all

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

Code: Select all

void ParseData(uint8_t * data)
{
    uint32_t val = (data[0] << 16) + (data[1] << 8) + data[2];
    ESP_LOGI(LOG_TAG, " val # %i ", val);
}
These implementations of ParseData assume that the integer is an unsigned integer.

Note that if it is a binary protocol the call to strlen in the following line of code is likely to be incorrect as it will stop counting the characters when it finds a '\0' which may not be what you want.

Code: Select all

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

wegunterjr
Posts: 34
Joined: Thu Jun 07, 2018 3:05 am

Re: How best to convert uint8_t from uart_read_bytes to a char or something that can be added to cJSON object?

Postby wegunterjr » Mon Dec 31, 2018 11:57 pm

This one worked for me:
uint32_t val = (data[0] * 100) + (data[1] * 10) + data[2];

Thanks!

Who is online

Users browsing this forum: MSN [Bot], Snedig, thethinker and 49 guests