【问题标题】:How to send URL of a website to arduino in NDEF format using python?如何使用 python 以 NDEF 格式将网站的 URL 发送到 arduino?
【发布时间】:2014-06-21 05:42:14
【问题描述】:

我希望能够将 NDEF 格式的 URI 发送到我的 Arduino UNO,它连接到 Sony 的 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);
   }

【问题讨论】:

  • 在 Hercules 设置实用程序中发送时使用哪些参数?
  • 我只发送十六进制数据,比如“100101000300000000000000001b0030d102165370d1011255010000000000676f6f676c652e636f6d00000000000000000000”。 i.imgur.com/8W3gBKP.jpg[/IMG] - 这是 hercules 设置工具的截图

标签: python arduino format nfc ndef


【解决方案1】:

发送时

"100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"

作为终端程序中的十六进制字节字符串(即字符串中包含十六进制值的字节),您在 Python 中将其作为 ASCII 字符串发送。所以你实际上做的和发送一样

"313030313031303030333030303030303030303030303030303031623030333064313032313635333730643130313132353530313733373437353734373436373631373237343265373336663665373932653634363530303030303030303030"

因此,要在 Python 中获得相同的行为,您必须先将十六进制字符串转换为字节数组:

input_hex = "100101000300000000000000001b0030d102165370d1011255017374757474676172742e736f6e792e64650000000000"
input = input_hex.decode("hex")

此外,为了确保数据是真实写入的,您可能需要使用类似

arduino.write(input)
arduino.flush()

【讨论】:

  • 我尝试了您建议的更改,但在 NXP 应用程序扫描时仍将其作为“空记录”。如果我在写入 Arduino 之前放置“time.sleep(5)”命令,那么我得到校验和错误,但数据被部分传输。你能告诉我为什么会这样吗?
  • 不,我不能,如果不知道您在 Arduino 方面实际做了什么,几乎不可能说出来。你从哪里得到“校验和错误”?
  • 我已将您的电子邮件 ID 上的 arduino 代码发送给您。请看一下。校验和错误显示在我的 NFC 手机上的 NXP 应用程序上
  • @Shreyas 抱歉,您应该将所有相关代码附加到问题中。我不会通过电子邮件接受问题/附加材料,因为这会妨碍其他人从这个问题/答案中受益。
猜你喜欢
  • 1970-01-01
  • 2014-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多