{"id":1044287,"date":"2024-12-31T13:13:14","date_gmt":"2024-12-31T05:13:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1044287.html"},"modified":"2024-12-31T13:13:17","modified_gmt":"2024-12-31T05:13:17","slug":"python%e5%a6%82%e4%bd%95%e5%8f%91%e9%80%81%e5%ad%97%e7%ac%a6%e4%b8%b2%e7%bb%99%e4%b8%b2%e5%8f%a3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1044287.html","title":{"rendered":"python\u5982\u4f55\u53d1\u9001\u5b57\u7b26\u4e32\u7ed9\u4e32\u53e3"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/e2f9c33b-2e15-4c23-b293-059318105cc4.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u53d1\u9001\u5b57\u7b26\u4e32\u7ed9\u4e32\u53e3\" \/><\/p>\n<p><p> <strong>\u8981\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u53d1\u9001\u5230\u4e32\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pyserial<\/code>\u5e93\u3001\u914d\u7f6e\u4e32\u53e3\u53c2\u6570\u3001\u6253\u5f00\u4e32\u53e3\u3001\u53d1\u9001\u6570\u636e<\/strong>\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u5b9e\u73b0\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<\/p>\n<p><p>Python\u662f\u4e00\u79cd\u5f3a\u5927\u7684\u7f16\u7a0b\u8bed\u8a00\uff0c\u5e7f\u6cdb\u5e94\u7528\u4e8e\u5404\u79cd\u9886\u57df\uff0c\u5305\u62ec\u4e0e\u786c\u4ef6\u8bbe\u5907\u7684\u901a\u4fe1\u3002\u4e32\u884c\u901a\u4fe1\u662f\u786c\u4ef6\u4e0e\u8ba1\u7b97\u673a\u4e4b\u95f4\u5e38\u7528\u7684\u4e00\u79cd\u901a\u4fe1\u65b9\u5f0f\u3002\u901a\u8fc7\u4e32\u53e3\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u662f\u5f88\u591a\u786c\u4ef6\u5e94\u7528\u7684\u57fa\u7840\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5<code>pyserial<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u60a8\u9700\u8981\u5b89\u88c5<code>pyserial<\/code>\u5e93\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8e\u4e32\u884c\u901a\u4fe1\u7684Python\u5e93\u3002\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyserial<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4ee3\u7801\u4e2d\u5bfc\u5165\u8be5\u5e93\uff0c\u5e76\u5f00\u59cb\u8fdb\u884c\u4e32\u53e3\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u4e32\u53e3\u53c2\u6570<\/h3>\n<\/p>\n<p><p>\u5728\u8fdb\u884c\u4e32\u53e3\u901a\u4fe1\u4e4b\u524d\uff0c\u60a8\u9700\u8981\u914d\u7f6e\u4e32\u53e3\u53c2\u6570\u3002\u8fd9\u4e9b\u53c2\u6570\u5305\u62ec\u7aef\u53e3\u53f7\u3001\u6ce2\u7279\u7387\u3001\u6570\u636e\u4f4d\u3001\u505c\u6b62\u4f4d\u548c\u6821\u9a8c\u4f4d\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<h2><strong>\u914d\u7f6e\u4e32\u53e3\u53c2\u6570<\/strong><\/h2>\n<p>port = &#39;COM3&#39;  # \u6839\u636e\u5b9e\u9645\u60c5\u51b5\u9009\u62e9\u4e32\u53e3\u53f7<\/p>\n<p>baudrate = 9600  # \u6ce2\u7279\u7387<\/p>\n<p>timeout = 1  # \u8d85\u65f6\u65f6\u95f4<\/p>\n<h2><strong>\u6253\u5f00\u4e32\u53e3<\/strong><\/h2>\n<p>ser = serial.Serial(port, baudrate, timeout=timeout)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u53d1\u9001\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u6253\u5f00\u4e32\u53e3\u540e\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>write<\/code>\u65b9\u6cd5\u53d1\u9001\u6570\u636e\u3002<code>write<\/code>\u65b9\u6cd5\u9700\u8981\u4f20\u5165\u4e00\u4e2a\u5b57\u8282\u6570\u7ec4\uff0c\u56e0\u6b64\u5728\u53d1\u9001\u5b57\u7b26\u4e32\u65f6\u9700\u8981\u5c06\u5176\u7f16\u7801\u4e3a\u5b57\u8282\u6570\u7ec4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u53d1\u9001\u5b57\u7b26\u4e32\u6570\u636e<\/p>\n<p>data = &quot;Hello, Serial Port!&quot;<\/p>\n<p>ser.write(data.encode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u63a5\u6536\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u786e\u4fdd\u6570\u636e\u6b63\u786e\u53d1\u9001\u548c\u63a5\u6536\uff0c\u60a8\u8fd8\u53ef\u4ee5\u4f7f\u7528<code>read<\/code>\u65b9\u6cd5\u6765\u63a5\u6536\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u63a5\u6536\u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u63a5\u6536\u6570\u636e<\/p>\n<p>received_data = ser.read(100)  # \u8bfb\u53d6100\u4e2a\u5b57\u8282<\/p>\n<p>print(received_data.decode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5173\u95ed\u4e32\u53e3<\/h3>\n<\/p>\n<p><p>\u5b8c\u6210\u901a\u4fe1\u540e\uff0c\u8bb0\u5f97\u5173\u95ed\u4e32\u53e3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5173\u95ed\u4e32\u53e3<\/p>\n<p>ser.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\uff0c\u5c55\u793a\u4e86\u5982\u4f55\u5728Python\u4e2d\u901a\u8fc7\u4e32\u53e3\u53d1\u9001\u5b57\u7b26\u4e32\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<h2><strong>\u914d\u7f6e\u4e32\u53e3\u53c2\u6570<\/strong><\/h2>\n<p>port = &#39;COM3&#39;<\/p>\n<p>baudrate = 9600<\/p>\n<p>timeout = 1<\/p>\n<h2><strong>\u6253\u5f00\u4e32\u53e3<\/strong><\/h2>\n<p>ser = serial.Serial(port, baudrate, timeout=timeout)<\/p>\n<h2><strong>\u53d1\u9001\u5b57\u7b26\u4e32\u6570\u636e<\/strong><\/h2>\n<p>data = &quot;Hello, Serial Port!&quot;<\/p>\n<p>ser.write(data.encode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u63a5\u6536\u6570\u636e<\/strong><\/h2>\n<p>received_data = ser.read(100)<\/p>\n<p>print(received_data.decode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u5173\u95ed\u4e32\u53e3<\/strong><\/h2>\n<p>ser.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5e38\u89c1\u95ee\u9898<\/h3>\n<\/p>\n<p><h4>1\u3001\u4e32\u53e3\u65e0\u6cd5\u6253\u5f00<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u4e32\u53e3\u65e0\u6cd5\u6253\u5f00\uff0c\u8bf7\u68c0\u67e5\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u786e\u8ba4\u4e32\u53e3\u53f7\u662f\u5426\u6b63\u786e<\/li>\n<li>\u786e\u8ba4\u8bbe\u5907\u662f\u5426\u8fde\u63a5<\/li>\n<li>\u68c0\u67e5\u662f\u5426\u6709\u5176\u4ed6\u7a0b\u5e8f\u5360\u7528\u4e86\u4e32\u53e3<\/li>\n<\/ul>\n<p><h4>2\u3001\u6570\u636e\u53d1\u9001\u5931\u8d25<\/h4>\n<\/p>\n<p><p>\u6570\u636e\u53d1\u9001\u5931\u8d25\u53ef\u80fd\u662f\u7531\u4e8e\u4ee5\u4e0b\u539f\u56e0\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u6ce2\u7279\u7387\u914d\u7f6e\u4e0d\u6b63\u786e<\/li>\n<li>\u6570\u636e\u7f16\u7801\u4e0d\u6b63\u786e<\/li>\n<li>\u4e32\u53e3\u672a\u6b63\u786e\u6253\u5f00<\/li>\n<\/ul>\n<p><h3>\u4e03\u3001\u9ad8\u7ea7\u529f\u80fd<\/h3>\n<\/p>\n<p><p><code>pyserial<\/code>\u5e93\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ul>\n<li>\u8bbe\u7f6e\u786c\u4ef6\u548c\u8f6f\u4ef6\u6d41\u63a7\u5236<\/li>\n<li>\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u7684\u65b9\u5f0f\u8fdb\u884c\u901a\u4fe1<\/li>\n<li>\u5904\u7406\u4e8c\u8fdb\u5236\u6570\u636e<\/li>\n<\/ul>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528\u4e8b\u4ef6\u9a71\u52a8\u65b9\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<p>import threading<\/p>\n<p>def read_from_port(ser):<\/p>\n<p>    while True:<\/p>\n<p>        if ser.in_w<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ting &gt; 0:<\/p>\n<p>            data = ser.read(ser.in_waiting)<\/p>\n<p>            print(data.decode(&#39;utf-8&#39;))<\/p>\n<h2><strong>\u914d\u7f6e\u4e32\u53e3\u53c2\u6570<\/strong><\/h2>\n<p>port = &#39;COM3&#39;<\/p>\n<p>baudrate = 9600<\/p>\n<p>timeout = 1<\/p>\n<h2><strong>\u6253\u5f00\u4e32\u53e3<\/strong><\/h2>\n<p>ser = serial.Serial(port, baudrate, timeout=timeout)<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u542f\u52a8\u7ebf\u7a0b<\/strong><\/h2>\n<p>thread = threading.Thread(target=read_from_port, args=(ser,))<\/p>\n<p>thread.start()<\/p>\n<h2><strong>\u53d1\u9001\u5b57\u7b26\u4e32\u6570\u636e<\/strong><\/h2>\n<p>data = &quot;Hello, Serial Port!&quot;<\/p>\n<p>ser.write(data.encode(&#39;utf-8&#39;))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u60a8\u53ef\u4ee5\u5728Python\u4e2d\u5b9e\u73b0\u5bf9\u4e32\u53e3\u7684\u5b57\u7b26\u4e32\u53d1\u9001\u4e0e\u63a5\u6536\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\u3002\u5982\u679c\u60a8\u6709\u4efb\u4f55\u95ee\u9898\u6216\u9700\u8981\u8fdb\u4e00\u6b65\u7684\u5e2e\u52a9\uff0c\u8bf7\u968f\u65f6\u5411\u6211\u4eec\u63d0\u95ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u9009\u62e9\u5408\u9002\u7684\u4e32\u53e3\u5e93\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u7528\u4e8e\u4e32\u53e3\u901a\u4fe1\uff0c\u5176\u4e2d\u6700\u5e38\u7528\u7684\u662f<code>pySerial<\/code>\u3002\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u7b80\u5355\u7684\u63a5\u53e3\u6765\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u5b89\u88c5\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>pip install pyserial<\/code>\u8f7b\u677e\u83b7\u53d6\u3002\u9009\u62e9\u5e93\u65f6\uff0c\u8003\u8651\u5230\u6027\u80fd\u548c\u6613\u7528\u6027\uff0c<code>pySerial<\/code>\u901a\u5e38\u662f\u6700\u4f73\u9009\u62e9\u3002<\/p>\n<p><strong>Python\u53d1\u9001\u5b57\u7b26\u4e32\u5230\u4e32\u53e3\u7684\u57fa\u672c\u6b65\u9aa4\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u53d1\u9001\u5b57\u7b26\u4e32\u5230\u4e32\u53e3\u7684\u57fa\u672c\u6b65\u9aa4\u5305\u62ec\uff1a\u9996\u5148\uff0c\u5bfc\u5165<code>serial<\/code>\u5e93\uff1b\u5176\u6b21\uff0c\u521b\u5efa\u4e00\u4e2a\u4e32\u53e3\u5bf9\u8c61\u5e76\u6307\u5b9a\u5bf9\u5e94\u7684\u4e32\u53e3\u53f7\u3001\u6ce2\u7279\u7387\u7b49\u53c2\u6570\uff1b\u63a5\u7740\uff0c\u4f7f\u7528\u4e32\u53e3\u5bf9\u8c61\u7684<code>write()<\/code>\u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u5b57\u8282\u5e76\u53d1\u9001\u3002\u5b8c\u6210\u540e\uff0c\u52a1\u5fc5\u5173\u95ed\u4e32\u53e3\uff0c\u4ee5\u786e\u4fdd\u8d44\u6e90\u5f97\u5230\u91ca\u653e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u53d1\u9001\u8fc7\u7a0b\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u4e32\u53e3\u901a\u4fe1\u65f6\uff0c\u9519\u8bef\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u73af\u8282\u3002\u53ef\u4ee5\u901a\u8fc7<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u7684\u5f02\u5e38\uff0c\u5982<code>serial.SerialException<\/code>\u548c<code>ValueError<\/code>\u3002\u901a\u8fc7\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\uff0c\u53ef\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u5728\u9047\u5230\u95ee\u9898\u65f6\u4e0d\u4f1a\u5d29\u6e83\uff0c\u80fd\u591f\u63d0\u4f9b\u6e05\u6670\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u5e76\u4e14\u53ef\u4ee5\u8fdb\u884c\u76f8\u5e94\u7684\u91cd\u8bd5\u6216\u9000\u51fa\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728Python\u4e2d\u5c06\u5b57\u7b26\u4e32\u53d1\u9001\u5230\u4e32\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528pyserial\u5e93\u3001\u914d\u7f6e\u4e32\u53e3\u53c2\u6570\u3001\u6253\u5f00\u4e32\u53e3\u3001\u53d1\u9001\u6570\u636e\u3002\u4e0b\u9762\u6211\u4eec\u5c06 [&hellip;]","protected":false},"author":3,"featured_media":1044298,"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\/1044287"}],"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=1044287"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044287\/revisions"}],"predecessor-version":[{"id":1044301,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1044287\/revisions\/1044301"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1044298"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1044287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1044287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1044287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}