我正在尝试使用Python脚本将Raspberry Pi与USB RFID读卡器接口。作为记录,我的代码如下:
import serial, time, sys
import sqlite3 as lite
reader = "/dev/ttyUSB0"
ser = serial.Serial(reader, timeout=1)
print("Connected to RFID reader on:", reader)
try:
while True:
thetime = time.strftime("%Y-%m-%d %a %H:%M:%S", time.localtime())
ser.flushInput() # flush any extra data from the serial port
rfid_data = ser.readline().strip()
if len(rfid_data) > 0:
rfid_data = rfid_data[1:11]
print("Card scanned", rfid_data)
else:
print("Card not found")
except:
print("Unexpected error")
finally:
ser.close()
此刻,我实际上收到了多个错误。我相信我不会用自己的语言来尽力解释它,而是确保以下片段对您中的大多数人更加有用。
Traceback (most recent call last): File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 275, in open self.fd = os.open(self.portstr, os.O_RDWR|os.O_NOCTTY|os.O_NONBLOCK) FileNotFoundError: [Errno 2] No such file or directory: '/dev/ttyUSB0' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/home/pi/Desktop/Alex Panagis/rfidtestfinal.py", line 5, in <module> ser = serial.Serial(reader, timeout=1) File "/usr/lib/python3/dist-packages/serial/serialutil.py", line 261, in __init__ self.open() File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 278, in open raise SerialException("could not open port %s: %s" % (self._port, msg)) serial.serialutil.SerialException: could not open port /dev/ttyUSB0: [Errno 2] No such file or directory: '/dev/ttyUSB0'
对于解决此问题的任何帮助,我们深表感谢:)