0
0
Fork 0

fix(uart): Parse baudrate and does not send

This commit is contained in:
windowsair 2022-01-20 11:51:09 +08:00
parent 89ddfc0603
commit a321da8fb6
2 changed files with 25 additions and 2 deletions

View File

@ -266,6 +266,10 @@ Send data -> TCP -> Uart TX -> external devices
Recv data <- TCP <- Uart Rx <- 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. 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.

View File

@ -87,6 +87,23 @@ static void uart_bridge_reset() {
is_conn_valid = false; 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 * 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); memcpy(tmp_buff, buffer, len_buf);
tmp_buff[len_buf] = '\0'; tmp_buff[len_buf] = '\0';
int baudrate = atoi(tmp_buff); int baudrate = atoi(tmp_buff);
if (baudrate > 0 && baudrate < 2000000) { if (baudrate > 0 && baudrate < 2000000 && num_digits(baudrate) == len_buf) {
ESP_LOGI(UART_TAG, "change baud:%d\r\n", baudrate); ESP_LOGI(UART_TAG, "change baud:%d", baudrate);
uart_set_baudrate(UART_NUM_0, baudrate); uart_set_baudrate(UART_NUM_0, baudrate);
uart_set_baudrate(UART_NUM_1, baudrate); uart_set_baudrate(UART_NUM_1, baudrate);
is_first_time_recv = false;
continue;
} }
} }
is_first_time_recv = false; is_first_time_recv = false;