{"id":610,"date":"2026-08-14T16:57:26","date_gmt":"2026-08-14T14:57:26","guid":{"rendered":"https:\/\/www.elrebo.de\/?p=610"},"modified":"2026-08-14T19:28:51","modified_gmt":"2026-08-14T17:28:51","slug":"generic_uart","status":"publish","type":"post","link":"https:\/\/www.elrebo.de\/index.php\/2026\/08\/14\/generic_uart\/","title":{"rendered":"generic_uart"},"content":{"rendered":"\n<p>An ESP-IDF component for <a href=\"https:\/\/elrebo.de\/index.php\/2026\/02\/20\/physical-computing\/\">Physical Computing<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-93b9e92e-855c-49e7-afc9-5d6ceb5dd196\">Purpose<\/h2>\n\n\n\n<p id=\"block-c64d92dd-11a2-4f24-b95e-9771a04b768e\">Generic UART functionality for ESP32 systems.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-ceb174ba-c9e2-4c81-b23c-28ec9ab219b3\">How it is done<\/h2>\n\n\n\n<p>To use a UART to communicate with another device, you have to<\/p>\n\n\n\n<ul id=\"block-4e10bc17-3bf7-4dbc-89a1-565b13e7c661\" class=\"wp-block-list\">\n<li>create an instance of class GenericUart.<\/li>\n<\/ul>\n\n\n\n<p>In the class constructor you state<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>what UART port you want to use,<\/li>\n\n\n\n<li>the structure with configuration parameters like baud rate, nr of stop bits etc.,<\/li>\n\n\n\n<li>the GPIO pin for transmitting signals (TX) and<\/li>\n\n\n\n<li>the GPIO pin for receiving signals (RX).<\/li>\n<\/ul>\n\n\n\n<p>Currently there are only two methods implemented:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>uart_port_t getUartNum();\nstd::string readString(int timeoutMs);<\/code><\/pre>\n\n\n\n<p id=\"block-d8c0bf53-1cf7-4138-99e8-dd37495d26d0\">Method getUartNum() returns the UART port used in the constructor. It can be used to use low-level transmit and receive functions directly.<\/p>\n\n\n\n<p>Method readString() receives all available bytes from the UART. The timeout for the receive operation must be specified.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-c829874a-6a6e-4ddd-b99a-643e26e3a15f\">Where to find it<\/h2>\n\n\n\n<p id=\"block-21f599b5-3623-49e1-a03e-557cf0f612c2\"><code>generic_uart<\/code> is published on the ESP Registry at <a href=\"https:\/\/components.espressif.com\/components\/elrebo-de\/generic_uart\">https:\/\/components.espressif.com\/components\/elrebo-de\/generic_uart<\/a>.<\/p>\n\n\n\n<p id=\"block-07ba1f89-462e-4449-9ed7-10bc6e4a29e3\">The source code can be found at <a href=\"https:\/\/github.com\/elrebo-de\/esp_generic_uart\">https:\/\/github.com\/elrebo-de\/esp_generic_uart<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"block-d4d64ecc-95e0-4193-8617-0588f2a771d5\">How to use it<\/h2>\n\n\n\n<p id=\"block-9db97be3-b411-47c5-a19b-0c2ba6e8abf4\">As an ESP-IDF component <code>generic_uart<\/code> must be included into idf_component.yml as a dependency. As usual this can be done by executing this command:<\/p>\n\n\n\n<p id=\"block-5e1d9cc0-0f9f-4632-9fb7-2d6c60327d63\"><code>idf.py add-dependency \"elrebo-de\/generic_uart^1.0.2\"<\/code><\/p>\n\n\n\n<p id=\"block-a39c2484-ecc0-4a3f-87fe-6f97558f4404\">Now you can include <code>generic_uart.hpp<\/code> and use the GenericUart class in your program.<\/p>\n\n\n\n<p>The first parameter to the constructor is the tag used for logging as a std::string.<\/p>\n\n\n\n<p>The second parameter to the constructor is the UART port number as a uart_port_t value.<\/p>\n\n\n\n<p>The third parameter is the address of a UART configuration structure. The structure uart_config is of type uart_config_t and defines these items:<\/p>\n\n\n\n<pre id=\"block-c7258a69-3ba7-4d84-819f-1c54a1be0a88\" class=\"wp-block-code\"><code>    uart_config_t uart_config = {\n        .baud_rate = 115200,\n        .data_bits = UART_DATA_8_BITS,\n        .parity = UART_PARITY_DISABLE,\n        .stop_bits = UART_STOP_BITS_1,\n        .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,\n        .rx_flow_ctrl_thresh = 0,\n        .source_clk = UART_SCLK_DEFAULT,\n        .flags = {\n           .allow_pd = 0,\n           .backup_before_sleep = 0,\n        },\n    };\n<\/code><\/pre>\n\n\n\n<p id=\"block-f5c0d1f8-d44d-4f4e-8462-7665a7e766f7\">Here the UART to receive data from an LC76G GNSS Module is defined:<\/p>\n\n\n\n<pre id=\"block-6c0ff5a0-ce22-4867-be6a-f02f3119f871\" class=\"wp-block-code\"><code>\/\/ set UART configuration\nconst uart_port_t uart_num = UART_NUM_2;\n\nuart_config_t uart_config = {\n    .baud_rate = 115200,\n    .data_bits = UART_DATA_8_BITS,\n    .parity = UART_PARITY_DISABLE,\n    .stop_bits = UART_STOP_BITS_1,\n    .flow_ctrl = UART_HW_FLOWCTRL_DISABLE,\n    .rx_flow_ctrl_thresh = 0,\n    .source_clk = UART_SCLK_DEFAULT,\n    .flags = {\n        .allow_pd = 0,\n        .backup_before_sleep = 0,\n    },\n};\n\/\/ we do not use the TX pin, because we are only reading from RX\n\n\/\/ M5STACK CORE2 V1.1\nconst int uart_tx_pin = UART_PIN_NO_CHANGE; \/\/ pin 32 used for LC76G PPS signal\nconst int uart_rx_pin = 33;\n\ngnssUart = new GenericUart(\n                 \"gnssUart\",\n                 uart_num,\n                 &amp;uart_config,\n                 uart_tx_pin,\n                 uart_rx_pin\n               );<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An ESP-IDF component for Physical Computing Purpose Generic UART functionality for ESP32 systems. How it is done To use a UART to communicate with another device, you have to In the class constructor you state Currently there are only two methods implemented: Method getUartNum() returns the UART port used in the constructor. It can be [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":613,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,1,16],"tags":[12],"class_list":["post-610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32_socs","category-oss","category-physical_computing","tag-esp-idf"],"_links":{"self":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts\/610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/comments?post=610"}],"version-history":[{"count":2,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts\/610\/revisions"}],"predecessor-version":[{"id":615,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/posts\/610\/revisions\/615"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/media\/613"}],"wp:attachment":[{"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/media?parent=610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/categories?post=610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.elrebo.de\/index.php\/wp-json\/wp\/v2\/tags?post=610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}