ESP32S3 IDF4.4.1 使用串口出现串口溢出的问题 求教。

zldiy_SINBA
Posts: 5
Joined: Thu Sep 29, 2022 10:41 am

ESP32S3 IDF4.4.1 使用串口出现串口溢出的问题 求教。

Postby zldiy_SINBA » Thu Jul 13, 2023 1:28 am

外部硬件: 使用两个串口不停接收数据,偶尔(也不是经常出现这样的问题)出现上电后,cpu串口buff溢出的情况,导致系统不停重启。
请教这样问题的原因是什么?如何解决?谢谢。
调试信息如下:

Code: Select all

I (36020) rr: Check sampling line
I (36020) rr: No adapter,connect adapter
I (36170) bsp_uart: UART buff full.uart2
RR rec toggl1
I (37160) bsp_uart: UART buff full.uart2
I (37770) bsp_uart: UART buff full.uart1
RR rec toggl1
I (38480) bsp_uart: UART fifo OV.uart2
ESP-ROM:esp32s3-20210327
Build:Mar 27 2021
rst:0x8 (TG1WDT_SYS_RST),boot:0x2f (SPI_FAST_FLASH_BOOT)
Saved PC:0x42002f85
0x42002f85: panic_handler at D:/Espressif/frameworks/esp-idf-v4.4.2/components/esp_system/port/panic_handler.c:148 (discriminator 3)
串口接收程序如下:

Code: Select all


//-----------------------------------------------------------------------------------------
void uart_event_handle(uart_port_t uart_num, QueueHandle_t queue, uart_event_t *event)
{
//	int length = 0;
	uint8_t *dtmp = malloc(BUF_SIZE);
	//char *dest = malloc(BUF_SIZE);

  bzero(dtmp, BUF_SIZE);
	//bzero(dest, BUF_SIZE);
 
	switch (event->type)
	{
		//Event of UART receving data
		/*We'd better handler data event fast, there would be much more data events than
		other types of events. If we take too much time on data event, the queue might
		be full.*/
		case UART_DATA:
			//ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length));
			uart_read_bytes(uart_num, dtmp,event->size, portMAX_DELAY); //
			//uart_write_bytes(uart_num, (const char *) dtmp, event->size);

			//HexToAscii(dtmp, dest, event->size);
      //ESP_LOGI(TAGu, "UART[%d] HEXdata:%s lenth=%d",uart_num,dest,event->size);

			if(uart_num == UART1_PORT_NUM)
				anlyze_SPO2(dtmp);
			else if(uart_num == UART2_PORT_NUM)
			{
			  //analzye_co2(dtmp);			
				data_analysis(dtmp[0]);
				exchange_value(&etco2_sensor);
				//HexToAscii(dtmp, dest, event->size);
        //ESP_LOGI(TAGu, "UART[%d] HEXdata:%s lenth=%d",uart_num,dest,event->size);
			}
			break;
 
		//Event of HW FIFO overflow detected
		case UART_FIFO_OVF:
			// If fifo overflow happened, you should consider adding flow control for your application.
			// The ISR has already reset the rx FIFO,
			// As an example, we directly flush the rx buffer here in order to read more data.
			// uart_flush_input(uart_num);
			uart_read_bytes(uart_num, dtmp,event->size, portMAX_DELAY); 
			bzero(dtmp, BUF_SIZE);

			uart_flush(uart_num);
			xQueueReset(queue);
      ESP_LOGI(TAGu, "UART fifo OV.uart%d",uart_num); 
			break;
 
		//Event of UART ring buffer full
		case UART_BUFFER_FULL:
			// If buffer full happened, you should consider encreasing your buffer size
			// As an example, we directly flush the rx buffer here in order to read more data.
		  // uart_flush_input(uart_num);
			uart_read_bytes(uart_num, dtmp,event->size, portMAX_DELAY); 
			bzero(dtmp, BUF_SIZE);

			uart_flush(uart_num);
			xQueueReset(queue);
      ESP_LOGI(TAGu, "UART buff full.uart%d",uart_num); 
			break;
 
		//Event of UART RX break detected
		case UART_BREAK:
			break;
 
		//Event of UART parity check error
		case UART_PARITY_ERR:
			break;
 
		//Event of UART frame error
		case UART_FRAME_ERR:
			break;
 
		//UART_PATTERN_DET
		case UART_PATTERN_DET:
			break;
 
		//Others
		default:
			break;
	}
    
  free(dtmp);
	//free(dest);
	dtmp = NULL;
}

bidrohini
Posts: 202
Joined: Thu Oct 27, 2022 12:55 pm

Re: ESP32S3 IDF4.4.1 使用串口出现串口溢出的问题 求教。

Postby bidrohini » Thu Jul 13, 2023 8:03 am

根据提供的信息,问题可能是由于串口缓冲区溢出引起的,这可能导致系统不停重启。这种情况通常发生在接收数据的速度超过处理数据的速度时。

以下是一些解决方案和建议:

1. 增加串口缓冲区大小:可以尝试增加串口接收缓冲区的大小,以容纳更多的数据。可以通过修改 `BUF_SIZE` 的值来实现。增加缓冲区大小可能会缓解缓冲区溢出的问题。

2. 提高数据处理速度:检查数据处理的代码,确保处理数据的速度足够快,以跟上串口接收数据的速度。如果处理数据的代码较慢,可能会导致数据在缓冲区中积压,最终导致缓冲区溢出。

3. 使用流控制:考虑在应用程序中添加流控制机制,以确保数据接收和处理之间的平衡。流控制可以通过硬件或软件实现,例如使用 RTS/CTS 硬件流控制信号或通过控制接收数据的速率来实现。

4. 优化代码逻辑:检查串口接收数据的代码逻辑,确保它高效并避免不必要的延迟或处理操作。优化代码可以改善数据处理速度,减少缓冲区溢出的可能性。

5. 调整串口配置参数:尝试调整串口的配置参数,例如波特率、数据位、停止位和校验位等,以提高串口传输的效率和稳定性。

6. 调试和日志记录:在代码中添加适当的调试信息和日志记录,以便跟踪问题和识别潜在的瓶颈。记录串口接收和处理数据的时间戳,以及缓冲区状态和大小的相关信息,有助于分析问题的根本原因。

7. 硬件优化:如果可能的话,检查硬件连接和线路,确保没有信号干扰或电气问题。使用合适的线缆和电气隔离方法,以减少串口通信的干扰和错误。

请根据您的具体应用场景和需求选择适合的解决方案。如果问题仍然存在,可能需要更详细的调试和分析来确定问题的根本原因。

zldiy_SINBA
Posts: 5
Joined: Thu Sep 29, 2022 10:41 am

Re: ESP32S3 IDF4.4.1 使用串口出现串口溢出的问题 求教。

Postby zldiy_SINBA » Fri Jul 14, 2023 9:32 am

Code: Select all

//Event of HW FIFO overflow detected
		case UART_FIFO_OVF:
			// If fifo overflow happened, you should consider adding flow control for your application.
			// The ISR has already reset the rx FIFO,
			// As an example, we directly flush the rx buffer here in order to read more data.
			// uart_flush_input(uart_num);
			uart_read_bytes(uart_num, dtmp,event->size, portMAX_DELAY); 
			bzero(dtmp, BUF_SIZE);

			uart_flush(uart_num);
			xQueueReset(queue);
      ESP_LOGI(TAGu, "UART fifo OV.uart%d",uart_num); 
			break;
 
		//Event of UART ring buffer full
		case UART_BUFFER_FULL:
			// If buffer full happened, you should consider encreasing your buffer size
			// As an example, we directly flush the rx buffer here in order to read more data.
		  // uart_flush_input(uart_num);
			uart_read_bytes(uart_num, dtmp,event->size, portMAX_DELAY); 
			bzero(dtmp, BUF_SIZE);

			uart_flush(uart_num);
			xQueueReset(queue);
      ESP_LOGI(TAGu, "UART buff full.uart%d",uart_num); 
			break;

非常感谢大神指导: 但是在我在OV和full事件中,增加了 清除缓存的API

Code: Select all

			
			bzero(dtmp, BUF_SIZE);
			uart_flush(uart_num);
			xQueueReset(queue);
,是没有起到作用吗?或者用其他什么方法来清除缓存?

Who is online

Users browsing this forum: No registered users and 43 guests