uart_read_bytes returning 0 bytes in long run

Kulasekaran
Posts: 4
Joined: Wed May 29, 2024 11:00 am

uart_read_bytes returning 0 bytes in long run

Postby Kulasekaran » Wed Jun 05, 2024 2:03 pm

Hello,

I'm using the uart_read_bytes() function to read data from a LTE chip. My application is working fine for one hour but after that I'm not getting any data. I probed the LTE TX pin and I could see the LTE sending data fine. But after one hour, I see "No data received"

My logic in code is like this:

Code: Select all

length = uart_read_bytes(UART_NUM, buffer, ticks_to_wait);
if (length > 0) //do something with the recvd data in the buffer
else print("No data received");
I tried to re-configure the UART and I also tried using uart_flush() functions. But both didn't help.

Please help me in resolving this.

Thanks

Kulasekaran
Posts: 4
Joined: Wed May 29, 2024 11:00 am

Re: uart_read_bytes returning 0 bytes in long run

Postby Kulasekaran » Mon Jun 10, 2024 5:18 am

Some help would be really appreciated. Stuck at this for nearly 3 weeks :?

MicroController
Posts: 1701
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: uart_read_bytes returning 0 bytes in long run

Postby MicroController » Mon Jun 10, 2024 11:21 am

Without seeing any of your code it's pretty hard to tell where the error might be...

Kulasekaran
Posts: 4
Joined: Wed May 29, 2024 11:00 am

Re: uart_read_bytes returning 0 bytes in long run

Postby Kulasekaran » Mon Jun 10, 2024 12:31 pm

My UART CONFIG settings:

Code: Select all

const uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_APB,
};
uart_driver_install(UART_NUM_1, 256, 0, 0, NULL, 0);
uart_param_config(UART_NUM_1, &uart_config);
uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
The part where my code checks the UART RX data :

Code: Select all

int8_t fetch_and_check_data(uint16_t timeout_ms, char *check_string, char *cmd_name)
{
        uart_flush_input(UART_NUM_1);
	size_t ring_buf_len;
	static uint8_t uart_reinit_count = 0;
	char *LTE_UART_data = (char *)calloc(BUF_SIZE, sizeof(char));
	if (LTE_UART_data == NULL)
	{
		snprintf(lte_log_buffer, sizeof(lte_log_buffer), "Memory allocation failed for LTE_uart_data");
		red_printf(LTE_ERROR_TAG, lte_log_buffer);
		return FAILURE;
	}
	uint32_t in_time = esp_timer_get_time();
	while ((esp_timer_get_time() - in_time) / 1000 < timeout_ms)
	{
		int length = uart_read_bytes(UART_NUM_1, LTE_UART_data, BUF_SIZE, pdMS_TO_TICKS(200));
		uart_get_buffered_data_len(UART_NUM_1, &ring_buf_len);
		if (length > 0)
		{
			sprintf(lte_log_buffer, "AT_CMD sent : %s | HEAP : %d BYTES | BUF_LEN : %d | UART_RX_BUF_LEN : %d", cmd_name, heap_caps_get_free_size(MALLOC_CAP_8BIT), strlen(LTE_UART_data), ring_buf_len);
			cyan_printf(LTE_DEBUG_TAG, lte_log_buffer);
			// It's safe to add to pubmesg now that we've received some data over UART from LTE
			hold_adding_to_pubmesg = false;
			if (check_response(LTE_UART_data, check_string) == SUCCESS)
			{
				if (LOG_DATA)
				{
					sprintf(lte_log_buffer, "%d bytes Data Received : %s", length, LTE_UART_data);
					cyan_printf(LTE_DEBUG_TAG, lte_log_buffer);
				}
				/*If its the case of READ MESG, then we need to parse JSON*/
				if (strstr(LTE_UART_data, "{"))
				{
					strcpy(LTE_UART_data, strstr(LTE_UART_data, "{"));
					parse_json_packet(LTE_UART_data);
				}
				free(LTE_UART_data);
				return SUCCESS;
			}
			else
			{
				if (LOG_DATA)
				{
					sprintf(lte_log_buffer, "%d bytes of Data Received : %s", length, LTE_UART_data);
					red_printf(LTE_ERROR_TAG, lte_log_buffer);
				}
				free(LTE_UART_data);
				return FAILURE;
			}
		}
	}
	sprintf(lte_log_buffer, "No Data | data_len : %d | ring_buf_len : %d", strlen(LTE_UART_data), ring_buf_len);

	// To avoid mem leak due to LTE no reponse issue, we're using this flag to hold publishing more to the pubmesg queue,
	// cuz it's of no use, when LTE is not responding. This leads to loss of data. We need to fix this later
	hold_adding_to_pubmesg = true;

	uart_reinit_count++;
	if (uart_reinit_count > 5)
	{
		uart_reinit_count = 0;
		sprintf(lte_log_buffer, "Re-initializing LTE UART");
		cyan_printf(LTE_DEBUG_TAG, lte_log_buffer);
		LTE_UART_INIT();
	}

	network_flag = 0;
	red_printf(LTE_ERROR_TAG, lte_log_buffer);
	free(LTE_UART_data);
	return FAILURE;
}
NOTE:
  • When the code is working fine, the uart_get_buffered_len() is always returning 0. But once the issue occurs, it returns 1070220272. I've tried uninstalling and reinstalling the driver, I've tried uart_flush_input() both before writing to TX and after writing to TX. Nothing seems to work. Attaching an image for reference that contains the actual point where the issue starts occurring.
Screenshot (8).png
Image showing the output log with the exact time when the issue starts occurring.
Screenshot (8).png (1.88 MiB) Viewed 1998 times

ESP_Sprite
Posts: 9724
Joined: Thu Nov 26, 2015 4:08 am

Re: uart_read_bytes returning 0 bytes in long run

Postby ESP_Sprite » Tue Jun 11, 2024 5:33 am

It feels like memory corruption. uart_get_buffered_data_len simply returns the value of a field of the UART object; there's no chance that that suddenly gets a number that is so high. I'd turn on relevant memory debugging options in ESP-IDF, maybe they can find something.

Kulasekaran
Posts: 4
Joined: Wed May 29, 2024 11:00 am

Re: uart_read_bytes returning 0 bytes in long run

Postby Kulasekaran » Tue Jun 11, 2024 5:42 am

Ok I'll check for memory corruption. But I've also printed out free heap. Until the issue occurs, the heap seems to be fine. I'm not seeing any memory leaks. So, I thought maybe memory must not be the issue. If there's other memory related things that I need to check upon, I'll do that. I'm new to this. So, I'm not entirely sure which memory elements to look up for. Thanks !

Who is online

Users browsing this forum: No registered users and 151 guests