• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

ESP32 PlatformIO Serial port issue

Q&A Forum › Category: ESP32 › ESP32 PlatformIO Serial port issue
0 Vote Up Vote Down
David Huisman asked 4 years ago

Hi,
I am wanting to implement a Uart in a ESP32 Arduino project using platformIO. The problem is that I want to use a timeout to detect end of received packet but the “uart.h” library in PlatformIO does not support “uart_event.timeout_flag”.
My code is below. Can you point to different version os esp-idf to use in the project? I am wondering if the version is old and that is why it is not supported? The project I copied the uart code from works fine and was created in VSCODE using esp-idf v4.3

void uart_event_task(void *params)
{
    uart_event_t uart_event;

    while(1)
    {
        if(xQueueReceive(uart_queue,&uart_event,portMAX_DELAY))
        {
            switch(uart_event.type)
            {
               
                case
                  UART_DATA:
                    if(uart_event.timeout_flag)
                    {
                      uart_read_bytes(rs485_uart, rx_buffer, uart_event.size,UART_BREAK_TIME);
                      bytesReceived = uart_event.size;
                      xSemaphoreGive(received_uart_data);
                    }
                break;
                case UART_FIFO_OVF:
                    ESP_LOGI(TAG, “hw fifo overflow”);
                    uart_flush_input(rs485_uart);
                    xQueueReset(uart_queue);
                    break;
                //Event of UART ring buffer full
                case UART_BUFFER_FULL:
                    ESP_LOGI(TAG, “ring buffer full”);
                    uart_flush_input(rs485_uart);
                    xQueueReset(uart_queue);
                    break;
               default:
                break;
            }
        }
    }
}

void init_rs485_uart(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,
    };
    ESP_ERROR_CHECK(uart_driver_install(rs485_uart, RX_BUFFER_SIZE,0,UART_QUEUE_SIZE,&uart_queue,0));
    uart_param_config(rs485_uart, &uart_config);
    uart_set_pin(rs485_uart, RS485_TXD_PIN, RS485_RXD_PIN, RS485_EN_PIN, UART_PIN_NO_CHANGE);
    ESP_ERROR_CHECK(uart_set_mode(rs485_uart, UART_MODE_RS485_HALF_DUPLEX));
    uart_set_rx_timeout(rs485_uart,UART_BREAK_TIME);
    //uart_set_always_rx_timeout(rs485_uart,true);
    xTaskCreate(uart_event_task,”uart event task”,2048,NULL,20,NULL);
}

int send_rs845_uart_data(char *command,int numbytes)
{
    //uart_clear_intr_status(rs485_uart, UART_TX_DONE_INT_CLR_M);
    if(uart_write_bytes(rs485_uart, command, numbytes) != numbytes)
    {
        ESP_LOGI(TAG,”Critical Send Data Error”);
    }
    ESP_ERROR_CHECK(uart_wait_tx_done(rs485_uart,50));
    return true;
}

int receive_rs485_uart_data(char* response, uint32_t max_length, int wait_ms)
{
    return uart_read_bytes(rs485_uart, (uint8_t *)response, max_length, pdMS_TO_TICKS(wait_ms));
}

void uart_init(void)
{
  gpio_pad_select_gpio(RS485_EN_PIN);
  gpio_set_direction(RS485_EN_PIN,GPIO_MODE_OUTPUT);
  init_rs485_uart();
}

 

Question Tags: arduio platformio vscode
5 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
I’m not familiar with programming using ESP IDF.
But I guess you would have to install that version of ESP IDF in VS Code.
Maybe this video can help: https://www.youtube.com/watch?v=Lc6ausiKvQM
Regards,
Sara

0 Vote Up Vote Down
David Huisman answered 4 years ago

Thanks Sara,

I had a play around and found it is actually easy to use another serial port using Serial2…

Now I just ned to clean up the code and see if I can move the html and css back into SPIFFS and get other code into separate modules. BTW: If I have standard “C” code in my app, that is running ok and I want to move it, do I just create a new “cpp” file and copy the code into it and a related “h” file to provide external reference to the functions?

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Hi.
I don’t know exactly how to do that in VS Code.
I think that is not as trivial as it seems.
I read a discussion about that and found the following answer that might help:

  • https://github.com/microsoft/vscode-arduino/issues/271#issuecomment-727564776

Regards,
Sara

0 Vote Up Vote Down
David Huisman answered 4 years ago

Sara,
I managed to implment the built-in Arduino Serial2 and it works fine.

0 Vote Up Vote Down
Sara Santos Staff answered 4 years ago

Great!
I’ll mark this issue as resolved.
If you need further help, you just need to open a new question in our forum.
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [New Edition] Build ESP32-CAM Projects eBook – 2nd Edition April 16, 2025
  • [eBook Updated] Learn ESP32 with Arduino IDE eBook – Version 3.2 April 16, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.