UART_NUM_2 printing characters to serial monitor not working

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

UART_NUM_2 printing characters to serial monitor not working

Postby wegunterjr » Mon Nov 26, 2018 8:09 am

This is my code, but I am not printing the HEX, but the ASCII. I must be overlooking something in the conversion, but open for guidance:

Code: Select all

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_param_config(UART_NUM_2, &uart_config);
    uart_set_pin(UART_NUM_2, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS);
    uart_driver_install(UART_NUM_2, BUF_SIZE * 2, 0, 0, NULL, 0);
This is the portion that is writing and reading from UART_NUM2 (pins 18, 19)
// Configure a temporary buffer for the incoming data
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
    char chex[8] = {0xfe, 0x01, 0x03, 0x05, 0x00, 0xff, 0xff, 0xff};

    while (1) {
       uint8_t buf[BUF_SIZE];
    int testWrite = uart_write_bytes(UART_NUM_2, chex, sizeof(chex));
        ESP_LOGI(LOG_TAG, "Size of sizeof(chex): %i", sizeof(chex));
        int len = uart_read_bytes(UART_NUM_2, data, BUF_SIZE, 20 / portTICK_RATE_MS);

        if (len > 0)
        {
            /* Log received data to the console. */
            //data[len] = '\0';
            ESP_LOGI(LOG_TAG, ">%s<", data);
                         uart_flush(UART_NUM_2);
        }
       
The output to serial monitor looks like this:

Code: Select all

␛[0;32mI (192301) testUart: >�␁␃␅<␛[0m
␛[0;32mI (192301) testUart: Size of sizeof(chex): 8␛[0m
␛[0;32mI (192321) testUart: >�␁␃␅<␛[0m
␛[0;32mI (192321) testUart: Size of len: 8␛[0m
␛[0;32mI (192341) testUart: >�␁␃␅<␛[0m
␛[0;32mI (192341) testUart: Size of sizeof(chex): 8␛[0m
␛[0;32mI (192361) testUart: >�␁␃␅<␛[0m
␛[0;32mI (192361) testUart: Size of len: 8␛[0m

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

Re: UART_NUM_2 printing characters to serial monitor not working

Postby ESP_Sprite » Mon Nov 26, 2018 10:01 am

I see no issue in your code, seemingly you get eight bytes of binary gobbledygook and forward that to the log UART, where 'make monitor' happily prints that out as whatever ASCII characters they seem to map to? What did you expect to get?

wegunterjr
Posts: 37
Joined: Thu Jun 07, 2018 3:05 am

Re: UART_NUM_2 printing characters to serial monitor not working

Postby wegunterjr » Tue Nov 27, 2018 3:38 am

I guess I expected to see something besides the gobbly gook. I can parse the data and do work with the serial data received, but was hoping to see it on the screen to do some debugging.

Any suggestions on how best to do that?

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

Re: UART_NUM_2 printing characters to serial monitor not working

Postby ESP_Sprite » Tue Nov 27, 2018 4:26 am

It depends on what you know you'll receive. If it's a standardized type of packet, you can write code to dissect it and display the various fields. If not, you can show it as hex using e.g. the ESP_LOG_BUFFER_HEX function.

vilkoivshchi
Posts: 2
Joined: Wed Apr 13, 2022 8:19 am

Re: UART_NUM_2 printing characters to serial monitor not working

Postby vilkoivshchi » Wed Apr 13, 2022 8:37 am

Hello hello.
I switched from ESP8266 Arduino style to ESP32 with pure C++ and now i am stuck.
My code:

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/uart.h"
#include "driver/gpio.h"
#include "sdkconfig.h"
#include <string.h>

#define ECHO_TEST_TXD (CONFIG_EXAMPLE_UART_TXD)
#define ECHO_TEST_RXD (CONFIG_EXAMPLE_UART_RXD)
#define ECHO_TEST_RTS (UART_PIN_NO_CHANGE)
#define ECHO_TEST_CTS (UART_PIN_NO_CHANGE)

#define ECHO_UART_PORT_NUM      (CONFIG_EXAMPLE_UART_PORT_NUM)
#define ECHO_UART_BAUD_RATE     (CONFIG_EXAMPLE_UART_BAUD_RATE)
#define ECHO_TASK_STACK_SIZE    (CONFIG_EXAMPLE_TASK_STACK_SIZE)

#define BUF_SIZE (1024)

static void echo_task(void *arg)
{
    uart_config_t uart_config = {
        .baud_rate = ECHO_UART_BAUD_RATE,
        .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,
    };
    int intr_alloc_flags = 0;

#if CONFIG_UART_ISR_IN_IRAM
    intr_alloc_flags = ESP_INTR_FLAG_IRAM;
#endif

    ESP_ERROR_CHECK(uart_driver_install(ECHO_UART_PORT_NUM, BUF_SIZE * 2, 0, 0, NULL, intr_alloc_flags));
    ESP_ERROR_CHECK(uart_param_config(ECHO_UART_PORT_NUM, &uart_config));
    ESP_ERROR_CHECK(uart_set_pin(ECHO_UART_PORT_NUM, ECHO_TEST_TXD, ECHO_TEST_RXD, ECHO_TEST_RTS, ECHO_TEST_CTS));
    
    uint8_t *data = (uint8_t *) malloc(BUF_SIZE);
    memset(data, 0, BUF_SIZE);

    while (1) {
        
        int len = uart_read_bytes(ECHO_UART_PORT_NUM, data, BUF_SIZE, 20 / portTICK_RATE_MS);
        if(len > 0)
        {
        	for(unsigned int i = 0; i < len; i++)
        	{
        		printf("%d", data[i]);
        	}
        	printf("\n");
        }
    }
}

void app_main(void)
{
    xTaskCreate(echo_task, "uart_echo_task", ECHO_TASK_STACK_SIZE, NULL, 10, NULL);
}
And when I send from another terminal some like "test" or "12345", I see same in ESP terminal.
But when i send:

Code: Select all

char[] txBuffer = { '\n', '0', '5', '8', '4', '\r' };
I see:

Code: Select all

00000000000000000
I've tried ESP_LOG_BUFFER_HEX, same effect. What I do wrong?
P.S. txBuffer simulate another device, who send temperature value

vilkoivshchi
Posts: 2
Joined: Wed Apr 13, 2022 8:19 am

Re: UART_NUM_2 printing characters to serial monitor not working

Postby vilkoivshchi » Wed Apr 13, 2022 3:09 pm

omg, I've found error. Different baudrate of ESP and remote terminal :facepalm:

Who is online

Users browsing this forum: Baidu [Spider], Bing [Bot], Google [Bot] and 136 guests