无法将 RC522 RFID 模块连接到 Raspberry Pi 4
Unable to connect RC522 RFID module to Raspberry Pi 4
我根据 https://pimylifeup.com/raspberry-pi-rfid-rc522/ 将我的 RC522 RFID 模块连接到我的 Raspberry Pi 4
所以我们有 Write.py 和 Read.py:
Write.py:
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
text = input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()
和Read.py是:
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
id, text = reader.read()
print(id)
print(text)
finally:
GPIO.cleanup()
但是当我执行 sudo python3 Write.py
时出现这个错误:
Traceback (most recent call last):
File "Write.py", line 6, in <module>
reader = SimpleMFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 14, in __init__
self.READER = MFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py", line 130, in __init__
self.spi.open(bus, device)
而对于 Read.py 我们有几乎与 Write.py 相同的执行错误,如下所示:
Traceback (most recent call last):
File "Read.py", line 6, in <module>
reader = SimpleMFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 14, in __init__
self.READER = MFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py", line 130, in __init__
self.spi.open(bus, device)
FileNotFoundError: [Errno 2] No such file or directory
FileNotFoundError: [Errno 2] No such file or directory
我尝试了几种方法,但根本不起作用:
1-检查接线
2-使用 python2
3-使用 GUI 和 boot/config.txt 检查启用的 SPI
4-使用 sudo apt-get update
、 sudo apt-get upgrade
、 sudo apt-get install python3-dev python3-pip
和 sudo pip3 install spidev
我尝试lsmod |grep spi
检查,结果是:
spidev 20480 0
spi_bcm2835 24576 0
spi_bcm2835aux 16384 0
你觉得问题是什么?我的 Raspberry Pi 4 也有一个 3.5 英寸的触摸屏 LCD。这是 spi0 被保留的原因吗?如何解决?我什至不能使用 github.com 中的另一个 RC522 程序,它们在 spi.open(bus, device).
中有几乎相同的错误
我找到了简单的答案。只需重新安装 Raspbian OS 以重置 SPI 配置(为 3.5 英寸 LCD 设置),这样 RFID 写入和读取工作正常,一切正常。
我根据 https://pimylifeup.com/raspberry-pi-rfid-rc522/ 将我的 RC522 RFID 模块连接到我的 Raspberry Pi 4 所以我们有 Write.py 和 Read.py:
Write.py:
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
text = input('New data:')
print("Now place your tag to write")
reader.write(text)
print("Written")
finally:
GPIO.cleanup()
和Read.py是:
#!/usr/bin/env python
import RPi.GPIO as GPIO
from mfrc522 import SimpleMFRC522
reader = SimpleMFRC522()
try:
id, text = reader.read()
print(id)
print(text)
finally:
GPIO.cleanup()
但是当我执行 sudo python3 Write.py
时出现这个错误:
Traceback (most recent call last):
File "Write.py", line 6, in <module>
reader = SimpleMFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 14, in __init__
self.READER = MFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py", line 130, in __init__
self.spi.open(bus, device)
而对于 Read.py 我们有几乎与 Write.py 相同的执行错误,如下所示:
Traceback (most recent call last):
File "Read.py", line 6, in <module>
reader = SimpleMFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 14, in __init__
self.READER = MFRC522()
File "/usr/local/lib/python3.7/dist-packages/mfrc522/MFRC522.py", line 130, in __init__
self.spi.open(bus, device)
FileNotFoundError: [Errno 2] No such file or directory
FileNotFoundError: [Errno 2] No such file or directory
我尝试了几种方法,但根本不起作用:
1-检查接线
2-使用 python2
3-使用 GUI 和 boot/config.txt 检查启用的 SPI
4-使用 sudo apt-get update
、 sudo apt-get upgrade
、 sudo apt-get install python3-dev python3-pip
和 sudo pip3 install spidev
我尝试lsmod |grep spi
检查,结果是:
spidev 20480 0
spi_bcm2835 24576 0
spi_bcm2835aux 16384 0
你觉得问题是什么?我的 Raspberry Pi 4 也有一个 3.5 英寸的触摸屏 LCD。这是 spi0 被保留的原因吗?如何解决?我什至不能使用 github.com 中的另一个 RC522 程序,它们在 spi.open(bus, device).
中有几乎相同的错误我找到了简单的答案。只需重新安装 Raspbian OS 以重置 SPI 配置(为 3.5 英寸 LCD 设置),这样 RFID 写入和读取工作正常,一切正常。