How UART working?
Posted: Sat Jun 05, 2021 7:04 am
Hello:
I'm trying to read 189 bytes from UART, but some times I doesnt work well.
I always receive the first 120 bytes from uart_read_bytes(), then task wait 30ms (vTaskDelay(30/portTICK_RATE_MS)) and the next
uart_read_bytes() give me 0 and other times give me 69 bytes. When I receive 0, the ProcessRecData fail because the frame is incomplete, and the frame CRC is wrong (logical).
I dont understand why in the first step always a get the first 120 bytes (why 120?) and why at the second step sometimes get 0 bytes or 69 bytes. If the baudrate is 9600, the byte time is 1ms
I checked portTICK_RATE_MS and it is 10 and RX_BUF_SIZE is 1024 bytes.
The system is a Modbus communication with diferent frame sizes and i want to detect the frame with the time without data in the line, for that I use the 0 bytes received after a 30ms as end frame.
Some one have a idea why this happen?
Thank you in advance
I'm trying to read 189 bytes from UART, but some times I doesnt work well.
Code: Select all
void init_uart() {
const uart_config_t uart_config = {
.baud_rate = 9600,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.rx_flow_ctrl_thresh = 122
};
uart_param_config(UART_NUM_1, &uart_config);
uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, RTS_PIN, UART_PIN_NO_CHANGE);
// We won't use a buffer for sending data.
uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
uart_set_mode(UART_NUM_1, UART_MODE_RS485_HALF_DUPLEX);
xTaskCreate(rx_task, "uart_rx_task", 1024*5, NULL, configMAX_PRIORITIES, NULL);
printf("create uart_rx_task\n");
}
static void rx_task()
{
static const char *RX_TASK_TAG = "RX_TASK";
static unsigned int recv_index;
static unsigned int i;
esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1);
uint8_t* recv_buffer = (uint8_t*) malloc(RX_BUF_SIZE+1);
recv_index=0;
while (1)
{
conts int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE,40/portTICK_RATE_MS);
if (rxBytes > 0)
{
//Save rec data
for(i=0;i<rxBytes;i++)
if((recv_index+i)<RX_BUF_SIZE) recv_buffer[recv_index+i] =data[i];
recv_index=recv_index+rxBytes;
}
else
{
if(recv_index>0) ProcessRecData();
recv_index=0;
}
vTaskDelay(30/portTICK_RATE_MS);
}
}
uart_read_bytes() give me 0 and other times give me 69 bytes. When I receive 0, the ProcessRecData fail because the frame is incomplete, and the frame CRC is wrong (logical).
I dont understand why in the first step always a get the first 120 bytes (why 120?) and why at the second step sometimes get 0 bytes or 69 bytes. If the baudrate is 9600, the byte time is 1ms
I checked portTICK_RATE_MS and it is 10 and RX_BUF_SIZE is 1024 bytes.
The system is a Modbus communication with diferent frame sizes and i want to detect the frame with the time without data in the line, for that I use the 0 bytes received after a 30ms as end frame.
Some one have a idea why this happen?
Thank you in advance