{"id":1047284,"date":"2024-12-31T13:41:13","date_gmt":"2024-12-31T05:41:13","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1047284.html"},"modified":"2024-12-31T13:41:21","modified_gmt":"2024-12-31T05:41:21","slug":"python%e7%9a%84%e7%a8%8b%e5%ba%8f%e5%a6%82%e4%bd%95%e5%86%99%e5%85%a5%e7%a1%ac%e4%bb%b6%e4%b8%ad","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1047284.html","title":{"rendered":"python\u7684\u7a0b\u5e8f\u5982\u4f55\u5199\u5165\u786c\u4ef6\u4e2d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/ea94ea4e-6068-4504-943f-17be32c61624.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u7684\u7a0b\u5e8f\u5982\u4f55\u5199\u5165\u786c\u4ef6\u4e2d\" \/><\/p>\n<p><p> <strong>Python\u7684\u7a0b\u5e8f\u5199\u5165\u786c\u4ef6\u4e2d\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528MicroPython\u6216CircuitPython\u3001\u4f7f\u7528\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u73af\u5883\u3001\u901a\u8fc7\u4e0e\u786c\u4ef6\u63a5\u53e3\u901a\u4fe1\u3001\u4f7f\u7528\u5355\u677f\u8ba1\u7b97\u673a\uff08\u5982Raspberry Pi\uff09\u3002<\/strong> \u5176\u4e2d\uff0c<strong>\u4f7f\u7528MicroPython\u6216CircuitPython<\/strong> \u662f\u4e00\u79cd\u975e\u5e38\u6d41\u884c\u4e14\u9ad8\u6548\u7684\u65b9\u6cd5\uff0c\u7279\u522b\u9002\u7528\u4e8e\u5fae\u63a7\u5236\u5668\u3002MicroPython\u662f\u4e00\u4e2a\u7cbe\u7b80\u9ad8\u6548\u7684Python 3\u5b9e\u73b0\uff0c\u4e13\u95e8\u7528\u4e8e\u8fd0\u884c\u5728\u5fae\u63a7\u5236\u5668\u548c\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e0a\u3002\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e00\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528MicroPython\u6216CircuitPython<\/h3>\n<\/p>\n<p><p>MicroPython\u548cCircuitPython\u662f\u9002\u7528\u4e8e\u5fae\u63a7\u5236\u5668\u7f16\u7a0b\u7684\u4e24\u4e2aPython\u7248\u672c\u3002\u8fd9\u4e24\u4e2a\u7248\u672c\u7684Python\u975e\u5e38\u9002\u5408\u5728\u8d44\u6e90\u6709\u9650\u7684\u73af\u5883\u4e2d\u8fd0\u884c\uff0c\u4f7f\u5f97\u5f00\u53d1\u5d4c\u5165\u5f0f\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u7b80\u5355\u548c\u9ad8\u6548\u3002<\/p>\n<\/p>\n<p><h4>1\u3001MicroPython\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>MicroPython\u662f\u4e00\u4e2a\u7cbe\u7b80\u7248\u7684Python 3\u5b9e\u73b0\uff0c\u4e13\u95e8\u4e3a\u5fae\u63a7\u5236\u5668\u548c\u5d4c\u5165\u5f0f\u7cfb\u7edf\u800c\u8bbe\u8ba1\u3002\u5b83\u63d0\u4f9b\u4e86\u5927\u90e8\u5206Python\u6807\u51c6\u5e93\uff0c\u5e76\u4e14\u80fd\u591f\u76f4\u63a5\u4e0e\u786c\u4ef6\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5MicroPython\u56fa\u4ef6<\/strong>\uff1a\u9996\u5148\uff0c\u9700\u8981\u5c06MicroPython\u56fa\u4ef6\u70e7\u5f55\u5230\u5fae\u63a7\u5236\u5668\u4e2d\u3002\u53ef\u4ee5\u901a\u8fc7\u5236\u9020\u5546\u63d0\u4f9b\u7684\u5de5\u5177\u6216\u8005\u4f7f\u7528esptool.py\u7b49\u5f00\u6e90\u5de5\u5177\u6765\u5b8c\u6210\u70e7\u5f55\u8fc7\u7a0b\u3002<\/li>\n<li><strong>\u7f16\u5199Python\u4ee3\u7801<\/strong>\uff1a\u5c06Python\u4ee3\u7801\u5199\u5165\u540d\u4e3a<code>m<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n.py<\/code>\u7684\u6587\u4ef6\u4e2d\uff0c\u8fd9\u4e2a\u6587\u4ef6\u5c06\u5728\u8bbe\u5907\u542f\u52a8\u65f6\u81ea\u52a8\u8fd0\u884c\u3002<\/li>\n<li><strong>\u4f20\u8f93\u4ee3\u7801<\/strong>\uff1a\u53ef\u4ee5\u901a\u8fc7\u4e32\u884c\u901a\u4fe1\u3001USB\u6216\u8005Wi-Fi\u5c06\u4ee3\u7801\u4f20\u8f93\u5230\u8bbe\u5907\u4e2d\u3002\u4f7f\u7528\u5de5\u5177\u5982ampy\u3001rshell\u6216\u8005Thonny IDE\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># main.py<\/p>\n<p>from machine import Pin<\/p>\n<p>import time<\/p>\n<p>led = Pin(2, Pin.OUT)<\/p>\n<p>while True:<\/p>\n<p>    led.value(not led.value())<\/p>\n<p>    time.sleep(0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\u5c55\u793a\u4e86\u5982\u4f55\u5728MicroPython\u4e2d\u63a7\u5236\u4e00\u4e2aLED\u706f\u7684\u95ea\u70c1\u3002<\/p>\n<\/p>\n<p><h4>2\u3001CircuitPython\u4ecb\u7ecd<\/h4>\n<\/p>\n<p><p>CircuitPython\u662f\u7531Adafruit\u5f00\u53d1\u7684\u4e00\u4e2aPython\u7248\u672c\uff0c\u57fa\u4e8eMicroPython\uff0c\u4e3b\u8981\u9762\u5411\u6559\u80b2\u548c\u521d\u5b66\u8005\u3002\u5b83\u66f4\u52a0\u6ce8\u91cd\u6613\u7528\u6027\u548c\u5373\u63d2\u5373\u7528\u7684\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<ul>\n<li><strong>\u5b89\u88c5CircuitPython\u56fa\u4ef6<\/strong>\uff1a\u7c7b\u4f3c\u4e8eMicroPython\uff0c\u5c06CircuitPython\u56fa\u4ef6\u70e7\u5f55\u5230\u652f\u6301\u7684\u5fae\u63a7\u5236\u5668\u4e0a\u3002<\/li>\n<li><strong>\u7f16\u5199\u4ee3\u7801<\/strong>\uff1a\u5c06Python\u4ee3\u7801\u5199\u5165\u540d\u4e3a<code>code.py<\/code>\u7684\u6587\u4ef6\u4e2d\uff0c\u8bbe\u5907\u542f\u52a8\u65f6\u4f1a\u81ea\u52a8\u8fd0\u884c\u8fd9\u4e2a\u6587\u4ef6\u3002<\/li>\n<li><strong>\u4f20\u8f93\u4ee3\u7801<\/strong>\uff1a\u5728\u8fde\u63a5\u8bbe\u5907\u540e\uff0c\u8bbe\u5907\u4f1a\u663e\u793a\u4e3a\u4e00\u4e2aUSB\u9a71\u52a8\u5668\uff0c\u53ef\u4ee5\u76f4\u63a5\u62d6\u653ePython\u4ee3\u7801\u5230\u9a71\u52a8\u5668\u4e2d\u3002<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># code.py<\/p>\n<p>import board<\/p>\n<p>import digitalio<\/p>\n<p>import time<\/p>\n<p>led = digitalio.DigitalInOut(board.D13)<\/p>\n<p>led.direction = digitalio.Direction.OUTPUT<\/p>\n<p>while True:<\/p>\n<p>    led.value = not led.value<\/p>\n<p>    time.sleep(0.5)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u73af\u5883<\/h3>\n<\/p>\n<p><p>\u5728\u66f4\u590d\u6742\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u4f8b\u5982\u4f7f\u7528ARM Cortex-M\u7cfb\u5217\u5fae\u63a7\u5236\u5668\uff0c\u53ef\u4ee5\u901a\u8fc7\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u73af\u5883\u5c06Python\u4ee3\u7801\u5199\u5165\u786c\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5\u5f00\u53d1\u73af\u5883<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5\u9002\u7528\u4e8e\u6240\u4f7f\u7528\u5fae\u63a7\u5236\u5668\u7684\u5f00\u53d1\u73af\u5883\uff0c\u4f8b\u5982Keil, IAR, STM32CubeIDE\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7f16\u5199\u5e76\u7f16\u8bd1\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u7f16\u5199Python\u4ee3\u7801\u5e76\u5c06\u5176\u8f6c\u6362\u6210C\u4ee3\u7801\u6216\u76f4\u63a5\u8c03\u7528Python\u89e3\u91ca\u5668\uff0c\u7f16\u8bd1\u540e\u751f\u6210\u56fa\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528MicroPython\u5c06Python\u4ee3\u7801\u8f6c\u6362\u6210C\u4ee3\u7801<\/p>\n<p>import pyb<\/p>\n<p>led = pyb.LED(1)<\/p>\n<p>while True:<\/p>\n<p>    led.toggle()<\/p>\n<p>    pyb.delay(500)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3\u3001\u70e7\u5f55\u56fa\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5f00\u53d1\u73af\u5883\u63d0\u4f9b\u7684\u5de5\u5177\u6216\u8005JTAG\/SWD\u63a5\u53e3\u5c06\u56fa\u4ef6\u70e7\u5f55\u5230\u5fae\u63a7\u5236\u5668\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u901a\u8fc7\u4e0e\u786c\u4ef6\u63a5\u53e3\u901a\u4fe1<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u901a\u8fc7Python\u4ee3\u7801\u4e0e\u786c\u4ef6\u63a5\u53e3\u901a\u4fe1\uff0c\u5c06\u6307\u4ee4\u6216\u6570\u636e\u53d1\u9001\u5230\u786c\u4ef6\u8bbe\u5907\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u4f7f\u7528\u4e32\u884c\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528PySerial\u5e93\uff0c\u901a\u8fc7\u4e32\u884c\u63a5\u53e3\u4e0e\u786c\u4ef6\u8fdb\u884c\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import serial<\/p>\n<p>ser = serial.Serial(&#39;\/dev\/ttyUSB0&#39;, 9600)<\/p>\n<p>while True:<\/p>\n<p>    ser.write(b&#39;Hello, hardware!&#39;)<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2\u3001\u4f7f\u7528I2C\/SPI\u901a\u4fe1<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528smbus2\u6216spidev\u5e93\uff0c\u901a\u8fc7I2C\u6216SPI\u63a5\u53e3\u4e0e\u786c\u4ef6\u901a\u4fe1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import smbus2<\/p>\n<p>import time<\/p>\n<p>bus = smbus2.SMBus(1)<\/p>\n<p>address = 0x48<\/p>\n<p>while True:<\/p>\n<p>    bus.write_byte(address, 0x01)<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528\u5355\u677f\u8ba1\u7b97\u673a\uff08\u5982Raspberry Pi\uff09<\/h3>\n<\/p>\n<p><p>\u5355\u677f\u8ba1\u7b97\u673a\uff08\u5982Raspberry Pi\uff09\u5177\u6709\u4e30\u5bcc\u7684\u786c\u4ef6\u63a5\u53e3\uff0c\u53ef\u4ee5\u76f4\u63a5\u8fd0\u884c\u6807\u51c6\u7684Python\u4ee3\u7801\uff0c\u5e76\u4e0e\u786c\u4ef6\u8fdb\u884c\u4ea4\u4e92\u3002<\/p>\n<\/p>\n<p><h4>1\u3001\u5b89\u88c5Raspberry Pi OS<\/h4>\n<\/p>\n<p><p>\u5b89\u88c5Raspberry Pi OS\uff0c\u5e76\u786e\u4fdd\u7cfb\u7edf\u4e2d\u5b89\u88c5\u4e86Python\u3002<\/p>\n<\/p>\n<p><h4>2\u3001\u7f16\u5199\u5e76\u8fd0\u884cPython\u4ee3\u7801<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528RPi.GPIO\u5e93\u6216\u5176\u4ed6\u786c\u4ef6\u63a5\u53e3\u5e93\u7f16\u5199\u4ee3\u7801\uff0c\u76f4\u63a5\u63a7\u5236\u786c\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>import time<\/p>\n<p>GPIO.setmode(GPIO.BCM)<\/p>\n<p>GPIO.setup(18, GPIO.OUT)<\/p>\n<p>while True:<\/p>\n<p>    GPIO.output(18, GPIO.HIGH)<\/p>\n<p>    time.sleep(1)<\/p>\n<p>    GPIO.output(18, GPIO.LOW)<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5c06Python\u4ee3\u7801\u6210\u529f\u5199\u5165\u548c\u8fd0\u884c\u5728\u5404\u79cd\u786c\u4ef6\u8bbe\u5907\u4e0a\uff0c\u4f7f\u5f97Python\u5728\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u4e2d\u53d8\u5f97\u66f4\u52a0\u7075\u6d3b\u548c\u5f3a\u5927\u3002\u9009\u62e9\u9002\u5408\u81ea\u5df1\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u786c\u4ef6\u5e73\u53f0\u548c\u5e94\u7528\u9700\u6c42\u6765\u51b3\u5b9a\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u7a0b\u5e8f\u5d4c\u5165\u5230\u786c\u4ef6\u8bbe\u5907\u4e2d\uff1f<\/strong><br \/>\u5c06Python\u7a0b\u5e8f\u5d4c\u5165\u786c\u4ef6\u8bbe\u5907\u901a\u5e38\u9700\u8981\u4f7f\u7528\u7279\u5b9a\u7684\u786c\u4ef6\u5e73\u53f0\uff0c\u5982\u6811\u8393\u6d3e\u6216Arduino\u3002\u901a\u8fc7\u5b89\u88c5Python\u89e3\u91ca\u5668\u6216\u4f7f\u7528MicroPython\u7b49\u8f7b\u91cf\u7ea7\u7248\u672c\uff0c\u53ef\u4ee5\u5728\u8fd9\u4e9b\u8bbe\u5907\u4e0a\u76f4\u63a5\u8fd0\u884cPython\u4ee3\u7801\u3002\u60a8\u9700\u8981\u6839\u636e\u76ee\u6807\u786c\u4ef6\u7684\u6587\u6863\uff0c\u8bbe\u7f6e\u5f00\u53d1\u73af\u5883\uff0c\u7f16\u5199\u4ee3\u7801\u5e76\u901a\u8fc7USB\u6216\u65e0\u7ebf\u8fde\u63a5\u5c06\u5176\u4e0a\u4f20\u5230\u8bbe\u5907\u4e2d\u3002<\/p>\n<p><strong>\u5728\u5c06Python\u7a0b\u5e8f\u5199\u5165\u786c\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5c06Python\u7a0b\u5e8f\u5199\u5165\u786c\u4ef6\u65f6\uff0c\u786e\u4fdd\u76ee\u6807\u786c\u4ef6\u652f\u6301Python\u8fd0\u884c\u73af\u5883\u3002\u6b64\u5916\uff0c\u7406\u89e3\u786c\u4ef6\u7684\u5904\u7406\u80fd\u529b\u548c\u5b58\u50a8\u9650\u5236\u662f\u5fc5\u8981\u7684\uff0c\u56e0\u4e3a\u67d0\u4e9b\u590d\u6742\u7684Python\u5e93\u53ef\u80fd\u65e0\u6cd5\u5728\u8d44\u6e90\u53d7\u9650\u7684\u8bbe\u5907\u4e0a\u8fd0\u884c\u3002\u8fd8\u8981\u8003\u8651\u7535\u6e90\u7ba1\u7406\u548c\u6e29\u5ea6\u63a7\u5236\uff0c\u786e\u4fdd\u786c\u4ef6\u5728\u8fd0\u884c\u65f6\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n<p><strong>\u5982\u4f55\u8c03\u8bd5\u5728\u786c\u4ef6\u4e2d\u8fd0\u884c\u7684Python\u7a0b\u5e8f\uff1f<\/strong><br \/>\u8c03\u8bd5\u5728\u786c\u4ef6\u4e2d\u8fd0\u884c\u7684Python\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u4e32\u53e3\u8fde\u63a5\u548c\u4f7f\u7528\u8c03\u8bd5\u5de5\u5177\u6765\u5b9e\u73b0\u3002\u8bb8\u591a\u5f00\u53d1\u677f\u652f\u6301\u4e32\u53e3\u8f93\u51fa\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e2d\u6dfb\u52a0\u6253\u5370\u8bed\u53e5\uff0c\u67e5\u770b\u8fd0\u884c\u72b6\u6001\u548c\u8f93\u51fa\u7ed3\u679c\u3002\u4f7f\u7528\u96c6\u6210\u5f00\u53d1\u73af\u5883\uff08IDE\uff09\u4e2d\u7684\u8c03\u8bd5\u529f\u80fd\u4e5f\u80fd\u5e2e\u52a9\u5feb\u901f\u5b9a\u4f4d\u95ee\u9898\u3002\u786e\u4fdd\u786c\u4ef6\u4e0e\u8ba1\u7b97\u673a\u7684\u8fde\u63a5\u7a33\u5b9a\uff0c\u4ee5\u4fbf\u53ca\u65f6\u83b7\u53d6\u53cd\u9988\u548c\u505a\u51fa\u8c03\u6574\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u7684\u7a0b\u5e8f\u5199\u5165\u786c\u4ef6\u4e2d\u7684\u65b9\u6cd5\u4e3b\u8981\u6709\u4ee5\u4e0b\u51e0\u79cd\uff1a\u4f7f\u7528MicroPython\u6216CircuitPython\u3001\u4f7f\u7528 [&hellip;]","protected":false},"author":3,"featured_media":1047301,"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\/1047284"}],"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=1047284"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047284\/revisions"}],"predecessor-version":[{"id":1047305,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1047284\/revisions\/1047305"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1047301"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1047284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1047284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1047284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}