【问题标题】:Python - RFID read in a loopPython - 循环读取 RFID
【发布时间】:2019-02-19 22:34:36
【问题描述】:

我正在尝试编写一个 python 代码来读取我的 RFID 卡,如果他不会得到任何响应,则继续。

但是当他等待读者的重播时我被卡住了 是否有任何 python 命令这样说:

while reader.available()? 

这是我的代码(正在等待放置射频标签)

#!/usr/bin/env python

import RPi.GPIO as GPIO
import SimpleMFRC522
import time

reader = SimpleMFRC522.SimpleMFRC522 ()
OldText = ''

while True:
   # GPIO.cleanup()
    time.sleep(1)
    try:
        status,TagType = reader.read_no_block()
        print(status)
        if status == 'None':
            print ("No Card Found")
        elif status != 'None':
            id,text = reader.read()
            if text != OldText:
                print(text)
                OldText=text

            else:
                print ("Same card")

    finally:
        GPIO.cleanup()
    time.sleep(5) 

这是修复代码 - 但是在外壳上我没有得到“找不到卡” - 为什么? 这是我现在得到的输出:

675036844849
new test 13.9.18                                
None
Same card
MFRC522-python/ReadOnce.py:28: RuntimeWarning: No channels have been set up yet - nothing to clean up!  Try cleaning up at the end of your program instead!
  GPIO.cleanup()
None
Same card
None
Same card
None
Same card
None
White_Card                                      
None
Same card
None
Same card
None

如果 RFID 上没有卡 - 它会卡住.... 为什么?

【问题讨论】:

  • 你能再帮忙吗?我想我需要了解这部分 def read(self): id, text = self.read_no_block() while not id: id, text = self.read_no_block() return id, text - 但我需要从中得到什么?
  • 没有一个关键字。检查此链接以了解其用法 - educative.io/edpresso/what-is-the-none-keyword-in-python

标签: python rfid reader


【解决方案1】:

print (status == None) 将返回 true。

print (status == 'None') 将返回 false。

因此使用: if (status == none)

【讨论】:

  • 如果状态为无:会好很多
【解决方案2】:
while True:
    try:
        # your code here
    except:
        pass
GPIO.cleanup()

应该排序,如果没有,请告诉我。

【讨论】:

  • 还没有工作 - 当 RFID 附近没有任何东西时,我仍然没有得到“未找到卡” - 我得到打印“无”但不是“未找到卡”。怎么来的?
  • 在每一行之前添加日志,看看它在哪里呕吐。
  • 我该怎么做? ,这是我第一次体验python...谢谢,
【解决方案3】:

你好,我的朋友,我刚刚查看了代码,你的错误是使用 ,我花了一段时间才注意到这个错误,你应该只使用 。不同的是一个是字符串,而函数的返回值不是。

【讨论】:

    【解决方案4】:

    您可以为此使用pyembedded python 库,它可以在扫描 rfid 卡时为您提供标签 ID。

    from pyembedded.rfid_module.rfid import RFID
    rfid = RFID(port='COM3', baud_rate=9600)
    print(rfid.get_id())
    

    https://pypi.org/project/pyembedded/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-06
      • 2016-01-27
      • 1970-01-01
      • 2022-11-18
      相关资源
      最近更新 更多