如何使用Python以NDEF格式将网站URL发送到Arduino?
我想把URI以NDEF格式发送到我的Arduino UNO,这个Arduino连接着索尼的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);
}
1 个回答
1
当你在发送
"100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"
时,在你的终端程序中是以十六进制字节字符串的形式发送的(也就是说,发送的是字符串中包含的十六进制值的字节)。而在Python中,你发送的是ASCII字符串。所以实际上你做的事情和发送
"313030313031303030333030303030303030303030303030303031623030333064313032313635333730643130313132353530313733373437353734373436373631373237343265373336663665373932653634363530303030303030303030"
是一样的。
所以,要在Python中实现相同的效果,你需要先把十六进制字符串转换成字节数组:
input_hex = "100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"
input = input_hex.decode("hex")
此外,为了确保数据确实被写入,你可能想使用类似于
arduino.write(input)
arduino.flush()