基于Python pyscard库采集ACS ACR122U NFC读卡器数据的详细操作步骤

步骤1:安装驱动

1. 下载驱动:

   - 访问ACS官网的驱动下载页面:[ACR122U驱动下载](https://www.acs.com.hk/en/drivers/6/acr122u-nfc-reader/)。

   - 选择适用于Windows的驱动(如 ACR122U Driver (Windows) V3.05.02.zip)。

2. 安装驱动:

   - 解压下载的压缩包,运行 Setup.exe。

   - 按照向导完成安装。

   - 插入ACR122U读卡器,Windows会自动识别并加载驱动。

3. 验证设备识别:

   - 打开 设备管理器,检查是否有 ACS ACR122U PICC Interface或类似设备,无感叹号即表示驱动正常。

步骤2:安装Python库

使用 pyscard 库(基于PC/SC标准):

pip install pyscard

步骤3:编写Python代码

# 基础代码(读取卡片UID)

from smartcard.System import readers

from smartcard.util import toHexString

# 获取所有读卡器列表

reader_list = readers()

if not reader_list:

    print("未检测到读卡器!请检查设备连接。")

    exit()

# 选择第一个读卡器(通常为ACR122U)

reader = reader_list[0]

print("已连接读卡器:", reader)

# 建立连接

connection = reader.createConnection()

try:

    connection.connect()

    print("读卡器连接成功!")

except Exception as e:

    print("连接失败:", e)

    exit()

# 定义获取UID的APDU指令(ACS ACR122U专用)

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

# 发送指令并获取响应

data, sw1, sw2 = connection.transmit(GET_UID_APDU)

# 检查响应状态码

if sw1 == 0x90 and sw2 == 0x00:

    print("卡片UID:", toHexString(data))

else:

    print("读取失败,状态码:", hex(sw1), hex(sw2))

 

步骤4:读取MIFARE Classic卡片数据

# 定义块号和密钥(默认密钥为6个0xFF)

BLOCK_NUMBER = 4

KEY = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF]

# 1. 发送认证指令

AUTH_APDU = [

    0xFF, 0x86, 0x00, 0x00,

    0x05, 0x01, 0x00, BLOCK_NUMBER, 0x60, 0x00

] + KEY

# 合并密钥到指令

auth_data, auth_sw1, auth_sw2 = connection.transmit(AUTH_APDU)

if auth_sw1 != 0x90:

    print("认证失败!请检查密钥或块号。")

    exit()

# 2. 读取块数据

READ_APDU = [0xFF, 0xB0, 0x00, BLOCK_NUMBER, 0x10]

data, sw1, sw2 = connection.transmit(READ_APDU)

if sw1 == 0x90:

    print(f"块 {BLOCK_NUMBER} 数据:", toHexString(data))

else:

    print("读取失败,状态码:", hex(sw1), hex(sw2))

步骤5:轮询检测卡片(持续监控)

import time

while True:

    try:

        # 重新连接读卡器(检测卡片状态变化)

        connection.reconnect()

        data, sw1, sw2 = connection.transmit(GET_UID_APDU)

        if sw1 == 0x90:

            print("检测到卡片,UID:", toHexString(data))

        else:

            print("未检测到卡片")

    except Exception as e:

        print("通信错误:", e)

    time.sleep(1) # 每秒检测一次

 

完整示例代码

# 保存为 `acr122u_read.py`

from smartcard.System import readers

from smartcard.util import toHexString

import time

def main():

    # 获取读卡器

    reader_list = readers()

    if not reader_list:

        print("未检测到读卡器!")

        return

    reader = reader_list[0]

    print("已连接读卡器:", reader)

    # 连接读卡器

    connection = reader.createConnection()

    try:

        connection.connect()

    except Exception as e:

        print("连接失败:", e)

        return

    # 持续轮询检测卡片

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

    while True:

        try:

            connection.reconnect()

            data, sw1, sw2 = connection.transmit(GET_UID_APDU)

            if sw1 == 0x90 and sw2 == 0x00:

                print("检测到卡片,UID:", toHexString(data))

            else:

                print("等待卡片靠近...")

            time.sleep(1)

        except KeyboardInterrupt:

            print("已退出")

            break

        except Exception as e:

            print("错误:", e)

if __name__ == "__main__":

    main()

运行方法

1. 保存代码为 .py文件(如 nfc_reader.py)。

2. 打开命令行,运行:

   python nfc_reader.py

3. 将NFC卡片靠近读卡器,观察输出结果。

通过以上步骤,你可以在Windows系统下使用Python成功采集ACS ACR122U NFC读卡器的数据!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mosquito_lover1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值