9502浏览
查看: 9502|回复: 6

[进阶] NFC近场通讯模块的简易教程和评测

[复制链接]
NFC近场通讯模块的简易教程和评测

大家好,我是Bill,事隔好久的第二次发文 。这次评测的是NFC近场通讯模块(DFR0231)https://www.dfrobot.com.cn/goods-762.html。其实上上个月就已经把NFC的库很早之前就写好了,只不过忘记写个教程了 。。。趁着自己还没忘记,写个总结吧,顺便把库文件也共享下。程序暂时实现了Mifare 1卡使用初始默认密码进行读写操作,像修改密码、控制区块这种容易写废卡的操作暂时没包括。不过里面预留有接口,有兴趣的可以啊根据PN532和Mifare 1卡的用户手册,自行填写。

第一部分:NFC的介绍
NFC是英文Near Field Communication的缩写,又称近距离无线通信,是一种短距离的高频无线通信技术,允许电子设备之间进行非接触式点对点数据传输,在十厘米(3.9英寸)内交换数据。目前在很多场合都有所应用,比如说门禁系统、银行卡、交通卡、身份证以及身份证 。现在很多手机也支持NFC卡标签的读写(http://en.wikipedia.org/wiki/List_of_NFC-enabled_mobile_devices)。通过读写NFC便签,能够实现快速启动程序、信息交互等功能。

接下来介绍下DFRobot的NFC近场通讯模块。DFRobot采用的是NXP的PN532模块支持6种不同的工作模式:读写器模式,支持ISO/IEC 14443A / MIFARE机制;读写器模式,支持 FeliCa机制;读写器模式,支持ISO/IEC 14443B机制;卡操作模式,支持ISO 14443A / MIFARE机制;卡操作模式,FeliCa机制;ISO/IEC18092,ECM340点对点。

第二部分 连接方式
这里使用软串口进行连接,不过因为软串口在115200频率下工作不稳定,程序会花费半秒左右尝试自动降频到57600。当然使用NFC模块的TX口连设备的RX口,RX口连设备的TX口,直接使用串口也可,不过注意烧录程序需拔下NFC的电源连接线NFC近场通讯模块的简易教程和评测图1

第三部分 代码介绍


这里是写好的库,主要根据adafruit的NFC库进行了适当的修改。保留了大部分功能并增加了简易读写NFC便签数据的功能。下面介绍下主要新增的几个函数。

int write(unsigned int byteAddr, uint8_t byteData);
实现了对指定位置字符的写操作,其中第一个参数为字符地址,第二个为要写入的数据。
返回值:1   操作成功
                0        未定义
-1  如果位置越
-2  如果无法找到合适M1卡或者NFC未连接
-3  如果验证失败
-4  如果读取数据所在区块失败
-5  如果写入数据失败

int writeBytes(uint8_t* buff, unsigned int byteAddrStart, unsigned int length);
实现了对指定位置字符串的写操作,其中第一个参数为写入的字符串的头地址,第二个为NFC的起始写入位置,第三个为写入字符串的长度
返回值:1   操作成功
                0   未定义
-1  如果位置越
-2  如果无法找到合适M1卡或者NFC未连接
-3  如果验证失败
-4  如果读取数据所在区块失败
-5  如果写入数据失败

int read(unsigned int byteAddr);
实现了对指定位置数据的读操作,其中第一个参数为数据地址。
返回值:>=0 操作成功
-1  如果位置越界
-2  如果无法找到合适M1卡或者NFC未连接
-3  如果验证失败
-4  如果读取数据所在区块失败

int readBytes(uint8_t* buff, unsigned int byteAddrStart, unsigned int length);
实现了对指定位置字符串的读操作,其中第一个参数为保存读取字符串的头地址,第二个为NFC的起始读取位置,第三个为要读取字符串的长度
返回值:1   操作成功
-1  如果位置越界
-2  如果无法找到合适M1卡或者NFC未连接
-3  如果验证失败
-4  如果读取数据所在区块失败

以下是一段总的测试代码,给的库里还有4个write、read、writeBytes和readBytes里分别的样例代码。代码水平有限,如果有什么问题,欢迎提出。
  1. /***************************************************
  2.       NFC Module for Arduino (SKU:DFR0231)
  3. <https://wiki.dfrobot.com/NFC_Module_for_Arduino__SKU_DFR0231_>
  4. ***************************************************
  5. This example reads characters in the Mifare Classic of the
  6. data blocks (address 0 to address 751) using default keyB {0xFF,0xFF,0xFF,
  7. 0xFF,0xFF,0xFF} and prints it to the computer.
  8. Created 2015-4-28
  9. By Bill Hong
  10. GNU Lesser General Public License.
  11. See <http://www.gnu.org/licenses/> for details.
  12. All above must be included in any redistribution
  13. ****************************************************/
  14. /***********Notice and Trouble shooting***************
  15. 1.The address is from 0 to 751, 752 Bytes in the data blocks.
  16. 2.This code is tested on Arduino Uno.
  17. ****************************************************/
  18. /***************************************************
  19.       NFC Module for Arduino (SKU:DFR0231)
  20. <https://wiki.dfrobot.com/NFC_Module_for_Arduino__SKU_DFR0231_>
  21. ***************************************************
  22. This example reads characters in the Mifare Classic of the
  23. data blocks (address 0 to address 751) using default keyB {0xFF,0xFF,0xFF,
  24. 0xFF,0xFF,0xFF} and prints it to the computer.
  25. Created 2015-4-28
  26. By Bill Hong
  27. GNU Lesser General Public License.
  28. See <http://www.gnu.org/licenses/> for details.
  29. All above must be included in any redistribution
  30. ****************************************************/
  31. /***********Notice and Trouble shooting***************
  32. 1.The address is from 0 to 751, 752 Bytes in the data blocks.
  33. 2.This code is tested on Arduino Uno.
  34. ****************************************************/
  35. #include "Arduino.h"
  36. #include "DFRNFC.h"
  37. #include "SoftwareSerial.h"
  38. DFRNFC nfc;
  39. SoftwareSerial mySerial(6, 7); // RX, TX
  40. void setup(void)
  41. {
  42.   
  43.   //initialize nfc module
  44.   Serial.begin(115200);
  45.   mySerial.begin(115200); //PN532 default SerialBaudRate is 115200
  46.   nfc.begin(mySerial);
  47.   Serial.println("Looking for PN532...");
  48. }
  49. void loop()
  50. {
  51.   uint8_t data[752];
  52.   int success = 0;
  53.   int startAddress = 0;
  54.   int dataLength = 752; //maximum length is 752 byte
  55.   for(int i=0;i<dataLength;i++)
  56.   {
  57.     data[i] = i;
  58.   }
  59.   Serial.println("writing");
  60.   for(int i=0;i<dataLength-1;i+=16)
  61.   {
  62.      //writing data
  63.       success = nfc.writeBytes(data+i,i,16);
  64.     if(success!=1)
  65.     {
  66.       Serial.println();
  67.       Serial.print("writing error ");
  68.     }
  69.     else
  70.     {
  71.       Serial.print("#");
  72.     }   
  73.   }
  74.   Serial.println();
  75.   Serial.println("writing end");
  76.   
  77.   Serial.println("reading");
  78.   // read byte all bytes of the Mifare Classic Card to data;
  79.   success = nfc.readBytes(data,startAddress,dataLength);
  80.   
  81.   //returned information
  82.   switch(success)
  83.   {
  84.     case -1:
  85.       Serial.println("address is without range");
  86.       break;
  87.     case -2:
  88.       Serial.println("failed to find a Mifare classic card");
  89.       break;
  90.     case -3:
  91.       Serial.println("faild to authenticate, the device will try to find  a new card");
  92.       break;
  93.     case -4:
  94.       Serial.println("faild to read");
  95.       break;
  96.     default:
  97.       Serial.println();
  98.       Serial.println("start printing the data");
  99.       nfc.setDebugSerial(Serial);
  100.       for(int i=0;i<752;i++)
  101.       {
  102.         if (data[i] <= 0xF)
  103.         {
  104.             Serial.print("0");
  105.         }
  106.         Serial.print(data[i],HEX); //print data,16 bytes per line
  107.         Serial.print(" ");
  108.         if((i+1)%16 == 0)
  109.         {     
  110.             Serial.println();
  111.         }
  112.       }
  113.       break;
  114.   }
  115.   while(1);
  116. }
复制代码

DFRNfc_lib.rar

25.7 KB, 下载次数: 633

大连林海  初级技神

发表于 2015-5-12 17:47:55

绝对要给精华 这个资料不是很丰富
回复

使用道具 举报

bill_hong  初级技师
 楼主|

发表于 2015-5-12 18:19:36

代码串口信息的输出
捕获.PNG
回复

使用道具 举报

Grey  中级技匠

发表于 2015-5-12 19:04:41

给个赞先! PS:软串口115200靠谱吗?感觉以前带不动啊
回复

使用道具 举报

映雪—斗牛士  学徒

发表于 2017-4-18 22:14:54

回复

使用道具 举报

314982750  学徒

发表于 2019-1-13 19:21:11

写的太好了
回复

使用道具 举报

GYJ  学徒

发表于 2019-7-8 10:22:26

写的太好了,顶起来
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail