我希望能够将 NDEF 格式的 URI 发送到我的 Arduino UNO,它连接到索尼的 RC-S801 动态 NFC 标签。我使用 Hercules 设置实用程序发送
"100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"
作为 http://www.stuttgart.sony.de 的十六进制字符串而且我能够从我的支持 NFC 的手机上读取它。但是当我使用以下 python 代码时,它不起作用:
import serial
import time
arduino = serial.Serial('COM6', 115200)
arduino.parity = 'M'
print arduino
print("writing")
input = ("100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000")
arduino.write(input)
time.sleep(5)
print("stopped writing")
arduino.close()
它显示为空记录。任何人都可以建议更改我的 python 代码以检测 NDEF 消息吗?这是我从手机读取标签时收到的消息的屏幕截图:
这是我的 arduino 代码:
#include <EEPROM.h>
#define BUTTON_PIN 8 // Arduino Digital I/O pin where button is connected
#define NUM_OF_BLOCKS 14 // capacity of NFC Tag in 16-byte blocks
#include "FeliCaPlug.h"
#include <avr/pgmspace.h> <
#include <inttypes.h>
FeliCaPlug plug;
volatile byte dataStorage[NUM_OF_BLOCKS*16] = {
// TT3 Attribute block + NDEF Message Text Message
0x10, 0x04, 0x01, 0x00, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x26,
0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
};
uint8_t blockData[NUM_OF_BLOCKS*16];
volatile uint8_t blinkCount = 1;
uint16_t numOfBlocks = NUM_OF_BLOCKS;
volatile boolean dataChanged = false;
void setup()
{
Serial.begin(115200);
uint8_t dfc[2] = {0xff, 0xe0};
uint8_t userParam[4] = {0x01, 0x23, 0x45, 0x67};
plug.initPort();
plug.setParam(FELICA_PLUG_MODE_TYPE3, dfc, userParam);
plug.setLEDPin(FELICA_PLUG_LED_PIN);
for(int i = 0; i< NUM_OF_BLOCKS*16; i++)
blockData[i] = dataStorage[i];
plug.setBlockData(blockData, numOfBlocks, 0);
plug.setFTWriteCallback(ftWriteCallback); // initialize write callback
}
void loop()
{
getFromUart();
plug.doLoop();
Serial.print(":");
if(dataChanged){
while(dataChanged)
{
Serial.println("NFC WRITING IN PROGRESS");
// if the WriteF flag is OFF, writing type3 tag data has completed
if(blockData[9] == 0x00)
{
Serial.println("NFC WRITING IS DONE"); //let's print complete memory written by external writer
Serial.println("Recieved data from External writer:");
Serial.println("<RAW_MEM>");
for(int i = 0; i< NUM_OF_BLOCKS*16; i++)
{
Serial.print(blockData[i], HEX);
}
Serial.println("</RAW_MEM>");
}
//let's blink
digitalWrite(FELICA_PLUG_LED_PIN, LOW);
delay(50);
digitalWrite(FELICA_PLUG_LED_PIN, HIGH);
if(--blinkCount==0) dataChanged = false;
}
digitalWrite(FELICA_PLUG_LED_PIN, HIGH);
delay(50);
}
}
byte index = 0;
void getFromUart()
{
if(Serial.available() > 0)
{
while(Serial.available() > 0)
{
byte data = Serial.read();
dataStorage[index] = data;
blockData[index] = dataStorage[index];
plug.setBlockData(blockData, numOfBlocks, 0);
index++;
}
Serial.flush();
Serial.println("Recieved data from UART:");
Serial.println("<RAW_MEM>");
for(int i = 0; i< NUM_OF_BLOCKS*16; i++)
{
Serial.print(blockData[i], HEX);
}
Serial.println("</RAW_MEM>");
}
index = 0;
}
void ftWriteCallback(uint16_t blockNum, uint8_t* data)
{
// if the WriteF flag is 0x00, writing type3 tag data has started
if(blockData[9] == 0x00)
{
Serial.println("NFC WRITING STARTED");
}
dataChanged=true;
blinkCount=1;
memcpy(blockData+blockNum*16, data, 16);
}
请您参考如下方法:
发送时
"100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"
作为终端程序中的十六进制字节字符串(即字符串中包含十六进制值的字节),您在 Python 中将其作为 ASCII 字符串发送。所以你实际上做了与发送相同的事情
"313030313031303030333030303030303030303030303030303031623030333064313032313635333730643130313132353530313733373437353734373436373631373237343265373336663665373932653634363530303030303030303030"
因此,要在 Python 中获得相同的行为,您必须先将十六进制字符串转换为字节数组:
input_hex = "100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"
input = input_hex.decode("hex")
此外,为了确保数据确实被写入,您可能需要使用类似
arduino.write(input)
arduino.flush()

