{"id":48872,"date":"2025-07-30T13:46:49","date_gmt":"2025-07-30T05:46:49","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/48872.html"},"modified":"2025-07-30T13:46:49","modified_gmt":"2025-07-30T05:46:49","slug":"esp32%e8%bf%9e%e6%8e%a5%e6%9c%8d%e5%8a%a1%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/48872.html","title":{"rendered":"ESP32\u8fde\u63a5\u670d\u52a1\u5668"},"content":{"rendered":"<p>\u6e29\u99a8\u63d0\u793a&#xff1a;\u5982\u679c\u4e0d\u60f3\u4e86\u89e3\u8fc7\u7a0b&#xff0c;\u76f4\u63a5\u4ece\u7b2c\u56db\u6b65\u5f00\u59cb\u4e5f\u80fd\u6210\u529f<\/p>\n<h2>\u4e00\u3001ESP32-\u300bPC&#xff08;\u4e32\u53e3\u8c03\u8bd5\u52a9\u624b&#xff09;&#xff1a;\u6570\u636e\u4e92\u53d1\u00a0<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 1\u3001\u6253\u5f00\u4e32\u53e3\u8c03\u8bd5\u52a9\u624b<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 2\u3001\u63a5\u4e0aesp32\u548cUSB\u8f6c\u4e32\u53e3&#xff08;\u4e24\u6839\u6570\u636e\u7ebf&#xff0c;TX\u63a5GPIO16&#xff0c;RX\u63a5GPIO17&#xff0c;GND\u63a5GND&#xff0c;\u5269\u4e0b\u4e00\u6839\u7ebf\u4e0d\u7528\u7ba1&#xff09;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 3\u3001\u5728\u8bbe\u5907\u7ba1\u7406\u5668\u4e2d\u67e5\u770b\u4e32\u53e3\u53f7&#xff0c;\u6ce2\u7279\u7387\u8bbe\u4e3a115200<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 4\u3001\u63a5\u53d7\u6a21\u5f0f\u548c\u53d1\u9001\u6a21\u5f0f\u8981\u4e00\u81f4&#xff08;\u6587\u672c\u6a21\u5f0f&#xff0c;UTF-8)<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 5\u3001\u7f16\u8bd1\u3001\u70e7\u5f55\u4e0b\u9762\u7684\u4ee3\u7801<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 6\u3001\u6253\u5f00\u76d1\u89c6\u8bbe\u5907&#xff0c;\u5728\u4e32\u53e3\u8c03\u8bd5\u52a9\u624b\u8fdb\u884c\u53d1\u9001\u6570\u636e&#xff0c;\u4e32\u53e3\u8c03\u8bd5\u52a9\u624b\u548c\u76d1\u89c6\u8bbe\u5907\u90fd\u4f1a\u6536\u5230\u6570\u636e<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 7\u3001\u5982\u679c\u63a5\u53e3\u51fa\u73b0\u95ee\u9898&#xff0c;\u51fa\u73b0\u4e24\u4e2a\u76f8\u540c\u7684\u4e32\u53e3\u53f7\u5c31\u5378\u8f7d\u91cd\u88c5<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 8\u3001\u5982\u679c\u51fa\u73b0\u4e71\u7801&#xff0c;\u5c06\u4e32\u53e3\u8c03\u8bd5\u52a9\u624b\u548cvscode\u7684\u6ce2\u7279\u7387\u8bbe\u4e3a4800&#xff0c;\u5982\u679c\u8fd8\u662f\u4e0d\u884c&#xff0c;\u518d\u6539\u56de\u6765&#xff08;115200&#xff09;<\/p>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &#034;freertos\/FreeRTOS.h&#034;<br \/>\n#include &#034;freertos\/task.h&#034;<br \/>\n#include &#034;driver\/uart.h&#034;<br \/>\n#include &#034;esp_log.h&#034;<\/p>\n<p>#define UART_NUM UART_NUM_1  \/\/ \u4f7f\u7528 UART1 \u4f5c\u4e3a\u989d\u5916\u7684\u901a\u4fe1\u4e32\u53e3<br \/>\n#define TXD_PIN 17           \/\/ UART1 TX \u5f15\u811a<br \/>\n#define RXD_PIN 16           \/\/ UART1 RX \u5f15\u811a<br \/>\n#define UART_RX_BUFFER_SIZE 1024<\/p>\n<p>static const char *TAG &#061; &#034;UART Communication&#034;;<\/p>\n<p>\/\/ UART \u914d\u7f6e\u51fd\u6570<br \/>\nvoid uart_init()<br \/>\n{<br \/>\n    const uart_config_t uart_config &#061; {<br \/>\n        .baud_rate &#061; 115200,  \/\/ \u6ce2\u7279\u7387<br \/>\n        .data_bits &#061; UART_DATA_8_BITS,  \/\/ \u6570\u636e\u4f4d<br \/>\n        .parity &#061; UART_PARITY_DISABLE,  \/\/ \u65e0\u5947\u5076\u6821\u9a8c<br \/>\n        .stop_bits &#061; UART_STOP_BITS_1,  \/\/ \u505c\u6b62\u4f4d<br \/>\n        .flow_ctrl &#061; UART_HW_FLOWCTRL_DISABLE,  \/\/ \u7981\u7528\u786c\u4ef6\u6d41\u63a7<br \/>\n        .source_clk &#061; UART_SCLK_APB,  \/\/ \u4f7f\u7528 APB \u65f6\u949f<br \/>\n    };<\/p>\n<p>    \/\/ \u914d\u7f6e UART \u53c2\u6570<br \/>\n    ESP_ERROR_CHECK(uart_driver_install(UART_NUM, UART_RX_BUFFER_SIZE, 0, 0, NULL, 0));<br \/>\n    ESP_ERROR_CHECK(uart_param_config(UART_NUM, &amp;uart_config));<br \/>\n    ESP_ERROR_CHECK(uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE));<br \/>\n    ESP_LOGI(TAG, &#034;UART initialized successfully&#034;);<br \/>\n}<\/p>\n<p>\/\/ UART \u901a\u4fe1\u4efb\u52a1<br \/>\nvoid uart_communication_task(void *param)<br \/>\n{<br \/>\n    uint8_t *uart_rx_buffer &#061; (uint8_t *)malloc(UART_RX_BUFFER_SIZE);<br \/>\n    if (uart_rx_buffer &#061;&#061; NULL)<br \/>\n    {<br \/>\n        ESP_LOGE(TAG, &#034;Failed to allocate memory for UART RX buffer&#034;);<br \/>\n        vTaskDelete(NULL);<br \/>\n        return;<br \/>\n    }<\/p>\n<p>    while (1)<br \/>\n    {<br \/>\n        int length &#061; uart_read_bytes(UART_NUM, uart_rx_buffer, UART_RX_BUFFER_SIZE, pdMS_TO_TICKS(100));<br \/>\n        if (length &gt; 0)<br \/>\n        {<br \/>\n            ESP_LOGI(TAG, &#034;Received %d bytes: %s&#034;, length, uart_rx_buffer);<br \/>\n            \/\/ \u56de\u4f20\u63a5\u6536\u5230\u7684\u6570\u636e<br \/>\n            uart_write_bytes(UART_NUM, uart_rx_buffer, length);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    free(uart_rx_buffer);<br \/>\n    vTaskDelete(NULL);<br \/>\n}<\/p>\n<p>void app_main()<br \/>\n{<br \/>\n    \/\/ \u521d\u59cb\u5316 UART<br \/>\n    uart_init();<\/p>\n<p>    \/\/ \u521b\u5efa UART \u901a\u4fe1\u4efb\u52a1<br \/>\n    xTaskCreate(uart_communication_task, &#034;uart_communication_task&#034;, 2048, NULL, 5, NULL);<\/p>\n<p>    \/\/ \u4fdd\u7559\u8c03\u8bd5\u4e32\u53e3\u8f93\u51fa<br \/>\n    while (1)<br \/>\n    {<br \/>\n        printf(&#034;Debug message from UART0\\\\n&#034;);<br \/>\n        vTaskDelay(pdMS_TO_TICKS(5000));<br \/>\n    }<br \/>\n}<\/p>\n<h2>\u4e8c\u3001ESP32-\u300bPC&#xff08;\u81ea\u5df1\u5199python\u811a\u672c&#xff09;&#xff1a;\u6570\u636e\u4e92\u53d1<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a01\u3001\u63a5\u4e0aesp32\u548cUSB\u8f6c\u4e32\u53e3&#xff08;\u4e24\u6839\u6570\u636e\u7ebf&#xff0c;TX\u63a5GPIO16&#xff0c;RX\u63a5GPIO17&#xff0c;GND\u63a5GND&#xff0c;\u5269\u4e0b\u4e00\u6839\u7ebf\u4e0d\u7528\u7ba1&#xff09;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a02\u3001\u786e\u4fdd\u81ea\u5df1\u5df2\u7ecf\u5b89\u88c5\u4e86python\u7a0b\u5e8f<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u53ef\u5728\u7ec8\u7aef\u4f7f\u7528\u547d\u4ee4&#xff1a;python &#8211;version<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u5982\u679c\u51fa\u73b0\u7248\u672c\u53f7&#xff0c;\u5219\u5df2\u5b89\u88c5&#xff0c;\u4f8b\u5982&#xff1a;Python 3.9.1<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a03\u3001\u5728\u7ec8\u7aef\u8fd0\u884cserial_echo.py\u6587\u4ef6&#xff08;\u4e0b\u9762\u7684\u4ee3\u7801&#xff0c;\u4e00\u5b9a\u8981\u8f6c\u4e3apython\u6587\u4ef6&#xff09;&#xff08;\u5728\u7ec8\u7aef\u8f93\u5165\u547d\u4ee4&#xff1a;python E:\\\\server.py(\u5730\u5740&#xff09;&#xff09;&#xff0c;\u5728vscode\u91cc\u9762\u7f16\u8bd1\u70e7\u5f55\u4e00\u4e2a\u6709\u8f93\u51fa\u7684\u4ee3\u7801<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u8f6cpython\u6587\u4ef6\u65b9\u6cd5&#xff1a;\u666e\u901a\u8bb0\u4e8b\u672c&#xff08;\u5728\u4fdd\u5b58\u65f6\u9009\u62e9\u5168\u90e8\u6587\u4ef6&#xff0c;\u6587\u4ef6\u540d\u52a0\u4e2a\u540e\u7f00.py)<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a04\u3001\u82e5\u7ec8\u7aef\u51fa\u73b0\u8f93\u51fa\u7ed3\u679c&#xff0c;\u5219\u8868\u793a\u6210\u529f<\/p>\n<p>serial_echo.py\u6587\u4ef6&#xff1a;<\/p>\n<p>import serial<br \/>\nimport time<\/p>\n<p>def serial_echo(port, baudrate&#061;115200):<br \/>\n\u00a0 \u00a0 &#034;&#034;&#034;<br \/>\n\u00a0 \u00a0 \u6253\u5f00\u6307\u5b9a\u7684\u4e32\u53e3&#xff0c;\u5e76\u5b9e\u73b0\u56de\u663e\u529f\u80fd\u3002<br \/>\n\u00a0 \u00a0 :param port: \u4e32\u53e3\u53f7&#xff0c;\u4f8b\u5982 &#039;COM3&#039;<br \/>\n\u00a0 \u00a0 :param baudrate: \u6ce2\u7279\u7387&#xff0c;\u9ed8\u8ba4\u4e3a 115200<br \/>\n\u00a0 \u00a0 &#034;&#034;&#034;<br \/>\n\u00a0 \u00a0 try:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 # \u6253\u5f00\u4e32\u53e3<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 ser &#061; serial.Serial(port, baudrate, timeout&#061;1)<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 print(f&#034;\u4e32\u53e3 {port} \u5df2\u6253\u5f00&#xff0c;\u6ce2\u7279\u7387 {baudrate}&#034;)<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 print(&#034;\u5f00\u59cb\u56de\u663e\u529f\u80fd&#xff0c;\u6309 Ctrl&#043;C \u9000\u51fa&#034;)<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0\u00a0<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 while True:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 if ser.in_waiting:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # \u8bfb\u53d6\u4e32\u53e3\u6570\u636e<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 data &#061; ser.readline().decode(&#039;utf-8&#039;).strip()<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 print(f&#034;\u63a5\u6536\u5230\u6570\u636e: {data}&#034;)<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # \u53d1\u9001\u76f8\u540c\u7684\u6570\u636e\u56de\u4e32\u53e3<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ser.write(data.encode(&#039;utf-8&#039;))<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0<br \/>\n\u00a0 \u00a0 except KeyboardInterrupt:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 print(&#034;\\\\n\u7528\u6237\u4e2d\u65ad&#034;)<br \/>\n\u00a0 \u00a0 except serial.SerialException as e:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 print(f&#034;\u4e32\u53e3\u9519\u8bef: {e}&#034;)<br \/>\n\u00a0 \u00a0 finally:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 # \u5173\u95ed\u4e32\u53e3<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 if &#039;ser&#039; in locals():<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 ser.close()<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 print(&#034;\u4e32\u53e3\u5df2\u5173\u95ed&#034;)<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n\u00a0 \u00a0 import sys<br \/>\n\u00a0 \u00a0 if len(sys.argv) &gt; 1:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 port &#061; sys.argv[1]<br \/>\n\u00a0 \u00a0 else:<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 port &#061; &#039;COM3&#039; \u00a0# \u9ed8\u8ba4\u4e32\u53e3\u53f7&#xff0c;\u6839\u636e\u9700\u8981\u4fee\u6539<br \/>\n\u00a0 \u00a0\u00a0<br \/>\n\u00a0 \u00a0 serial_echo(port)<\/p>\n<h2>\u4e09\u3001PC&#xff08;\u81ea\u5df1\u5199python\u811a\u672c&#xff09;\u2014\u300b\u670d\u52a1\u5668&#xff1a;\u6570\u636e\u4e92\u53d1<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 1\u3001\u521b\u5efa\u548c\u4fdd\u5b58 Python \u811a\u672c\u6587\u4ef6<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u751f\u6210\u4e00\u4e2a\u540e\u7f00\u4e3a.py\u7684python\u6587\u4ef6&#xff0c;\u670d\u52a1\u5668\u7aef\u4ee3\u7801\u4fdd\u5b58\u4e3a server.py\u3002<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u5ba2\u6237\u7aef\u4ee3\u7801\u4fdd\u5b58\u4e3a client.py\u3002<\/p>\n<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 2\u3001\u5728\u7ec8\u7aef\u8fd0\u884cserver.py\u548cclient.py&#xff08;\u5206\u522b\u8f93\u5165\u547d\u4ee4&#xff1a;python E:\\\\server.py(\u5730\u5740&#xff09;\u548cpython E:\\\\client.py)<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 3\u3001\u670d\u52a1\u5668\u542f\u52a8\u540e&#xff0c;\u4f60\u4f1a\u770b\u5230\u7c7b\u4f3c\u4ee5\u4e0b\u7684\u8f93\u51fa&#xff1a;<br \/>\n\u670d\u52a1\u5668\u5df2\u542f\u52a8&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#8230;<\/p>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 4\u3001\u4e4b\u540e\u5c31\u53ef\u4ee5\u8fdb\u884c\u6570\u636e\u4e92\u53d1<\/p>\n<p>\nserver.py\u6587\u4ef6&#xff1a;<\/p>\n<p>import socket<br \/>\nimport threading<\/p>\n<p>def handle_client(client_socket, client_address):<br \/>\n    print(f&#034;\u5ba2\u6237\u7aef {client_address} \u5df2\u8fde\u63a5\u3002&#034;)<br \/>\n    try:<br \/>\n        while True:<br \/>\n            data &#061; client_socket.recv(1024)<br \/>\n            if not data:<br \/>\n                print(f&#034;\u5ba2\u6237\u7aef {client_address} \u5df2\u65ad\u5f00\u8fde\u63a5\u3002&#034;)<br \/>\n                break<br \/>\n            print(f&#034;\u6765\u81ea {client_address} \u7684\u6d88\u606f: {data.decode(&#039;utf-8&#039;)}&#034;)<br \/>\n            response &#061; input(&#034;\u8bf7\u8f93\u5165\u56de\u590d\u5185\u5bb9: &#034;)<br \/>\n            client_socket.send(response.encode(&#039;utf-8&#039;))<br \/>\n    finally:<br \/>\n        client_socket.close()<\/p>\n<p>def start_server():<br \/>\n    server &#061; socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\n    server.bind((&#039;0.0.0.0&#039;, 12345))  # \u7ed1\u5b9a\u5230\u6240\u6709\u53ef\u7528\u7684\u7f51\u7edc\u63a5\u53e3&#xff0c;\u7aef\u53e3\u4e3a 12345<br \/>\n    server.listen(5)<br \/>\n    print(&#034;\u670d\u52a1\u5668\u5df2\u542f\u52a8&#xff0c;\u7b49\u5f85\u5ba2\u6237\u7aef\u8fde\u63a5&#8230;&#034;)<\/p>\n<p>    while True:<br \/>\n        client_socket, client_address &#061; server.accept()<br \/>\n        client_handler &#061; threading.Thread(target&#061;handle_client, args&#061;(client_socket, client_address))<br \/>\n        client_handler.start()<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    start_server()<\/p>\n<p>client.py\u6587\u4ef6&#xff1a;<\/p>\n<p>import socket<\/p>\n<p>def start_client():<br \/>\n    client &#061; socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\n    server_address &#061; (&#039;127.0.0.1&#039;, 12345)  # \u670d\u52a1\u5668\u7684 IP \u5730\u5740\u548c\u7aef\u53e3<br \/>\n    client.connect(server_address)<br \/>\n    print(&#034;\u5df2\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3002&#034;)<\/p>\n<p>    try:<br \/>\n        while True:<br \/>\n            message &#061; input(&#034;\u8bf7\u8f93\u5165\u8981\u53d1\u9001\u7684\u6d88\u606f: &#034;)<br \/>\n            client.send(message.encode(&#039;utf-8&#039;))<br \/>\n            response &#061; client.recv(1024)<br \/>\n            print(f&#034;\u670d\u52a1\u5668\u56de\u590d: {response.decode(&#039;utf-8&#039;)}&#034;)<br \/>\n    finally:<br \/>\n        client.close()<br \/>\n        print(&#034;\u8fde\u63a5\u5df2\u5173\u95ed\u3002&#034;)<\/p>\n<p>if __name__ &#061;&#061; &#039;__main__&#039;:<br \/>\n    start_client()<\/p>\n<h2>\u56db\u3001ESP32-\u300b\u670d\u52a1\u5668&#xff1a;\u5c06\u524d\u9762\u7684\u6b65\u9aa4\u6574\u5408\u5728\u4e00\u8d77<\/h2>\n<p>\u00a0 \u00a0 \u00a0 \u00a0 1\u3001\u8fde\u63a5\u597dESP32\u5f00\u53d1\u677f&#xff0c;\u70e7\u5f55\u4e00\u4e2a\u6709\u8f93\u51fa\u7684\u4ee3\u7801<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 2\u3001\u5728\u7ec8\u7aef\u8fd0\u884cserver.py&#xff08;\u8ddf\u4e0a\u9762\u7684server\u65f6\u4e00\u6837\u7684&#xff08;\u8f93\u5165python E:\\\\server.py&#xff08;\u5730\u5740&#xff09;&#xff09;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 3\u3001\u5728\u7ec8\u7aef\u8fd0\u884cclient2.py &#xff08;\u8f93\u5165python E:\\\\client2.py&#xff08;\u5730\u5740&#xff09;&#xff09;<br \/>\n\u00a0 \u00a0 \u00a0 \u00a0 4\u3001\u5373\u53ef\u8fdb\u884c\u8f93\u5165\u4e92\u53d1&#xff0c;\u5f53ESP32\u53d1\u9001\u4e00\u4e2a\u6d88\u606f\u65f6&#xff0c;\u670d\u52a1\u5668\u63a5\u6536\u5230\u5e76\u4e14\u8fd4\u56de\u4e00\u4e2a\u6d88\u606f\u7684\u65f6\u5019ESP32\u624d\u80fd\u53d1\u9001\u4e0b\u4e00\u4e2a\u6d88\u606f<\/p>\n<p>client2.py\u6587\u4ef6&#xff1a;<\/p>\n<p>import serial<br \/>\nimport socket<br \/>\nimport threading<\/p>\n<p>def serial_echo(port, baudrate&#061;115200, server_ip&#061;&#039;127.0.0.1&#039;, server_port&#061;12345):<br \/>\n    try:<br \/>\n        ser &#061; serial.Serial(port, baudrate, timeout&#061;1)<br \/>\n        print(f&#034;\u4e32\u53e3 {port} \u5df2\u6253\u5f00&#xff0c;\u6ce2\u7279\u7387&#xff1a;{baudrate}&#034;)<br \/>\n        print(&#034;\u5f00\u59cb\u6570\u636e\u8f6c\u53d1\u529f\u80fd\u3002\u6309 Ctrl&#043;C \u9000\u51fa&#034;)<\/p>\n<p>        client &#061; socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\n        client.connect((server_ip, server_port))<br \/>\n        print(&#034;\u5df2\u8fde\u63a5\u5230\u670d\u52a1\u5668\u3002&#034;)<\/p>\n<p>        while True:<br \/>\n            if ser.in_waiting:<br \/>\n                data &#061; ser.readline().decode(&#039;utf-8&#039;).strip()<br \/>\n                print(f&#034;\u63a5\u6536\u5230\u6570\u636e&#xff1a;{data}&#034;)<br \/>\n                client.send(data.encode(&#039;utf-8&#039;))  # \u53d1\u9001\u6570\u636e\u5230\u670d\u52a1\u5668<br \/>\n                response &#061; client.recv(1024)<br \/>\n                print(f&#034;\u670d\u52a1\u5668\u54cd\u5e94&#xff1a;{response.decode(&#039;utf-8&#039;)}&#034;)<br \/>\n                ser.write(data.encode(&#039;utf-8&#039;))  # \u53d1\u9001\u76f8\u540c\u7684\u6570\u636e\u56de\u4e32\u53e3<\/p>\n<p>    except serial.SerialException as e:<br \/>\n        print(f&#034;\u4e32\u53e3\u9519\u8bef&#xff1a;{e}&#034;)<br \/>\n    except ConnectionRefusedError:<br \/>\n        print(&#034;\u8fde\u63a5\u88ab\u62d2\u7edd&#xff0c;\u8bf7\u68c0\u67e5\u670d\u52a1\u5668\u5730\u5740\u548c\u7aef\u53e3\u53f7\u662f\u5426\u6b63\u786e&#xff01;&#034;)<br \/>\n    except Exception as e:<br \/>\n        print(f&#034;\u53d1\u751f\u9519\u8bef&#xff1a;{e}&#034;)<br \/>\n    finally:<br \/>\n        if &#039;ser&#039; in locals():<br \/>\n            ser.close()<br \/>\n            print(&#034;\u4e32\u53e3\u5df2\u5173\u95ed&#034;)<br \/>\n        client.close()<br \/>\n        print(&#034;\u8fde\u63a5\u5df2\u5173\u95ed\u3002&#034;)<\/p>\n<p>if __name__ &#061;&#061; &#034;__main__&#034;:<br \/>\n    import sys<br \/>\n    if len(sys.argv) &gt; 1:<br \/>\n        port &#061; sys.argv[1]<br \/>\n    else:<br \/>\n        port &#061; &#039;COM3&#039;  # \u9ed8\u8ba4\u4e32\u53e3\u53f7<\/p>\n<p>    serial_thread &#061; threading.Thread(target&#061;serial_echo, args&#061;(port,))<br \/>\n    serial_thread.start()<\/p>\n<p>    # \u7b49\u5f85\u7ebf\u7a0b\u7ed3\u675f<br \/>\n    serial_thread.join()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e27\u6b21\uff0c\u6536\u85cf27\u6b21\u3002ESP32\u8fde\u63a5\u670d\u52a1\u5668\uff08\u81ea\u5df1\u5199\u7684\u811a\u672c\uff09_eps32\u600e\u4e48\u8fde\u63a5\u540e\u7aef\u670d\u52a1\u5668<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[4736,43,44],"topic":[],"class_list":["post-48872","post","type-post","status-publish","format-standard","hentry","category-server","tag-esp32","tag-43","tag-44"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>ESP32\u8fde\u63a5\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/48872.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"ESP32\u8fde\u63a5\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e27\u6b21\uff0c\u6536\u85cf27\u6b21\u3002ESP32\u8fde\u63a5\u670d\u52a1\u5668\uff08\u81ea\u5df1\u5199\u7684\u811a\u672c\uff09_eps32\u600e\u4e48\u8fde\u63a5\u540e\u7aef\u670d\u52a1\u5668\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/48872.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-30T05:46:49+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/48872.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/48872.html\",\"name\":\"ESP32\u8fde\u63a5\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-07-30T05:46:49+00:00\",\"dateModified\":\"2025-07-30T05:46:49+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/48872.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/48872.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/48872.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"ESP32\u8fde\u63a5\u670d\u52a1\u5668\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"ESP32\u8fde\u63a5\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/48872.html","og_locale":"zh_CN","og_type":"article","og_title":"ESP32\u8fde\u63a5\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb1.3k\u6b21\uff0c\u70b9\u8d5e27\u6b21\uff0c\u6536\u85cf27\u6b21\u3002ESP32\u8fde\u63a5\u670d\u52a1\u5668\uff08\u81ea\u5df1\u5199\u7684\u811a\u672c\uff09_eps32\u600e\u4e48\u8fde\u63a5\u540e\u7aef\u670d\u52a1\u5668","og_url":"https:\/\/www.wsisp.com\/helps\/48872.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-07-30T05:46:49+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"4 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/48872.html","url":"https:\/\/www.wsisp.com\/helps\/48872.html","name":"ESP32\u8fde\u63a5\u670d\u52a1\u5668 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-07-30T05:46:49+00:00","dateModified":"2025-07-30T05:46:49+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/48872.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/48872.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/48872.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"ESP32\u8fde\u63a5\u670d\u52a1\u5668"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/48872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=48872"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/48872\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=48872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=48872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=48872"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=48872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}