ESP32/UART : Buffer is null
Posted: Thu Apr 01, 2021 9:49 am
Hello guys,
I'm testing the UART connection between ESP32 (as receiver) and a PIC microcontroller (transmitting an integer). The code below is for the ESP32 side in order to configure it for the UART reception.
[Codebox]
#include <stdio.h>
#include <esp_vfs_fat.h>
#include <esp_modbus_slave.h>
#include <esp_err.h>
#include "UART.h"
void app_main() {
int len = 0;
int data = 0;
const int uart_buffer_size = (1024*2);
const int uart_num = UART_NUM_2;
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,
.source_clk = UART_SCLK_APB,
};
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, uart_buffer_size, 0, 0, NULL, 0);
while (1) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));
// Read data from the UART
len = uart_read_bytes(uart_num, data, uart_buffer_size, 20 / portTICK_RATE_MS);
printf("data is %d", data);
// Write data back to the UART
uart_write_bytes(uart_num, (const char *) data, len);
}
}[/Codebox]
After building and flushing, the buffer always reads null :
E (1875) uart: uart_read_bytes(1155): uart data null
E (1885) uart: uart_write_bytes(1122): buffer null
Could you please highlight where the issue is ?
Thanks in advance
I'm testing the UART connection between ESP32 (as receiver) and a PIC microcontroller (transmitting an integer). The code below is for the ESP32 side in order to configure it for the UART reception.
[Codebox]
#include <stdio.h>
#include <esp_vfs_fat.h>
#include <esp_modbus_slave.h>
#include <esp_err.h>
#include "UART.h"
void app_main() {
int len = 0;
int data = 0;
const int uart_buffer_size = (1024*2);
const int uart_num = UART_NUM_2;
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,
.source_clk = UART_SCLK_APB,
};
// Configure UART parameters
uart_param_config(uart_num, &uart_config);
uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
uart_driver_install(uart_num, uart_buffer_size, 0, 0, NULL, 0);
while (1) {
ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&len));
// Read data from the UART
len = uart_read_bytes(uart_num, data, uart_buffer_size, 20 / portTICK_RATE_MS);
printf("data is %d", data);
// Write data back to the UART
uart_write_bytes(uart_num, (const char *) data, len);
}
}[/Codebox]
After building and flushing, the buffer always reads null :
E (1875) uart: uart_read_bytes(1155): uart data null
E (1885) uart: uart_write_bytes(1122): buffer null
Could you please highlight where the issue is ?
Thanks in advance