UART rx length problem

espOAK
Posts: 9
Joined: Wed Mar 18, 2020 8:34 am

UART rx length problem

Postby espOAK » Tue Apr 14, 2020 1:15 pm

Hi,

I am trying to understand how to use uart_get_buffered_data_len().

My test code:
  1. uint8_t data[10];
  2. int len;
  3. const int uart_buffer_size = (1024 * 2);
  4. QueueHandle_t uart_queue;
  5.  
  6. uart_config_t uart_config =
  7. {
  8.     .baud_rate = 9600,
  9.     .data_bits = UART_DATA_8_BITS,
  10.     .parity = UART_PARITY_DISABLE,
  11.     .stop_bits = UART_STOP_BITS_1,
  12.     .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
  13.     .rx_flow_ctrl_thresh = 122,
  14. };
  15. ESP_ERROR_CHECK(uart_param_config(UART2, &uart_config));
  16. ESP_ERROR_CHECK(uart_set_pin(UART2, 17, 16, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));
  17. ESP_ERROR_CHECK(uart_driver_install(UART2, uart_buffer_size, 0, 10, &uart_queue, 0));
  18.  
  19. [
  20.     ... code, which send TX bytes. RX and TX pins connected together.
  21. ]
  22.  
  23. // Get RX length
  24. len = 0;
  25. while (len == 0)
  26. {
  27.     ESP_ERROR_CHECK(uart_get_buffered_data_len(UART2, (size_t*)&len));
  28. }
  29.    
  30. // Get RX byte
  31. len = 0;
  32. while (length != 1)
  33. {
  34.     len = uart_read_bytes(UART2, data, 1, 1);
  35. }
If I use the "Get RX length" part, the code get stuck forever in the while loop, but if I comment it out it works, ie there will be one rx byte read in the "Get RX byte" part.

Do I need to use uart_get_buffered_data_len() in some special way?

Who is online

Users browsing this forum: No registered users and 208 guests