{"id":1056494,"date":"2024-12-31T15:00:10","date_gmt":"2024-12-31T07:00:10","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1056494.html"},"modified":"2024-12-31T15:00:12","modified_gmt":"2024-12-31T07:00:12","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e5%85%a5rc522a","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1056494.html","title":{"rendered":"python\u5982\u4f55\u5199\u5165rc522a"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/dfcfa4b4-e85b-430f-8f5d-acbb73c182ee.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u5199\u5165rc522a\" \/><\/p>\n<p><p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5199\u5165RC522<\/strong><\/p>\n<\/p>\n<p><p><strong>\u8981\u5728Python\u4e2d\u5199\u5165RC522\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5e93\u3001\u914d\u7f6e\u786c\u4ef6\u8fde\u63a5\u3001\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u5199\u5165\u64cd\u4f5c\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u5199\u5165\u7ed3\u679c\u3002<\/strong> \u5176\u4e2d\uff0c\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5e93\u662f\u6700\u91cd\u8981\u7684\u6b65\u9aa4\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5e93<\/h3>\n<\/p>\n<p><p>\u5728\u5f00\u59cb\u7f16\u5199\u4ee3\u7801\u4e4b\u524d\uff0c\u9700\u8981\u786e\u4fdd\u5df2\u7ecf\u5b89\u88c5\u4e86\u9002\u5f53\u7684\u8f6f\u4ef6\u5e93\u3002\u5bf9\u4e8eRC522\u6a21\u5757\uff0c\u6211\u4eec\u901a\u5e38\u4f7f\u7528 <code>MFRC522<\/code> \u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e0eRC522\u8fdb\u884c\u901a\u4fe1\u7684\u57fa\u672c\u529f\u80fd\u3002<\/p>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5b89\u88c5MFRC522\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u5b89\u88c5 <code>MFRC522<\/code> \u5e93\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install mfrc522<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u4e2a\u5e93\u63d0\u4f9b\u4e86\u4e0eRC522\u8fdb\u884c\u901a\u4fe1\u7684\u57fa\u672c\u529f\u80fd\uff0c\u786e\u4fdd\u5b89\u88c5\u6210\u529f\u540e\u624d\u80fd\u8fdb\u884c\u540e\u7eed\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u5b89\u88c5\u5176\u4ed6\u4f9d\u8d56\u5e93<\/strong><\/p>\n<\/p>\n<p><p>\u9664\u4e86 <code>MFRC522<\/code> \u5e93\uff0c\u8fd8\u9700\u8981\u5b89\u88c5 <code>spidev<\/code> \u5e93\u6765\u652f\u6301SPI\u901a\u4fe1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">sudo pip install spidev<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e8c\u3001\u914d\u7f6e\u786c\u4ef6\u8fde\u63a5<\/h3>\n<\/p>\n<p><p>\u786e\u4fddRC522\u8bfb\u5361\u5668\u6a21\u5757\u4e0eRaspberry Pi\u6b63\u786e\u8fde\u63a5\u3002\u4ee5\u4e0b\u662f\u5e38\u89c1\u7684\u8fde\u63a5\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>SDA<\/strong> (CS): \u63a5\u5230GPIO 24 (Pin 18)<\/li>\n<li><strong>SCK<\/strong>: \u63a5\u5230GPIO 11 (Pin 23)<\/li>\n<li><strong>MOSI<\/strong>: \u63a5\u5230GPIO 10 (Pin 19)<\/li>\n<li><strong>MISO<\/strong>: \u63a5\u5230GPIO 9 (Pin 21)<\/li>\n<li><strong>GND<\/strong>: \u63a5\u5230GND (Pin 6)<\/li>\n<li><strong>RST<\/strong>: \u63a5\u5230GPIO 25 (Pin 22)<\/li>\n<li><strong>3.3V<\/strong>: \u63a5\u52303.3V (Pin 1)<\/li>\n<\/ul>\n<p><p>\u786e\u4fdd\u6240\u6709\u8fde\u63a5\u90fd\u7262\u56fa\uff0c\u5e76\u4e14Raspberry Pi\u5df2\u5f00\u542fSPI\u63a5\u53e3\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u7f16\u5199\u4ee3\u7801\u5b9e\u73b0\u5199\u5165\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u7b80\u5355\u7684Python\u4ee3\u7801\u793a\u4f8b\uff0c\u7528\u4e8e\u5728RC522\u4e0a\u5199\u5165\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>from mfrc522 import SimpleMFRC522<\/p>\n<h2><strong>\u521d\u59cb\u5316RC522\u6a21\u5757<\/strong><\/h2>\n<p>reader = SimpleMFRC522()<\/p>\n<p>try:<\/p>\n<p>    # \u7b49\u5f85\u7528\u6237\u8f93\u5165\u8981\u5199\u5165\u7684\u6570\u636e<\/p>\n<p>    text = input(&#39;Enter text to write to the RFID tag: &#39;)<\/p>\n<p>    print(&quot;Now place your tag to write&quot;)<\/p>\n<p>    # \u5199\u5165\u6570\u636e<\/p>\n<p>    reader.write(text)<\/p>\n<p>    print(&quot;Written successfully&quot;)<\/p>\n<p>finally:<\/p>\n<p>    GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6d4b\u8bd5\u548c\u9a8c\u8bc1\u5199\u5165\u7ed3\u679c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u8fd0\u884c\u4ee3\u7801<\/strong><\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd0\u884c\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728RFID\u6807\u7b7e\u4e0a\u5199\u5165\u6570\u636e\u3002\u786e\u4fdd\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\uff0cRFID\u6807\u7b7e\u5df2\u7ecf\u653e\u7f6e\u5728RC522\u6a21\u5757\u7684\u8bfb\u53d6\u533a\u57df\u5185\u3002<\/p>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u7f16\u5199\u4e00\u4e2a\u7b80\u5355\u7684\u8bfb\u53d6\u7a0b\u5e8f\u6765\u9a8c\u8bc1\u5199\u5165\u7684\u6570\u636e\u662f\u5426\u6b63\u786e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>from mfrc522 import SimpleMFRC522<\/p>\n<p>reader = SimpleMFRC522()<\/p>\n<p>try:<\/p>\n<p>    print(&quot;Now place your tag to read&quot;)<\/p>\n<p>    id, text = reader.read()<\/p>\n<p>    print(&quot;ID: %s\\nText: %s&quot; % (id, text))<\/p>\n<p>finally:<\/p>\n<p>    GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd0\u884c\u8fd9\u4e2a\u8bfb\u53d6\u7a0b\u5e8f\uff0c\u786e\u4fdd\u80fd\u591f\u6b63\u786e\u8bfb\u53d6\u4e4b\u524d\u5199\u5165\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<\/li>\n<\/ol>\n<p><h3>\u4e94\u3001\u5e38\u89c1\u95ee\u9898\u89e3\u51b3<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u65e0\u6cd5\u68c0\u6d4b\u5230RC522\u6a21\u5757<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u68c0\u67e5SPI\u63a5\u53e3\u662f\u5426\u5df2\u5728Raspberry Pi\u4e0a\u542f\u7528\u3002<\/li>\n<li>\u786e\u8ba4\u8fde\u63a5\u7ebf\u662f\u5426\u6b63\u786e\u8fde\u63a5\u548c\u7262\u56fa\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5199\u5165\u6570\u636e\u5931\u8d25<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fddRFID\u6807\u7b7e\u5728\u5199\u5165\u65f6\u6b63\u786e\u653e\u7f6e\u5728RC522\u6a21\u5757\u7684\u8bfb\u53d6\u533a\u57df\u5185\u3002<\/li>\n<li>\u68c0\u67e5\u4ee3\u7801\u4e2d\u662f\u5426\u6709\u9519\u8bef\uff0c\u7279\u522b\u662f\u4e0eGPIO\u76f8\u5173\u7684\u914d\u7f6e\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u8bfb\u53d6\u6570\u636e\u4e0d\u4e00\u81f4<\/strong><\/p>\n<\/p>\n<ul>\n<li>\u786e\u4fdd\u8bfb\u53d6\u7a0b\u5e8f\u4e0e\u5199\u5165\u7a0b\u5e8f\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u7f16\u7801\u3002<\/li>\n<li>\u68c0\u67e5RFID\u6807\u7b7e\u662f\u5426\u635f\u574f\u6216\u6709\u5176\u4ed6\u95ee\u9898\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p><h3>\u516d\u3001\u8fdb\u9636\u64cd\u4f5c<\/h3>\n<\/p>\n<ol>\n<li>\n<p><strong>\u5199\u5165\u7279\u5b9a\u6247\u533a<\/strong><\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5199\u5165\u7279\u5b9a\u6247\u533a\u7684\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528\u66f4\u5e95\u5c42\u7684MFRC522\u5e93\u8fdb\u884c\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>from mfrc522 import MFRC522<\/p>\n<p>MIFAREReader = MFRC522()<\/p>\n<p>def write_block(block_num, data):<\/p>\n<p>    # Scan for cards    <\/p>\n<p>    status, TagType = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)<\/p>\n<p>    # Get the UID of the card<\/p>\n<p>    status, uid = MIFAREReader.MFRC522_Anticoll()<\/p>\n<p>    if status == MIFAREReader.MI_OK:<\/p>\n<p>        # Select the scanned tag<\/p>\n<p>        MIFAREReader.MFRC522_SelectTag(uid)<\/p>\n<p>        # Authenticate<\/p>\n<p>        key = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]<\/p>\n<p>        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, block_num, key, uid)<\/p>\n<p>        if status == MIFAREReader.MI_OK:<\/p>\n<p>            MIFAREReader.MFRC522_Write(block_num, data)<\/p>\n<p>            print(&quot;Data written to block&quot;, block_num)<\/p>\n<p>        else:<\/p>\n<p>            print(&quot;Authentication error&quot;)<\/p>\n<h2><strong>Example usage<\/strong><\/h2>\n<p>write_block(8, [0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00])<\/p>\n<p>GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<li>\n<p><strong>\u9519\u8bef\u5904\u7406<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u95ee\u9898\uff0c\u5982\u786c\u4ef6\u6545\u969c\u3001\u901a\u4fe1\u9519\u8bef\u7b49\u3002\u5728\u7f16\u5199\u4ee3\u7801\u65f6\uff0c\u52a0\u5165\u9002\u5f53\u7684\u9519\u8bef\u5904\u7406\u673a\u5236\u53ef\u4ee5\u63d0\u9ad8\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import RPi.GPIO as GPIO<\/p>\n<p>from mfrc522 import SimpleMFRC522<\/p>\n<p>reader = SimpleMFRC522()<\/p>\n<p>try:<\/p>\n<p>    text = input(&#39;Enter text to write to the RFID tag: &#39;)<\/p>\n<p>    print(&quot;Now place your tag to write&quot;)<\/p>\n<p>    reader.write(text)<\/p>\n<p>    print(&quot;Written successfully&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(&quot;An error occurred:&quot;, str(e))<\/p>\n<p>finally:<\/p>\n<p>    GPIO.cleanup()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728Python\u4e2d\u6210\u529f\u5b9e\u73b0\u5bf9RC522\u6a21\u5757\u7684\u5199\u5165\u64cd\u4f5c\u3002\u786e\u4fdd\u786c\u4ef6\u8fde\u63a5\u6b63\u786e\u3001\u4ee3\u7801\u7f16\u5199\u65e0\u8bef\uff0c\u5e76\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u548c\u9a8c\u8bc1\uff0c\u4ee5\u786e\u4fdd\u7cfb\u7edf\u7684\u7a33\u5b9a\u6027\u548c\u53ef\u9760\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u4e0eRC522A\u6a21\u5757\u8fdb\u884c\u901a\u4fe1\uff1f<\/strong><br \/>\u4f7f\u7528Python\u4e0eRC522A\u6a21\u5757\u8fdb\u884c\u901a\u4fe1\uff0c\u901a\u5e38\u9700\u8981\u5b89\u88c5\u76f8\u5173\u7684\u5e93\uff0c\u5982<code>MFRC522<\/code>\u5e93\u3002\u53ef\u4ee5\u901a\u8fc7<code>pip install mfrc522<\/code>\u547d\u4ee4\u6765\u5b89\u88c5\u3002\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165\u5e93\u5e76\u521d\u59cb\u5316RC522A\u6a21\u5757\u6765\u5f00\u59cb\u8fdb\u884c\u8bfb\u5199\u64cd\u4f5c\u3002\u5177\u4f53\u4ee3\u7801\u793a\u4f8b\u53ef\u4ee5\u5728\u5e93\u7684\u6587\u6863\u4e2d\u627e\u5230\u3002<\/p>\n<p><strong>RC522A\u6a21\u5757\u53ef\u4ee5\u5199\u5165\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\uff1f<\/strong><br \/>RC522A\u6a21\u5757\u53ef\u4ee5\u5199\u5165\u591a\u79cd\u7c7b\u578b\u7684\u6570\u636e\uff0c\u5305\u62ec\u6587\u672c\u4fe1\u606f\u3001\u7f51\u5740\u3001\u8eab\u4efd\u8bc6\u522b\u4fe1\u606f\u7b49\u3002\u60a8\u53ef\u4ee5\u6839\u636e\u5e94\u7528\u573a\u666f\u9009\u62e9\u9002\u5408\u7684\u6570\u636e\u683c\u5f0f\u8fdb\u884c\u5199\u5165\u3002\u901a\u5e38\uff0c\u6570\u636e\u5c06\u4ee5\u5b57\u8282\u5f62\u5f0f\u5b58\u50a8\u5728NFC\u6807\u7b7e\u4e2d\u3002<\/p>\n<p><strong>\u5728\u4f7f\u7528Python\u8fdb\u884c\u5199\u5165\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u4f7f\u7528Python\u5199\u5165RC522A\u6a21\u5757\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u8fde\u63a5\u6b63\u786e\u4e14\u7535\u6e90\u7a33\u5b9a\u3002\u6b64\u5916\uff0c\u5199\u5165\u64cd\u4f5c\u4f1a\u8986\u76d6\u6807\u7b7e\u4e0a\u7684\u539f\u6709\u6570\u636e\uff0c\u56e0\u6b64\u5728\u5199\u5165\u4e4b\u524d\uff0c\u6700\u597d\u5148\u8bfb\u53d6\u6807\u7b7e\u5185\u5bb9\u4ee5\u5907\u4efd\u3002\u540c\u65f6\uff0c\u786e\u4fdd\u4f7f\u7528\u5408\u9002\u7684\u6743\u9650\u8bbe\u7f6e\uff0c\u4ee5\u907f\u514d\u6570\u636e\u4e22\u5931\u6216\u635f\u574f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5982\u4f55\u5728Python\u4e2d\u5199\u5165RC522 \u8981\u5728Python\u4e2d\u5199\u5165RC522\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\uff1a\u5b89\u88c5\u5fc5\u8981\u7684\u8f6f\u4ef6\u5e93\u3001\u914d\u7f6e [&hellip;]","protected":false},"author":3,"featured_media":1056498,"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\/1056494"}],"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=1056494"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056494\/revisions"}],"predecessor-version":[{"id":1056503,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1056494\/revisions\/1056503"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1056498"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1056494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1056494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1056494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}