uart_read_bytes() and UART_PATTERN_DET do not work well together
Posted: Mon Jul 31, 2017 1:21 am
Hi,
I have some time critical code where I need to read data from the UART, wait for a specific character (using UART_PATTERN_DET), read the buffer up until the detected character and then, if conditions are met, write a new set of characters to UART TX (within 4 milliseconds). The only issue is that when an UART_PATTERN_DET event is triggered and I use uart_read_bytes() no characters are in the buffer. I set ticks_to_wait to zero, because this data should be in the UART ringbuffer. I would expect that the uart_read_bytes() to return all the characters up until the UART_PATTERN_DET event was triggered.
How do I get all the characters in the UART buffer after the UART_PATTERN_DET event is triggered without waiting any ticks?
Below is the sample code (note: CONFIG_FREERTOS_HZ = 1000).
I have some time critical code where I need to read data from the UART, wait for a specific character (using UART_PATTERN_DET), read the buffer up until the detected character and then, if conditions are met, write a new set of characters to UART TX (within 4 milliseconds). The only issue is that when an UART_PATTERN_DET event is triggered and I use uart_read_bytes() no characters are in the buffer. I set ticks_to_wait to zero, because this data should be in the UART ringbuffer. I would expect that the uart_read_bytes() to return all the characters up until the UART_PATTERN_DET event was triggered.
How do I get all the characters in the UART buffer after the UART_PATTERN_DET event is triggered without waiting any ticks?
Below is the sample code (note: CONFIG_FREERTOS_HZ = 1000).
Code: Select all
while (true) {
if (pdTRUE == xQueueReceive(uart_read_queue, &uart_read_event, 0)) {
switch (uart_read_event.type) {
case UART_PATTERN_DET:
uart_get_buffered_data_len(UART_NUM_1, ¤t_buf_len); // This always sets current_buf_len to zero.
// Read the data, set the wait time to zero, because all the data should be in the buffer already.
num_bytes_read = uart_read_bytes(UART_NUM_1, uart_read_buf, current_buf_len, 0);
ESP_LOGI("uart", "UART_PATTERN_DET: current_buf_len=>%d, read %d bytes", current_buf_len, num_bytes_read);
break;
case UART_DATA:
uart_get_buffered_data_len(UART_NUM_1, ¤t_buf_len);
num_bytes_read = uart_read_bytes(UART_NUM_1, uart_read_buf, current_buf_len, 0);
ESP_LOGI("uart", "UART_DATA: current_buf_len=>%d, read %d bytes", current_buf_len, num_bytes_read);
break;
default:
ESP_LOGI("uart", "other event.");
break;
}
}
}