From a321da8fb65b5fb584d8cd7d6fb0a870d124002c Mon Sep 17 00:00:00 2001 From: windowsair Date: Thu, 20 Jan 2022 11:51:09 +0800 Subject: [PATCH] fix(uart): Parse baudrate and does not send --- README.md | 4 ++++ main/uart_bridge.c | 23 +++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 25d8522..97efa3e 100644 --- a/README.md +++ b/README.md @@ -266,6 +266,10 @@ Send data -> TCP -> Uart TX -> external devices Recv data <- TCP <- Uart Rx <- external devices ``` +When the TCP connection is established, bridge will try to resolve the text sent for the first time. When the text is a valid baud rate, bridge will switch to it. +For example, sending the ASCII text `115200` will switch the baud rate to 115200. + + For performance reasons, this feature is not enabled by default. You can can modify [wifi_configuration.h](main/wifi_configuration.h) to turn it on. diff --git a/main/uart_bridge.c b/main/uart_bridge.c index c46a5eb..5ff30b6 100644 --- a/main/uart_bridge.c +++ b/main/uart_bridge.c @@ -87,6 +87,23 @@ static void uart_bridge_reset() { is_conn_valid = false; } +static int num_digits(int n) { + if (n < 10) + return 1; + if (n < 100) + return 2; + if (n < 1000) + return 3; + if (n < 10000) + return 4; + if (n < 100000) + return 5; + if (n < 1000000) + return 6; + + return 7; +} + /* * This function will be call in Lwip in each event on netconn */ @@ -241,10 +258,12 @@ void uart_bridge_task() { memcpy(tmp_buff, buffer, len_buf); tmp_buff[len_buf] = '\0'; int baudrate = atoi(tmp_buff); - if (baudrate > 0 && baudrate < 2000000) { - ESP_LOGI(UART_TAG, "change baud:%d\r\n", baudrate); + if (baudrate > 0 && baudrate < 2000000 && num_digits(baudrate) == len_buf) { + ESP_LOGI(UART_TAG, "change baud:%d", baudrate); uart_set_baudrate(UART_NUM_0, baudrate); uart_set_baudrate(UART_NUM_1, baudrate); + is_first_time_recv = false; + continue; } } is_first_time_recv = false;