UART read timeout looks not working
Posted: Sat Jan 09, 2021 12:35 pm
Hello,
I am connected to communication line. Device is sending two frames of bytes with gap between.
I use code:
When printing to console I get 26 bytes instead of 2 x 13 bytes.
I used:
#define UART_TOUT_THRESH_DEFAULT (1)
uart_intr.rx_timeout_thresh = 1
and
uart_enable_rx_intr(UART_NUM_1);
but all of that looks not working.
The gap between frames is c.a. 180 ms (1.5 size of byte).
What I am doing wrong? How to solve this?
Thanks in advance.
I am connected to communication line. Device is sending two frames of bytes with gap between.
I use code:
Code: Untitled.c Select all
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "driver/uart.h"
#include "string.h"
#include "driver/gpio.h"
static const int RX_BUF_SIZE = 128;
#define TXD_PIN (GPIO_NUM_4)
#define RXD_PIN (GPIO_NUM_5)
#define UART_TOUT_THRESH_DEFAULT (1)
void init(void)
{
const uart_config_t uart_config = {
.baud_rate = 100,
.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,
};
const uart_intr_config_t uart_intr = {
// .intr_enable_mask = UART_RXFIFO_TOUT_INT_ENA_M,
// .rxfifo_full_thresh = 1, //UART_FULL_THRESH_DEFAULT, //120 default!! aghh! need receive 120 chars before we see them
.rx_timeout_thresh = 1, //UART_TOUT_THRESH_DEFAULT, //10 works well for my short messages I need send/receive
// .txfifo_empty_intr_thresh = 10, //UART_EMPTY_THRESH_DEFAULT
};
uart_intr_config(UART_NUM_1, &uart_intr);
uart_set_rx_timeout(UART_NUM_1, 1);
uart_enable_rx_intr(UART_NUM_1);
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);
uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
}
static void rx_task(void *arg)
{
LOG(LL_INFO, ("Start receiving"));
uint8_t *data = (uint8_t *)malloc(RX_BUF_SIZE + 1);
while (1)
{
int rxBytes = 0;
rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 10);
if (rxBytes > 0)
{
data[rxBytes] = 0;
LOG(LL_INFO, ("Read %d bytes: '%s'", rxBytes, data));
}
}
free(data);
}
enum mgos_app_init_result mgos_app_init(void)
{
init();
xTaskCreate(rx_task, "uart_rx_task", 1024 * 2, NULL, configMAX_PRIORITIES, NULL);
return MGOS_APP_INIT_SUCCESS;
}I used:
#define UART_TOUT_THRESH_DEFAULT (1)
uart_intr.rx_timeout_thresh = 1
and
uart_enable_rx_intr(UART_NUM_1);
but all of that looks not working.
The gap between frames is c.a. 180 ms (1.5 size of byte).
What I am doing wrong? How to solve this?
Thanks in advance.