{"id":941200,"date":"2024-12-26T21:28:01","date_gmt":"2024-12-26T13:28:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/941200.html"},"modified":"2024-12-26T21:28:05","modified_gmt":"2024-12-26T13:28:05","slug":"arduino%e5%a6%82%e4%bd%95%e6%8e%a5%e5%85%a5python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/941200.html","title":{"rendered":"arduino\u5982\u4f55\u63a5\u5165python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25075502\/1607bdc0-e9c2-4aa1-89fe-53f2280724f6.webp\" alt=\"arduino\u5982\u4f55\u63a5\u5165python\" \/><\/p>\n<p><p> <strong>\u8981\u5c06Arduino\u63a5\u5165Python\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u7f16\u5199Arduino\u548cPython\u4ee3\u7801\u3002\u4e32\u53e3\u901a\u4fe1\u662f\u6700\u5e38\u7528\u7684\u65b9\u6cd5\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u4e24\u4e2a\u8bbe\u5907\u4e4b\u95f4\u8fdb\u884c\u7b80\u5355\u7684\u6570\u636e\u4f20\u8f93\u3002<\/strong>\u53ef\u4ee5\u901a\u8fc7\u5b89\u88c5pySerial\u5e93\u5728Python\u4e2d\u5904\u7406\u4e32\u53e3\u901a\u4fe1\u3002\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u8fde\u63a5Arduino\u548cPython\uff0c\u4ee5\u53ca\u5177\u4f53\u7684\u6b65\u9aa4\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4ec0\u4e48\u662f\u4e32\u53e3\u901a\u4fe1\uff1f<\/p>\n<\/p>\n<p><p>\u4e32\u53e3\u901a\u4fe1\u662f\u4e00\u79cd\u5141\u8bb8\u4e24\u4e2a\u8bbe\u5907\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4ea4\u6362\u7684\u6280\u672f\u3002\u5728Arduino\u4e0ePython\u7684\u6574\u5408\u4e2d\uff0c\u4e32\u53e3\u901a\u4fe1\u901a\u5e38\u662f\u6700\u7b80\u5355\u548c\u6700\u6709\u6548\u7684\u65b9\u5f0f\u4e4b\u4e00\u3002Arduino\u53ef\u4ee5\u901a\u8fc7USB\u7535\u7f06\u8fde\u63a5\u5230\u8ba1\u7b97\u673a\uff0c\u8fd9\u79cd\u8fde\u63a5\u901a\u5e38\u88ab\u89c6\u4e3a\u4e32\u53e3\u8fde\u63a5\u3002\u901a\u8fc7\u8fd9\u79cd\u8fde\u63a5\uff0c\u6211\u4eec\u53ef\u4ee5\u5728Python\u4e2d\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>Arduino\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u7684\u57fa\u672c\u539f\u7406\u662f\uff1a\u901a\u8fc7\u5728Arduino\u4ee3\u7801\u4e2d\u5b9a\u4e49\u4e32\u53e3\u901a\u4fe1\u7684\u6ce2\u7279\u7387\uff08\u901a\u5e38\u662f9600 bps\uff09\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u8ba1\u7b97\u673a\u548cArduino\u4e4b\u95f4\u4f20\u8f93\u6570\u636e\u3002\u5728Python\u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528pySerial\u5e93\u6765\u5904\u7406\u8fd9\u4e9b\u6570\u636e\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u548c\u5e93<\/p>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u786e\u4fdd\u5b89\u88c5\u4e86\u4e00\u4e9b\u5fc5\u8981\u7684\u8f6f\u4ef6\u548c\u5e93\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>Arduino IDE<\/strong>\uff1a\u8fd9\u662f\u7f16\u5199\u548c\u4e0a\u4f20Arduino\u4ee3\u7801\u7684\u5fc5\u5907\u5de5\u5177\u3002\u53ef\u4ee5\u4eceArduino\u5b98\u65b9\u7f51\u7ad9\u4e0b\u8f7d\u5e76\u5b89\u88c5\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>Python<\/strong>\uff1a\u786e\u4fdd\u8ba1\u7b97\u673a\u4e0a\u5b89\u88c5\u4e86Python\u3002\u63a8\u8350\u4f7f\u7528Python 3.x\u7248\u672c\uff0c\u56e0\u4e3a\u5b83\u662f\u6700\u65b0\u7684\u4e14\u6709\u66f4\u591a\u7684\u529f\u80fd\u548c\u652f\u6301\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>pySerial\u5e93<\/strong>\uff1a\u8fd9\u662fPython\u4e2d\u7528\u4e8e\u4e32\u53e3\u901a\u4fe1\u7684\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7pip\u5b89\u88c5\uff0c\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pyserial<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u7f16\u5199Arduino\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u5728Arduino\u4e2d\uff0c\u6211\u4eec\u9700\u8981\u7f16\u5199\u4e00\u6bb5\u4ee3\u7801\u4ee5\u4fbf\u4e0ePython\u8fdb\u884c\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-cpp\">void setup() {<\/p>\n<p>  Serial.begin(9600); \/\/ \u8bbe\u7f6e\u4e32\u53e3\u6ce2\u7279\u7387<\/p>\n<p>}<\/p>\n<p>void loop() {<\/p>\n<p>  if (Serial.av<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>lable() &gt; 0) {<\/p>\n<p>    int data = Serial.read(); \/\/ \u8bfb\u53d6\u6765\u81eaPython\u7684\u6570\u636e<\/p>\n<p>    Serial.println(data);     \/\/ \u6253\u5370\u6536\u5230\u7684\u6570\u636e<\/p>\n<p>  }<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u6bb5\u4ee3\u7801\u7684\u4f5c\u7528\u662f\u521d\u59cb\u5316\u4e32\u53e3\u901a\u4fe1\uff0c\u5e76\u4e14\u5728\u63a5\u6536\u5230\u6765\u81eaPython\u7684\u6570\u636e\u65f6\uff0c\u5c06\u5176\u8bfb\u53d6\u5e76\u901a\u8fc7\u4e32\u53e3\u6253\u5370\u51fa\u6765\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u7f16\u5199Python\u4ee3\u7801<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u5728Python\u4e2d\u7f16\u5199\u4ee3\u7801\uff0c\u4ee5\u4fbf\u4e0eArduino\u8fdb\u884c\u901a\u4fe1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u4ee3\u7801\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<p>import time<\/p>\n<h2><strong>\u8bbe\u7f6e\u4e32\u53e3\u8fde\u63a5\u53c2\u6570<\/strong><\/h2>\n<p>arduino = serial.Serial(port=&#39;COM3&#39;, baudrate=9600, timeout=.1) <\/p>\n<p>def write_read(x):<\/p>\n<p>    arduino.write(bytes(x, &#39;utf-8&#39;))  # \u5c06\u6570\u636e\u53d1\u9001\u7ed9Arduino<\/p>\n<p>    time.sleep(0.05)  # \u7a0d\u4f5c\u5ef6\u65f6<\/p>\n<p>    data = arduino.readline()  # \u8bfb\u53d6Arduino\u8fd4\u56de\u7684\u6570\u636e<\/p>\n<p>    return data<\/p>\n<p>while True:<\/p>\n<p>    num = input(&quot;Enter a number: &quot;)  # \u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165<\/p>\n<p>    value = write_read(num)<\/p>\n<p>    print(value)  # \u6253\u5370Arduino\u8fd4\u56de\u7684\u6570\u636e<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u6bb5Python\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u901a\u8fc7pySerial\u5e93\u5efa\u7acb\u4e0eArduino\u7684\u4e32\u53e3\u8fde\u63a5\u3002\u7136\u540e\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u4e2a\u51fd\u6570<code>write_read()<\/code>\uff0c\u7528\u4e8e\u53d1\u9001\u6570\u636e\u7ed9Arduino\u5e76\u8bfb\u53d6\u8fd4\u56de\u7684\u6570\u636e\u3002\u6700\u540e\uff0c\u901a\u8fc7\u4e00\u4e2a\u65e0\u9650\u5faa\u73af\uff0c\u6211\u4eec\u4e0d\u65ad\u4ece\u7528\u6237\u83b7\u53d6\u8f93\u5165\uff0c\u5e76\u5c06\u5176\u53d1\u9001\u7ed9Arduino\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u6d4b\u8bd5\u548c\u8c03\u8bd5<\/p>\n<\/p>\n<p><p>\u5728\u5b8c\u6210\u4ee3\u7801\u7f16\u5199\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u6d4b\u8bd5\u548c\u8c03\u8bd5\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u4e0a\u4f20Arduino\u4ee3\u7801<\/strong>\uff1a\u4f7f\u7528Arduino IDE\u5c06\u4ee3\u7801\u4e0a\u4f20\u5230Arduino\u677f\u4e0a\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8fd0\u884cPython\u811a\u672c<\/strong>\uff1a\u5728\u547d\u4ee4\u884c\u6216\u7ec8\u7aef\u4e2d\u8fd0\u884cPython\u811a\u672c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u89c2\u5bdf\u7ed3\u679c<\/strong>\uff1a\u5728\u7ec8\u7aef\u4e2d\u8f93\u5165\u6570\u5b57\uff0c\u89c2\u5bdfArduino\u8fd4\u56de\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u5728\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e9b\u5e38\u89c1\u7684\u95ee\u9898\uff0c\u5982\u4e32\u53e3\u5360\u7528\u3001\u6ce2\u7279\u7387\u4e0d\u5339\u914d\u7b49\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u4e32\u53e3\u8bbe\u7f6e\u6216\u68c0\u67e5\u8fde\u63a5\u6765\u89e3\u51b3\u8fd9\u4e9b\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u5e94\u7528\u573a\u666f\u548c\u6269\u5c55<\/p>\n<\/p>\n<p><p>\u5c06Arduino\u4e0ePython\u7ed3\u5408\u4f7f\u7528\uff0c\u53ef\u4ee5\u5b9e\u73b0\u8bb8\u591a\u6709\u8da3\u548c\u5b9e\u7528\u7684\u9879\u76ee\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u6570\u636e\u91c7\u96c6<\/strong>\uff1a\u901a\u8fc7Arduino\u91c7\u96c6\u4f20\u611f\u5668\u6570\u636e\uff0c\u5e76\u4f7f\u7528Python\u8fdb\u884c\u6570\u636e\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u81ea\u52a8\u5316\u63a7\u5236<\/strong>\uff1a\u4f7f\u7528Python\u63a7\u5236Arduino\u4e0a\u7684\u7535\u673a\u6216\u5176\u4ed6\u6267\u884c\u5668\uff0c\u5b9e\u73b0\u81ea\u52a8\u5316\u4efb\u52a1\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u7269\u8054\u7f51\u9879\u76ee<\/strong>\uff1a\u7ed3\u5408\u7f51\u7edc\u5e93\uff0c\u5982Flask\u6216Django\uff0c\u5c06Arduino\u7684\u6570\u636e\u4e0a\u4f20\u5230\u7f51\u7edc\uff0c\u5b9e\u73b0\u8fdc\u7a0b\u76d1\u63a7\u548c\u63a7\u5236\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0d\u65ad\u7684\u5b9e\u9a8c\u548c\u5b9e\u8df5\uff0c\u53ef\u4ee5\u63a2\u7d22\u66f4\u591aArduino\u4e0ePython\u7ed3\u5408\u7684\u53ef\u80fd\u6027\uff0c\u521b\u9020\u51fa\u66f4\u591a\u5bcc\u6709\u521b\u610f\u548c\u4ef7\u503c\u7684\u9879\u76ee\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Arduino\u4e2d\u4f7f\u7528Python\u8fdb\u884c\u7f16\u7a0b\uff1f<\/strong><br \/>\u8981\u5728Arduino\u4e2d\u4f7f\u7528Python\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7PySerial\u5e93\u4e0eArduino\u8fdb\u884c\u4e32\u884c\u901a\u4fe1\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u5df2\u7ecf\u5728Arduino IDE\u4e2d\u7f16\u5199\u5e76\u4e0a\u4f20\u4e86\u4e00\u4e2a\u80fd\u591f\u63a5\u53d7\u4e32\u884c\u8f93\u5165\u7684\u8349\u56fe\u3002\u63a5\u7740\uff0c\u5b89\u88c5PySerial\u5e93\uff0c\u5e76\u7f16\u5199Python\u811a\u672c\u6765\u53d1\u9001\u548c\u63a5\u6536\u6570\u636e\u3002\u8fd9\u6837\uff0c\u60a8\u5c31\u53ef\u4ee5\u5229\u7528Python\u7684\u5f3a\u5927\u529f\u80fd\u6765\u63a7\u5236Arduino\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u7528Python\u63a7\u5236Arduino\u7684\u54ea\u4e9b\u529f\u80fd\uff1f<\/strong><br \/>\u4f7f\u7528Python\u63a7\u5236Arduino\uff0c\u60a8\u53ef\u4ee5\u5b9e\u73b0\u591a\u79cd\u529f\u80fd\uff0c\u5305\u62ec\u8bfb\u53d6\u4f20\u611f\u5668\u6570\u636e\u3001\u63a7\u5236\u7535\u673a\u3001\u706f\u5149\u4ee5\u53ca\u5176\u4ed6\u5916\u8bbe\u3002\u901a\u8fc7\u4e32\u884c\u901a\u4fe1\uff0cPython\u80fd\u591f\u53d1\u9001\u547d\u4ee4\u7ed9Arduino\uff0c\u5e76\u63a5\u6536Arduino\u53d1\u9001\u7684\u6570\u636e\uff0c\u4ece\u800c\u5b9e\u73b0\u590d\u6742\u7684\u63a7\u5236\u903b\u8f91\u548c\u6570\u636e\u5904\u7406\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406Arduino\u548cPython\u4e4b\u95f4\u7684\u6570\u636e\u4f20\u8f93\u95ee\u9898\uff1f<\/strong><br \/>\u5728Arduino\u4e0ePython\u4e4b\u95f4\u8fdb\u884c\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u786e\u4fdd\u4e24\u8005\u7684\u6ce2\u7279\u7387\u8bbe\u7f6e\u4e00\u81f4\u3002\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u7684\u4e32\u884c\u901a\u4fe1\u534f\u8bae\uff0c\u786e\u4fdd\u6570\u636e\u683c\u5f0f\uff08\u5982\u5b57\u7b26\u4e32\u3001\u6574\u6570\u7b49\uff09\u76f8\u5339\u914d\u3002\u5728Python\u4e2d\uff0c\u4f7f\u7528PySerial\u5e93\u5904\u7406\u6570\u636e\u4f20\u8f93\u65f6\uff0c\u6ce8\u610f\u5904\u7406\u7f13\u51b2\u533a\u548c\u6570\u636e\u89e3\u6790\uff0c\u4ee5\u907f\u514d\u4e22\u5305\u6216\u6570\u636e\u9519\u8bef\u3002\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u4e5f\u662f\u786e\u4fdd\u6570\u636e\u4f20\u8f93\u987a\u5229\u7684\u91cd\u8981\u90e8\u5206\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06Arduino\u63a5\u5165Python\uff0c\u901a\u5e38\u9700\u8981\u4f7f\u7528\u4e32\u53e3\u901a\u4fe1\u3001\u5b89\u88c5\u76f8\u5173\u5e93\u3001\u7f16\u5199Arduino\u548cPython\u4ee3\u7801\u3002\u4e32 [&hellip;]","protected":false},"author":3,"featured_media":941207,"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\/941200"}],"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=941200"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941200\/revisions"}],"predecessor-version":[{"id":941208,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/941200\/revisions\/941208"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/941207"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=941200"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=941200"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=941200"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}