简体   繁体   English

使用 Raspberry Pi 将 NDEF 写入 Mifare S50

[英]Writing NDEF to Mifare S50 with Raspberry Pi

I am trying to write NDEF data to an NFC Tag (Mifare S50 chip) so it can work with my iPhone, I'm using a Raspberry Pi 4 with a RC522 NFC module connected via GPIO and using SPI.我正在尝试将 NDEF 数据写入 NFC 标签(Mifare S50 芯片),以便它可以与我的 iPhone 一起使用,我正在使用 Raspberry Pi 4 和通过 GPIO 连接的 RC522 NFC 模块并使用 SPI。

I tried a few approaches to this, and ended up with the code above which didn't seem to write anything to the tag, and it's indeed not readable by my phone, and I think I might be messing up somewhere.我尝试了一些方法,最后得到了上面的代码,它似乎没有向标签写入任何内容,而且我的手机确实无法读取它,我想我可能在某个地方搞砸了。

Here's the code:这是代码:

#!/usr/bin/env python

import RPi.GPIO as GPIO
import ndef
from mfrc522 import SimpleMFRC522

reader = SimpleMFRC522()

try:
        message = [ndef.TextRecord("Hello"), ndef.TextRecord("World")]
        print("Approach a tag to the reader")
        reader.write(b''.join(ndef.message_encoder(message)))
        print("Written")
finally:
        GPIO.cleanup()

And this is the console output:这是控制台 output:

Traceback (most recent call last):
  File "Write.py", line 12, in <module>
    reader.write(b''.join(ndef.message_encoder(message)))
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 62, in write
    id, text_in = self.write_no_block(text)
  File "/usr/local/lib/python3.7/dist-packages/mfrc522/SimpleMFRC522.py", line 78, in write_no_block
    data.extend(bytearray(text.ljust(len(self.BLOCK_ADDRS) * 16).encode('ascii')))
AttributeError: 'bytes' object has no attribute 'encode'

There are two problems with this code.这段代码有两个问题。

1)You have created a "stream of bytes" of the ndef message but the SimpleMFRC522 package expects a Unicode String and encodes it to convert it to a "stream of bytes", so basically as it is already a "stream of bytes" it cannot be double encoded. 1)您已经创建了 ndef 消息的“字节流”,但 SimpleMFRC522 package 需要一个 Unicode 字符串并将其编码以将其转换为“字节流”,所以基本上因为它已经是“字节流”了不能双重编码。

2)The Mifare S50 chip is a Mifare Classic family chip and these are a proprietary format and don't conform to the NFC Forum Standards and thus don't have a Standards defined way to store Ndef data on them (You need to do more than write a raw set of bytes to the memory to chip for it to be recognised as the data being the Ndef format of data). 2)Mifare S50 芯片是 Mifare Classic 系列芯片,这些是专有格式,不符合 NFC 论坛标准,因此没有标准定义的方式来存储 Ndef 数据(您需要做更多而不是将一组原始字节写入 memory 到芯片中,以便将其识别为 Ndef 格式的数据)。

For NFC standard compliant/compatible chips there are a number of defined types of how the Ndef data is stored on each type.对于 NFC 标准兼容/兼容的芯片,有许多定义的类型来说明 Ndef 数据如何存储在每种类型上。

While NXP has defined a proprietary way to store Ndef data on a Mifare Classic chip, but the "SimpleMFRC522" is very simple indeed and does not have the code to write the Standard Ndef messages never mind the non standard proprietary methods.虽然 NXP 已经定义了一种在 Mifare Classic 芯片上存储 Ndef 数据的专有方法,但“SimpleMFRC522”确实非常简单,没有编写标准 Ndef 消息的代码,更不用说非标准的专有方法。

I also note that while the iPhone hardware can read Mifare Classic cards at the low level, I'm not sure it has implemented the Ndef data format on top as well.我还注意到,虽然 iPhone 硬件可以在底层读取 Mifare Classic 卡,但我不确定它是否也实现了 Ndef 数据格式。 I do know that some Android phones don't support the proprietary Mifare Classic Hardware at all.我知道有些 Android 手机根本不支持专有的 Mifare Classic 硬件。

I suggest you read https://www.nxp.com/docs/en/application-note/AN1305.pdf as this is the proprietary format definition on how to store NDEF data on these cards我建议您阅读https://www.nxp.com/docs/en/application-note/AN1305.pdf因为这是关于如何在这些卡上存储 NDEF 数据的专有格式定义

Unfortunately the RC522 is very old and has limited capabilities and thus is not well supported by more capable Python NFC modules.不幸的是,RC522 非常老旧,功能有限,因此功能更强大的 Python NFC 模块无法很好地支持。

You might be better off with https://github.com/ondryaso/pi-rc522 as that at least allows you to write raw bytes.使用https://github.com/ondryaso/pi-rc522可能会更好,因为这至少允许您写入原始字节。

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

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