使用Adafruit PN532库写入RFID标签的内存时,字节数组会被截断 [英] Byte array gets truncated when writing to memory of RFID tag using Adafruit PN532 library

查看:298
本文介绍了使用Adafruit PN532库写入RFID标签的内存时,字节数组会被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将字节数组从十六进制字符串转换为NTAG203 RFID标签的内存.我正在使用Raspberry Pi 3,PN532芯片和 Adafruit PN532 python库.

I'm trying to write a byte array that was converted from a hex string into the memory of an NTAG203 RFID tag. I'm using a Raspberry Pi 3, a PN532 chip and the Adafruit PN532 python library.

hex_string = '59e7168f267df300018e15b0'
formatted_hex = bytearray.fromhex(hex_string)

byte_data = bytearray(16)
byte_data[3:15] = formatted_hex

if not pn532.mifare_classic_write_block(4, byte_data):
    print('Error! Failed to write to the card.')
    sys.exit(-1)

当我再次执行pn532.mifare_classic_read_block(4)来从内存中读取值时,它会像这样出现:

When I do pn532.mifare_classic_read_block(4) to read the value from memory again it comes out like this:

print '0x{0}'.format(binascii.hexlify(<function call result>))
>>> 0x00000059440300fe0000000000000000

该值被截断并且具有尾随和前导零.这是怎么回事?

The value is chopped off and has trailing and leading zeroes. What is happening here?

我希望能够再次将值转换回十六进制,以使用它来搜索数据库.

I would like to be able to convert the value back into hex again to use it to search a database.

推荐答案

首先,名为mifare_classic_*的函数可能不太适合访问NTAG203标签,因为MIF​​ARE Classic和NTAG203使用偏离的帧和命令集.

First of all, functions named mifare_classic_* might not be well suited for accessing NTAG203 tags since MIFARE Classic and NTAG203 use deviating framing and command sets.

但是,PN532使用NTAG203标签在某种程度上支持的命令集来抽象对MIFARE Classic标签的访问.更具体地说,

However, the PN532 abstracts access to MIFARE Classic tags using a command set that is somewhat supported by NTAG203 tags. More specifically,

  • READ命令是相同的(从给定的块号开始都读取16个字节的数据).唯一的区别是与MIFARE Classic相比,NTAG203上的存储组织. MIFARE Classic每个块有16个字节,而NTAG203每个块只有4个字节.结果,READ命令返回4个连续的块.

  • the READ commands are identical (both read 16 bytes of data starting at a given block number). The only difference is the memory organization on NTAG203 compared to MIFARE Classic. While MIFARE Classic has 16 bytes per block, NTAG203 only has 4 bytes per block. As a result, the READ command returns 4 consecutive blocks.

NTAG203将MIFARE Classic WRITE命令支持为COMPATIBILITY WRITE c命令.唯一的区别是,您只能在NTAG203上写入4个字节(一个块).因此,由于您仍然必须在write命令中提供16个字节的数据,因此只写入前4个字节.

the MIFARE Classic WRITE command is supported as COMPATIBILITY WRITE c command by NTAG203. The only difference is that you can only write 4 bytes (one block) on NTAG203. Thus, since you still have to provide 16 bytes of data in the write command, only the first 4 bytes are written.

这正是您所观察到的:pn532.mifare_classic_write_block(4, byte_data)仅将byte_data的前4个字节写入块4.请注意,因为将formatted_hex复制到切片3:15,所以前4个字节是00 00 00 59.并将字节0..2保留为默认值0.由于READ命令读取16个字节(4个块,每个块4个字节),因此读取的数据还包含块5..7:440300fe0000000000000000的一些旧内存内容.该值看起来很合理,因为它与NTAG203上出厂设置的初始存储器内容相匹配.

And this is exactly what you observed: pn532.mifare_classic_write_block(4, byte_data) writes only the first 4 bytes of byte_data to block 4. Note that the first 4 bytes are 00 00 00 59 since you copy formatted_hex to the slice 3:15 and leave bytes 0..2 at their default value of 0. Since the READ command reads 16 bytes (4 blocks with 4 bytes each), the read data also contains some old memory content for blocks 5..7: 440300fe0000000000000000. That value looks reasonable since that matches the initial memory contents that's factory programmed on NTAG203.

这篇关于使用Adafruit PN532库写入RFID标签的内存时,字节数组会被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆