查看“向标签内写入内容”的源代码
←
向标签内写入内容
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{| style="width: 800px;" |- | <p style="color: #4F4E4E;font-size:220%">'''向标签内写入内容'''</p> <br> 在下面的示例中,在获得一张新的mifare标签时,需要将其格式化为ndef数据格式 <br> <br> <p style="color: #E87E05;font-size:155%">所需硬件</p> *[[MCookie-Core+]] *[[MBattery+]] *[[MCookie-NFC/zh]](带天线) *一个NFC卡片 <br> <p style="color: #E87E05;font-size:155%">电路搭建</p> 将Battery+、Core+、MCookie_SD、MCookie-NFC堆叠在一起,通过MicroUSB数据线接入电脑。 <br> <p style="color: #E87E05;font-size:155%">代码</p> <source lang="cpp"> /**************************************************************************/ /*! @file mifareclassic_formatndef.pde This example attempts to format a clean Mifare Classic 1K card as an NFC Forum tag (to store NDEF messages that can be read by any NFC enabled Android phone, etc.) Note that you need the baud rate to be 115200 because we need to print out the data and read from the card at the same time! Check out the links above for our tutorials and wiring diagrams These chips use I2C to communicate */ /**************************************************************************/ #include <Microduino_NFC.h> NFC nfc; /* We can encode many different kinds of pointers to the card, from a URL, to an Email address, to a phone number, and many more check the library header .h file to see the large # of supported prefixes! */ // For a http://www. url: const char *url = "www.microduino.cn"; uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT; // for an email address //const char * url = "mail@example.com"; //uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO; // for a phone number //const char * url = "+1 212 555 1212"; //uint8_t ndefprefix = NDEF_URIPREFIX_TEL; void setup(void) { Serial.begin(115200); Serial.println("Looking for PN532..."); uint32_t versiondata = nfc.begin(); if (! versiondata) { Serial.print("Didn't find PN53x board"); while (1); // halt } // Got ok data, print it out! Serial.print("Found chip PN5"); Serial.println((versiondata >> 24) & 0xFF, HEX); Serial.print("Firmware ver. "); Serial.print((versiondata >> 16) & 0xFF, DEC); Serial.print('.'); Serial.println((versiondata >> 8) & 0xFF, DEC); } void loop(void) { uint8_t success; // Flag to check if there was an error with the PN532 uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on ISO14443A card type) bool authenticated = false; // Flag to indicate if the sector is authenticated // Use the default key uint8_t keya[6] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; Serial.println(""); Serial.println("PLEASE NOTE: Formatting your card for NDEF records will change the"); Serial.println("authentication keys. To reformat your NDEF tag as a clean Mifare"); Serial.println("Classic tag, use the mifareclassic_ndeftoclassic example!"); Serial.println(""); Serial.println("Place your Mifare Classic card on the reader to format with NDEF"); Serial.println("and press any key to continue ..."); // Wait for user input before proceeding while (!Serial.available()); // a key was pressed1 while (Serial.available()) Serial.read(); // Wait for an ISO14443A type card (Mifare, etc.). When one is found // 'uid' will be populated with the UID, and uidLength will indicate // if the uid is 4 bytes (Mifare Classic) or 7 bytes (Mifare Ultralight) success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength); if (success) { // Display some basic information about the card Serial.println("Found an ISO14443A card"); Serial.print(" UID Length: "); Serial.print(uidLength, DEC); Serial.println(" bytes"); Serial.print(" UID Value: "); nfc.PrintHex(uid, uidLength); Serial.println(""); // Make sure this is a Mifare Classic card if (uidLength != 4) { Serial.println("Ooops ... this doesn't seem to be a Mifare Classic card!"); return; } // We probably have a Mifare Classic card ... Serial.println("Seems to be a Mifare Classic card (4 byte UID)"); // Try to format the card for NDEF data success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 0, 0, keya); if (!success) { Serial.println("Unable to authenticate block 0 to enable card formatting!"); return; } success = nfc.mifareclassic_FormatNDEF(); if (!success) { Serial.println("Unable to format the card for NDEF"); return; } Serial.println("Card has been formatted for NDEF data using MAD1"); // Try to authenticate block 4 (first block of sector 1) using our key success = nfc.mifareclassic_AuthenticateBlock (uid, uidLength, 4, 0, keya); // Make sure the authentification process didn't fail if (!success) { Serial.println("Authentication failed."); return; } // Try to write a URL Serial.println("Writing URI to sector 1 as an NDEF Message"); // Authenticated seems to have worked // Try to write an NDEF record to sector 1 // Use 0x01 for the URI Identifier Code to prepend "http://www." // to the url (and save some space). For information on URI ID Codes // see http://www.ladyada.net/wiki/private/articlestaging/nfc/ndef if (strlen(url) > 38) { // The length is also checked in the WriteNDEFURI function, but lets // warn users here just in case they change the value and it's bigger // than it should be Serial.println("URI is too long ... must be less than 38 characters long"); return; } // URI is within size limits ... write it to the card and report success/failure success = nfc.mifareclassic_WriteNDEFURI(1, ndefprefix, url); if (success) { Serial.println("NDEF URI Record written to sector 1"); } else { Serial.println("NDEF Record creation failed! :("); } } // Wait a bit before trying again Serial.println("\n\nDone!"); delay(1000); Serial.flush(); while(Serial.available()) Serial.read(); } </source> <br> ---- <br> *将卡片放在读卡器上,确认模块与读卡器连接好。打开串口等待提示,当出现“press any key to continue”时在串口输入任意字符即可进入之后步骤。 *值得注意的是:默认格式化密码为: <source lang="cpp"> uint8_t keya[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; </source> 初始写入内容在如下代码处更改 <source lang="cpp"> const char * url = "www.microduino.cn"; uint8_t ndefprefix = NDEF_URIPREFIX_HTTP_WWWDOT; // for an email address //const char * url = "mail@example.com"; //uint8_t ndefprefix = NDEF_URIPREFIX_MAILTO; // for a phone number //const char * url = "+1 212 555 1212"; //uint8_t ndefprefix = NDEF_URIPREFIX_TEL; </source> *打开示例中的“readMifare”示例 在程序中找到如下程序段,该段程序会向卡片中写入“50274895mcokie8”字符串(不包括引号)。之后程序会再次读出该字符串。 <source lang="cpp"> // If you want to write something to block 4 to test with, uncomment // the following line and this text should be read back in a minute memcpy(data, (const uint8_t[]){ '5', '0', '2', '7', '4', '8', '9', '5', 'm', 'c', 'o', 'k', 'i', 'e', '8' }, sizeof data); success = nfc.mifareclassic_WriteDataBlock (4, data); </source> 打开串口开始运行程序,会看到如下返回。 *该示例中包括:写入数据,读出数据,读出卡片ID,输入密码(keya数组)等功能。 [[file:Readmifare.png|center|600px|thumb]] |} [[MCookie-NFC/zh | 返回MCookie-NFC/zh页面]]
返回至
向标签内写入内容
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息