{"id":1161251,"date":"2025-01-13T19:14:20","date_gmt":"2025-01-13T11:14:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1161251.html"},"modified":"2025-01-13T19:14:22","modified_gmt":"2025-01-13T11:14:22","slug":"python%e5%a6%82%e4%bd%95%e6%8e%a5%e5%8f%97%e8%93%9d%e7%89%99%e4%b8%b2%e5%8f%a3%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1161251.html","title":{"rendered":"python\u5982\u4f55\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25202552\/1bfcbb3d-af55-40a8-91d2-d9a1aa95132e.webp\" alt=\"python\u5982\u4f55\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e\uff0c<strong>\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pybluez<\/code>\u5e93\u3001<code>pyserial<\/code>\u5e93\u3001<code>socket<\/code>\u5e93<\/strong>\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u4e0e\u84dd\u7259\u8bbe\u5907\u901a\u4fe1\u7684\u80fd\u529b\uff0c\u5e76\u4e14\u53ef\u4ee5\u8f7b\u677e\u5730\u4e0e\u84dd\u7259\u4e32\u53e3\u670d\u52a1\u8fdb\u884c\u4ea4\u4e92\u3002<strong>\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>pybluez<\/code>\u5e93\uff0c\u56e0\u4e3a\u5b83\u662f\u4e13\u95e8\u4e3a\u84dd\u7259\u901a\u4fe1\u8bbe\u8ba1\u7684<\/strong>\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h3>\u5b89\u88c5\u5fc5\u8981\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5\u8fd9\u4e9b\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528<code>pip<\/code>\u6765\u5b89\u88c5\u5b83\u4eec\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pybluez pyserial<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>pybluez<\/code>\u5e93<\/h3>\n<\/p>\n<p><h4>\u4e00\u3001\u67e5\u627e\u84dd\u7259\u8bbe\u5907<\/h4>\n<\/p>\n<p><p>\u5728\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u67e5\u627e\u9644\u8fd1\u7684\u84dd\u7259\u8bbe\u5907\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528<code>pybluez<\/code>\u5e93\u6765\u67e5\u627e\u84dd\u7259\u8bbe\u5907\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bluetooth<\/p>\n<p>def find_bluetooth_devices():<\/p>\n<p>    print(&quot;Searching for devices...&quot;)<\/p>\n<p>    devices = bluetooth.discover_devices(duration=8, lookup_names=True, flush_cache=True, lookup_oui=True)<\/p>\n<p>    for addr, name in devices:<\/p>\n<p>        print(f&quot;Found Bluetooth device {name} with address {addr}&quot;)<\/p>\n<p>find_bluetooth_devices()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e8c\u3001\u8fde\u63a5\u5230\u84dd\u7259\u8bbe\u5907<\/h4>\n<\/p>\n<p><p>\u4e00\u65e6\u627e\u5230\u8bbe\u5907\uff0c\u5c31\u53ef\u4ee5\u8fde\u63a5\u5230\u5b83\u3002\u4ee5\u4e0b\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u8fde\u63a5\u5230\u84dd\u7259\u8bbe\u5907\u5e76\u63a5\u53d7\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import bluetooth<\/p>\n<p>def connect_to_device(bt_address, port):<\/p>\n<p>    sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)<\/p>\n<p>    sock.connect((bt_address, port))<\/p>\n<p>    return sock<\/p>\n<p>def receive_data(sock):<\/p>\n<p>    try:<\/p>\n<p>        while True:<\/p>\n<p>            data = sock.recv(1024)<\/p>\n<p>            if not data:<\/p>\n<p>                break<\/p>\n<p>            print(f&quot;Received: {data}&quot;)<\/p>\n<p>    except OSError:<\/p>\n<p>        pass<\/p>\n<p>    finally:<\/p>\n<p>        sock.close()<\/p>\n<p>bt_address = &#39;XX:XX:XX:XX:XX:XX&#39;  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u84dd\u7259\u8bbe\u5907\u5730\u5740<\/p>\n<p>port = 1<\/p>\n<p>sock = connect_to_device(bt_address, port)<\/p>\n<p>receive_data(sock)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>pyserial<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>pyserial<\/code>\u5e93\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4e0e\u84dd\u7259\u4e32\u53e3\u901a\u4fe1\u3002\u5b83\u7684\u4f7f\u7528\u65b9\u6cd5\u4e0e\u6807\u51c6\u4e32\u53e3\u901a\u4fe1\u7c7b\u4f3c\u3002<\/p>\n<\/p>\n<p><h4>\u4e00\u3001\u5b89\u88c5<code>pyserial<\/code><\/h4>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyserial<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u4e8c\u3001\u8fde\u63a5\u5230\u84dd\u7259\u4e32\u53e3<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<p>def connect_to_serial(port):<\/p>\n<p>    ser = serial.Serial(port, baudrate=9600, timeout=1)<\/p>\n<p>    return ser<\/p>\n<p>def receive_data(ser):<\/p>\n<p>    try:<\/p>\n<p>        while True:<\/p>\n<p>            data = ser.readline().decode(&#39;utf-8&#39;).strip()<\/p>\n<p>            if data:<\/p>\n<p>                print(f&quot;Received: {data}&quot;)<\/p>\n<p>    except serial.SerialException:<\/p>\n<p>        pass<\/p>\n<p>    finally:<\/p>\n<p>        ser.close()<\/p>\n<p>port = &#39;\/dev\/rfcomm0&#39;  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u4e32\u53e3\u7aef\u53e3<\/p>\n<p>ser = connect_to_serial(port)<\/p>\n<p>receive_data(ser)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4f7f\u7528<code>socket<\/code>\u5e93<\/h3>\n<\/p>\n<p><h4>\u4e00\u3001\u521b\u5efaRFCOMM\u5957\u63a5\u5b57<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import socket<\/p>\n<p>def create_rfcomm_socket(bt_address, port):<\/p>\n<p>    sock = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)<\/p>\n<p>    sock.connect((bt_address, port))<\/p>\n<p>    return sock<\/p>\n<p>def receive_data(sock):<\/p>\n<p>    try:<\/p>\n<p>        while True:<\/p>\n<p>            data = sock.recv(1024)<\/p>\n<p>            if not data:<\/p>\n<p>                break<\/p>\n<p>            print(f&quot;Received: {data}&quot;)<\/p>\n<p>    except OSError:<\/p>\n<p>        pass<\/p>\n<p>    finally:<\/p>\n<p>        sock.close()<\/p>\n<p>bt_address = &#39;XX:XX:XX:XX:XX:XX&#39;  # \u66ff\u6362\u4e3a\u5b9e\u9645\u7684\u84dd\u7259\u8bbe\u5907\u5730\u5740<\/p>\n<p>port = 1<\/p>\n<p>sock = create_rfcomm_socket(bt_address, port)<\/p>\n<p>receive_data(sock)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\uff0c\u5305\u62ec<code>pybluez<\/code>\u3001<code>pyserial<\/code>\u548c<code>socket<\/code>\u5e93\u3002<strong>\u63a8\u8350\u4f7f\u7528<code>pybluez<\/code>\u5e93\uff0c\u56e0\u4e3a\u5b83\u662f\u4e13\u95e8\u4e3a\u84dd\u7259\u901a\u4fe1\u8bbe\u8ba1\u7684<\/strong>\u3002\u65e0\u8bba\u4f7f\u7528\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u5148\u627e\u5230\u84dd\u7259\u8bbe\u5907\uff0c\u7136\u540e\u8fde\u63a5\u5230\u8bbe\u5907\u5e76\u63a5\u6536\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5728Python\u4e2d\u5b9e\u73b0\u84dd\u7259\u4e32\u53e3\u901a\u4fe1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u4ee5\u63a5\u6536\u4e32\u53e3\u6570\u636e\uff1f<\/strong><br \/>\u8981\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u5e76\u63a5\u6536\u4e32\u53e3\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pybluez<\/code>\u5e93\u3002\u9996\u5148\uff0c\u786e\u4fdd\u5df2\u5b89\u88c5\u8be5\u5e93\u3002\u8fde\u63a5\u84dd\u7259\u8bbe\u5907\u65f6\uff0c\u9700\u8981\u83b7\u53d6\u8bbe\u5907\u7684MAC\u5730\u5740\uff0c\u5e76\u4f7f\u7528<code>bluetooth.BluetoothSocket<\/code>\u521b\u5efa\u8fde\u63a5\u3002\u901a\u8fc7\u8bbe\u7f6e\u4e3a\u4e32\u53e3\u670d\u52a1\uff0c\u60a8\u53ef\u4ee5\u8bfb\u53d6\u6570\u636e\u6d41\u3002\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import bluetooth\n\n# \u66ff\u6362\u4e3a\u5b9e\u9645\u7684MAC\u5730\u5740\nbd_addr = &quot;00:11:22:33:44:55&quot;  \nport = 1  # \u901a\u5e38\u4f7f\u75281\u53f7\u7aef\u53e3\nsock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)\nsock.connect((bd_addr, port))\n\nwhile True:\n    data = sock.recv(1024)  # \u63a5\u6536\u6570\u636e\n    print(&quot;\u63a5\u6536\u5230\u7684\u6570\u636e:&quot;, data)\n\nsock.close()\n<\/code><\/pre>\n<p><strong>\u5728Python\u4e2d\u5904\u7406\u63a5\u6536\u5230\u7684\u84dd\u7259\u4e32\u53e3\u6570\u636e\u65f6\u5e94\u8be5\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5904\u7406\u63a5\u6536\u5230\u7684\u6570\u636e\u65f6\uff0c\u786e\u4fdd\u8981\u68c0\u67e5\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u6709\u6548\u6027\u3002\u7531\u4e8e\u84dd\u7259\u8fde\u63a5\u53ef\u80fd\u4e0d\u7a33\u5b9a\uff0c\u5efa\u8bae\u5b9e\u73b0\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4ee5\u4fbf\u5728\u8fde\u63a5\u4e2d\u65ad\u6216\u6570\u636e\u9519\u8bef\u65f6\u8fdb\u884c\u9002\u5f53\u7684\u91cd\u8bd5\u6216\u5904\u7406\u3002\u6b64\u5916\uff0c\u6839\u636e\u6570\u636e\u7684\u683c\u5f0f\uff0c\u53ef\u80fd\u9700\u8981\u8fdb\u884c\u89e3\u6790\u6216\u8f6c\u6362\uff0c\u4ee5\u4fbf\u4e8e\u540e\u7eed\u4f7f\u7528\u3002<\/p>\n<p><strong>\u6709\u4ec0\u4e48\u5e93\u53ef\u4ee5\u7b80\u5316Python\u4e2d\u84dd\u7259\u4e32\u53e3\u6570\u636e\u7684\u63a5\u6536\u8fc7\u7a0b\uff1f<\/strong><br \/>\u9664\u4e86<code>pybluez<\/code>\uff0c<code>pyserial<\/code>\u4e5f\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5e93\u3002\u867d\u7136<code>pyserial<\/code>\u4e3b\u8981\u7528\u4e8e\u4e32\u53e3\u901a\u4fe1\uff0c\u4f46\u5b83\u53ef\u4ee5\u4e0e\u84dd\u7259\u4e32\u53e3\u8bbe\u5907\u914d\u5408\u4f7f\u7528\uff0c\u7b80\u5316\u6570\u636e\u63a5\u6536\u6d41\u7a0b\u3002\u4f7f\u7528<code>pyserial<\/code>\u65f6\uff0c\u53ef\u4ee5\u6307\u5b9a\u84dd\u7259\u8bbe\u5907\u7684\u4e32\u53e3\u540d\u79f0\uff08\u5982<code>COM3<\/code>\uff09\uff0c\u7136\u540e\u50cf\u64cd\u4f5c\u666e\u901a\u4e32\u53e3\u4e00\u6837\u63a5\u6536\u6570\u636e\u3002<\/p>\n<pre><code class=\"language-python\">import serial\n\n# \u66ff\u6362\u4e3a\u84dd\u7259\u4e32\u53e3\u7684\u540d\u79f0\nbluetooth_port = &quot;COM3&quot;  \nbaud_rate = 9600\nser = serial.Serial(bluetooth_port, baud_rate)\n\nwhile True:\n    data = ser.readline()  # \u6309\u884c\u8bfb\u53d6\u6570\u636e\n    print(&quot;\u63a5\u6536\u5230\u7684\u6570\u636e:&quot;, data.decode().strip())\n\nser.close()\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u63a5\u53d7\u84dd\u7259\u4e32\u53e3\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528pybluez\u5e93\u3001pyserial\u5e93\u3001socket\u5e93\u3002\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86 [&hellip;]","protected":false},"author":3,"featured_media":1161261,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161251"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1161251"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161251\/revisions"}],"predecessor-version":[{"id":1161265,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1161251\/revisions\/1161265"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1161261"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1161251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1161251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1161251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}