Uart: receives unexpected values

ArminArmin
Posts: 9
Joined: Mon Aug 08, 2022 6:14 pm

Uart: receives unexpected values

Postby ArminArmin » Fri Mar 10, 2023 10:31 pm

Hello,

What is the problem? The received data from uart pins of esp32 is different from normal putty communication.

What am I trying to do? what is my approach?
I am sending some data through UART (pins 33, 32) to a device.
Once I send command in putty, the responses that I get is either "ok" or "err" (based on the validity of the command), but the below code in esp32 is getting either "oC", "oJ", "erR" or nothing.

The code that I am using:

Code: Select all

#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/uart.h"
#include "esp_log.h"

#define READ_BUF_SIZE (1024)

static const char *TAG = "esp";

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,
};

uart_port_t uart_1_init()
{
    uart_port_t uart_num_1 = UART_NUM_1;

    ESP_ERROR_CHECK(uart_param_config(uart_num_1, &uart_config));
    uart_set_pin(uart_num_1, 33, 32, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
    ESP_ERROR_CHECK(uart_driver_install(uart_num_1, READ_BUF_SIZE * 2, 0, 0, NULL, 0));

    return uart_num_1;
}

static char data_rec[1024];

void app_main(void)
{
    uart_port_t uart_num_1 = uart_1_init();

    while (1)
    {
        uart_write_bytes(uart_num_1, "command", strlen((const char *)"command"));

        uart_read_bytes(uart_num_1, data_rec, 1024, 100 / portTICK_PERIOD_MS);
        printf("Data: %s \n", data_rec);
    }
}
I tried to use interrupt and sample code, but after compiling and running the sample code it stucks in this line:

Code: Select all

cpu_start: Starting scheduler on P
What is the solution to get the reliable output in the esp32?

Thanks

Who is online

Users browsing this forum: hiengee, suycksuyck and 128 guests