简体   繁体   English

Python pyscard 控制NFC读卡器的蜂鸣器

[英]Python pyscard control the buzzer of an NFC reader

I have two NFC reading devices: ACR12U and ACR1252U我有两个 NFC 读取设备:ACR12U 和 ACR1252U

I have a short script in python using pyscard to read the UIDs of NFC tags:我在 python 中有一个简短的脚本,使用 pyscard 读取 NFC 标签的 UID:

from smartcard.CardMonitoring import CardMonitor, CardObserver
from smartcard.util import toHexString
from smartcard.Exceptions import CardConnectionException

getuid = [0xFF, 0xCA, 0x00, 0x00, 0x00]

class transmitobserver(CardObserver):
    def update(self, observable, actions):
        (addedcards, removedcards) = actions
        for card in addedcards:
            card.connection = card.createConnection()
            try:
                card.connection.connect()
            except CardConnectionException:
                # print("Card was removed too fast!")
                return
            response, sw1, sw2 = card.connection.transmit(
                getuid)
            nfc_id = "{}".format(toHexString(response)).replace(" ", "").lower()
            return nfc_id

if __name__ == '__main__':
    cardmonitor = CardMonitor()
    cardobserver = transmitobserver()
    cardmonitor.addObserver(cardobserver)
    try:
        while True:
            pass
    except KeyboardInterrupt:
        pass

The ACR122U buzzes as soon as I an NFC card is in proximity.只要靠近 NFC 卡,ACR122U 就会发出嗡嗡声。 The ACR1252U buzzes when an NFC is in proximity and buzzes again when I remove it. ACR1252U 在靠近 NFC 时会发出嗡嗡声,当我移除它时会再次发出嗡嗡声。

I would like to change this behaviour so they only buzz when I remove the card and the card has been read.我想改变这种行为,这样它们只会在我取出卡片并且卡片已被读取时才会发出嗡嗡声。 I tested it out and the program fails if I remove a card too fast, but the reader still buzzes like it was a success.我对它进行了测试,如果我取出卡片的速度太快,程序就会失败,但读卡器仍然嗡嗡作响,就像它成功了一样。

This is the first time I am working with things like this so I tried some trial and error methods.这是我第一次处理这样的事情,所以我尝试了一些试错法。

So I figured getuid = [0xFF, 0xCA, 0x00, 0x00, 0x00] is sending the reader a command to read the UID, which I was able to find in both of their manuals:所以我认为getuid = [0xFF, 0xCA, 0x00, 0x00, 0x00]正在向读者发送一个读取 UID 的命令,我可以在他们的两本手册中找到该命令:

在此处输入图像描述

Now I thought I could use this to control the buzzer.现在我想我可以用它来控制蜂鸣器。 In the manual for the ACR1252U I found this:在 ACR1252U 的手册中,我发现了这个:

在此处输入图像描述

So I tried the following:所以我尝试了以下方法:

buzzer = [0xE0, 0x00, 0x00, 0x28, 0x01, 0xFF]
response = card.connection.transmit(buzzer)

But I get an error.但我得到一个错误。 There is a different instruction for the ACR122U, but same thing happens. ACR122U 有不同的指令,但同样的事情发生。 Can anyone aid me what am I doing wrong?谁能帮助我我做错了什么?

Two pointers两个指针

1)All commands sent using Command APDU's have a standard Response APDU format of Result (If available - most do have a Result), SW1 and SW2 1)使用命令 APDU 发送的所有命令都有一个标准的响应 APDU 格式的Result (如果可用 - 大多数都有一个结果), SW1SW2

So you should use the code response, sw1, sw2 = card.connection.transmit(buzzer)所以你应该使用代码response, sw1, sw2 = card.connection.transmit(buzzer)

You should always check that SW1 = 90h and SW2 = 00h to confirm the command was successful (Other values will indicate an error, see the datasheet for more details of what the codes mean)您应始终检查SW1 = 90hSW2 = 00h以确认命令成功(其他值将指示错误,有关代码含义的更多详细信息,请参见数据表)

2)You say you want it to buzz when it successful read the card, but you don't check the SW1 and SW2 values for success when you read the UID. 2)你说你希望它在读卡成功时发出嗡嗡声,但你在读 UID 时没有检查SW1SW2值是否成功。

3)The command buzzer = [0xE0, 0x00, 0x00, 0x28, 0x01, 0xFF] is to manually turn the buzzer on, great for when you manually want to indicate success after checking in the SW1 and SW2 values of the read but this command does not change the default buzzing behaviour of the reader. 3) 命令buzzer = [0xE0, 0x00, 0x00, 0x28, 0x01, 0xFF]是手动打开蜂鸣器,非常适合当您在检查读取的SW1SW2值后手动想要指示成功但此命令不会改变阅读器的默认蜂鸣行为。

On the ACR1253U configBuzzer = [0xE0, 0x00, 0x00, 0x21, 0x01, 0x67] should be the right bits to turn off buzzing on Card Insertion Events and Card Removal Events, which is what you described you wanted, so you can manually trigger the buzzer on success (Again check SW1 and SW2 for success)在 ACR1253U 上configBuzzer = [0xE0, 0x00, 0x00, 0x21, 0x01, 0x67]应该是关闭卡插入事件和卡移除事件的嗡嗡声的正确位,这是您所描述的您想要的,因此您可以手动触发成功蜂鸣器(再次检查SW1SW2是否成功)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM