ESP32/UART : Buffer is null

aakash
Posts: 3
Joined: Wed Apr 07, 2021 5:35 am

Re: ESP32/UART : Buffer is null

Postby aakash » Wed Apr 07, 2021 5:40 am

i was using ESP32 wroom and stm32g431RB i was just using HAL uart liberary to sent At commands to ESp32 but im unable to read ok response from the esp32 ....are der any suggestions.

ESP_Sprite
Posts: 9761
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32/UART : Buffer is null

Postby ESP_Sprite » Wed Apr 07, 2021 9:25 am

You're hijhacking a thread with your problem. Suggest you collect more information and start your own thread.

BiHan90
Posts: 12
Joined: Thu Apr 01, 2021 9:29 am

Re: ESP32/UART : Buffer is null

Postby BiHan90 » Wed Apr 07, 2021 6:49 pm

@ESP_Sprite.. So i did add '\0' at the end of the string but nothing changed, the serial monitor is always rubbish. what's weird to me is that i can see a response on the oscilloscope when i do an echo, so that means my esp32 is receiving correctly via rx buffer and write the data back via tx buffer.

ESP_Sprite
Posts: 9761
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32/UART : Buffer is null

Postby ESP_Sprite » Mon Apr 12, 2021 2:53 am

Hm, dunno. Just for shits and giggles, can you post your current code again? Perhaps there's still something wrong with it.

BiHan90
Posts: 12
Joined: Thu Apr 01, 2021 9:29 am

Re: ESP32/UART : Buffer is null

Postby BiHan90 » Sat Apr 24, 2021 9:40 pm

Code: Select all

#include <stdio.h>
#include <esp_vfs_fat.h>
#include <esp_modbus_slave.h>
#include <esp_err.h>
#include "UART.h"

void app_main() {
    int uart_buffer_size = (1024*2);
    const int uart_num = UART_NUM_2;

    uart_config_t uart_config = {
            .baud_rate = 115200,
            .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);
    QueueHandle_t uart_queue;
    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, uart_buffer_size, 10, &uart_queue, 0);

    uint8_t *data = malloc(uart_buffer_size);

    while(1) {
        int length;
        length = uart_read_bytes(uart_num, data, 1,50 / portTICK_RATE_MS);
        printf("%s", data);
        if (length > 0) {
            data[length] = '\0';
            printf("%s", data);
            fflush(stdout);
        }
    }
}

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: ESP32/UART : Buffer is null

Postby WiFive » Sun Apr 25, 2021 2:26 am

Code: Select all

uart_set_pin(uart_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
Why do people always do this? (Probably because all of the example code that uses it with uart0.) Specify your pins explicitly.

BiHan90
Posts: 12
Joined: Thu Apr 01, 2021 9:29 am

Re: ESP32/UART : Buffer is null

Postby BiHan90 » Sun Apr 25, 2021 9:46 am

I have already specified them earlier but then I changed them into default hoping it could help!

ESP_Sprite
Posts: 9761
Joined: Thu Nov 26, 2015 4:08 am

Re: ESP32/UART : Buffer is null

Postby ESP_Sprite » Mon Apr 26, 2021 3:51 am

You do printf("%s", data); twice, and the first time the data var still is not zero-terminated.

BiHan90
Posts: 12
Joined: Thu Apr 01, 2021 9:29 am

Re: ESP32/UART : Buffer is null

Postby BiHan90 » Mon Apr 26, 2021 8:39 am

At the beginning, I was using only the second one (the one terminated with 0) but then I added the first one (before the while loop) just to compare.

BiHan90
Posts: 12
Joined: Thu Apr 01, 2021 9:29 am

Re: ESP32/UART : Buffer is null

Postby BiHan90 » Thu Apr 29, 2021 3:10 pm

Ok so I partially fixed the issue and now I can read the letter "p" that is being transmitting from a pic microcontroller. but I'm having this Guru meditation error after the buffer get full. I tried to flush after the loop but it didn't help. The code is below :

Code: 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 = 1024;

#define TXD_PIN (GPIO_NUM_4)
#define RXD_PIN (GPIO_NUM_5)

#define TXD_PIN (GPIO_NUM_18)
#define RXD_PIN (GPIO_NUM_19)

void init(void) {
    const uart_config_t uart_config = {
            .baud_rate = 115200,
            .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,
    };
    // We won't use a buffer for sending data.
    uart_driver_install(UART_NUM_1, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
    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);
}


static void rx_task(void *arg)
{
    static const char *RX_TASK_TAG = "RX_TASK";
    esp_log_level_set(RX_TASK_TAG, ESP_LOG_INFO);
    uint8_t* data = (uint8_t*) malloc(RX_BUF_SIZE+1);
    while (1) {
        const int rxBytes = uart_read_bytes(UART_NUM_1, data, RX_BUF_SIZE, 1000 / portTICK_RATE_MS);
        if (rxBytes > 0) {
            data[rxBytes] = 0;
            ESP_LOGI(RX_TASK_TAG, "Read %d bytes: '%s'", rxBytes, data);
            //vTaskDelay(500);
            ESP_LOG_BUFFER_HEXDUMP(RX_TASK_TAG, data, rxBytes, ESP_LOG_INFO);
        }
    }
    free(data);
}

void app_main(void)
{
    init();
    xTaskCreate(rx_task, "uart_echo_task", 1024, NULL, 10, NULL);
}
The error is :

I (518) RX_TASK: Read 1024 bytes: 'ppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppppGuru Meditation Error: Core 0 panic'ed (IllegalInstruction). Exception was unhandled.

Core 0 register dump:
PC : 0x70707070 PS : 0x00050033 A0 : 0x70707070 A1 : 0x3ffb7150
A2 : 0x70707070 A3 : 0x70707070 A4 : 0x70707070 A5 : 0x70707070
A6 : 0x70707070 A7 : 0x70707070 A8 : 0x70707070 A9 : 0x70707070
A10 : 0x70707070 A11 : 0x70707070 A12 : 0x70707070 A13 : 0x70707070
A14 : 0x70707070 A15 : 0x3ffb7520 SAR : 0x00000004 EXCCAUSE: 0x00000000
EXCVADDR: 0x00000000 LBEG : 0x400014fd LEND : 0x4000150d LCOUNT : 0xfffffeff

Backtrace:0x7070706d:0x3ffb7150 |<-CORRUPTED


ELF file SHA256: fe7687a6cb18dc92

Rebooting...

Who is online

Users browsing this forum: No registered users and 107 guests