Nick-ccq 发表于 2021-3-27 17:14:52

【Mind+Python】NFC录入信息和excel存储信息(一)

校园门禁一(NFC录入信息和excel存储信息)
## 前言
  
Mind+1.7.0迎来了目前火热的Python模式,这一版本的发布却引来很多人的使用。废话不多说,直接上项目讲解。

在使用之前先给大家看一下简单的教程简介,以防在使用中出现错误后不知所措。

## Python教程

最新版本(http://mindplus.cc/download.html)

**Mind+ Python模式使用教程大家可以到官方教程中查看:**(https://mindplus.dfrobot.com.cn/Python)

![](https://img.dfrobot.com.cn/wiki/none/a2f7a8252d3f480b735ef6f7ba613ee8.png)

## 项目分析

实现**表格储存NFC信息**,首先要利用**pinpong库**,来控制硬件获取NFC信息,读取该卡信息后写入表格,用于信息存储。**xlwt**是 Python 用来在 Excel 写入数据和格式化数据的工具包,可以实现指定表单、指定单元格的写入。

## 准备工作

## 软件方面

![](https://img.dfrobot.com.cn/wiki/none/03351e5b7ec160d0951b05718885dbd2.png)

#### 安装库文件

1.**pinpong库**

![](https://img.dfrobot.com.cn/wiki/none/72590e37391f1b9df03aab44ca04f8e1.png)

2.**xlwt**

![](https://img.dfrobot.com.cn/wiki/none/d737c3292240596d6c6ae932a3072cc9.png)

#### 硬件连接

![](https://img.dfrobot.com.cn/wiki/none/0b6a51b2c97b66fec5b669829b11748a.png)

## 程序代码

```

# -*- coding: utf-8 -*-

import time
from pinpong.board import Board
from pinpong.libs.dfrobot_pn532 import PN532
import xlwt
import datetime
Board("uno").begin()#初始化,选择板型和端口号,不输入端口号则进行自动识别

nfc = PN532()

workbook = xlwt.Workbook(encoding = 'acsii') #创建一个workbook 设置编码
worksheet = workbook.add_sheet('student') #用add_worksheet添加一个新的工作表,student是表名


student ={}#建立一个空的字典
block_num = 2 #NFC写入块

while not nfc.begin():
print("initial failure")
time.sleep(1)
print("Please place the info card/tag on module..... ")

def parse_data(read_data):
if read_data != None:
    print("read success! data is ", end=" ")
    print(read_data)
else:
    print("read failure!")

a = input("请输入录入学生人数:")#输入录入学习人数
a = int(a)#转化为整形
worksheet.write(0,0,'学号') #指定表格的行和列,写入数据
worksheet.write(0,1,'姓名')
worksheet.write(0,2,'温度')
worksheet.write(0,3,'时间')
style = xlwt.XFStyle()
style.num_format_str = 'M/D/YY'
global i,j
i=1
j=0
while True:
   
    if a == 0 :
      break
    else :
      num = input("请输入学生学号:")
      name = input("请输入学生姓名:")
      student = name
      print("请放入卡片!录入信息")
      a=a-1
      while not (nfc.scan()==True):
      pass
      while not (nfc.get_information()!=None):
      pass
      if nfc.write_data(block_num, num):
      print("录入成功", num,student)
      worksheet.write(i,0,num)
      worksheet.write(i,1,student)
      worksheet.write(i,3,datetime.datetime.now(),style)
      i = i+1
      workbook.save('student.xls')
         


   
   
   
```

## 调试

1.键盘输入录入学生人数(**NFC的标签卡和人数是一一对应的**)

![](https://img.dfrobot.com.cn/wiki/none/196312c659476623074fe3e3d720b933.png)

2.键盘输入学号和姓名,回车按下,等待卡片放入

![](https://img.dfrobot.com.cn/wiki/none/272d921b4b4e71c13b8eabbf000d5156.png)

3.卡片识别成功后,提示录入信息成功(两个学生录入完成,终止录入)
![](https://img.dfrobot.com.cn/wiki/none/89e4455f3a082fb74f7e0a2f5ffad39f.png)

![](https://img.dfrobot.com.cn/wiki/none/171dfea4ecd0437832d45e156cf6a426.png)

4.打开表格

![](https://img.dfrobot.com.cn/wiki/none/de0cd8d9f168bc57eaeab5f8cabe06b2.png)

![](https://img.dfrobot.com.cn/wiki/none/dc391b451ad230285f1b4e829a202d61.png)

![](https://img.dfrobot.com.cn/wiki/none/e8451ca5a48e2786ca52ce45ff88f251.png)

![](https://img.dfrobot.com.cn/wiki/none/b2eaff23c0d444a535302468d12af23a.png)

## 演示图

![](https://img.dfrobot.com.cn/wiki/none/b6d2de2cab795869e9650ac936474f03.gif)

## 总结

在表格中大家看到了温度,因为这篇是一个类似校园门禁的项目,目前只是如何把信息放入表格,下一篇帖子会体现口罩识别和温度检测,并且会把学生体温放表格中。

Anonymous 发表于 2022-5-30 19:18:36

大佬你好,运行程序在40到42时出错怎么搞

Nick-ccq 发表于 2022-6-6 10:46:04

匿名者 发表于 2022-5-30 19:18
大佬你好,运行程序在40到42时出错怎么搞

40行,变量赋值问题

zbh 发表于 2023-7-21 16:42:26

大佬,请问运行后没有出现excel表格是怎么回事啊

三春牛-创客 发表于 2023-7-21 22:35:07

不错,赞!{:6_209:}

三春牛-创客 发表于 2023-7-21 22:36:08

来学习了,感谢分享!{:6_202:}

花生编程 发表于 2023-7-22 22:52:02

好厉害啊!大神!{:6_215:}

花生编程 发表于 2023-7-22 22:53:38

校园门禁,赞{:6_209:}
页: [1]
查看完整版本: 【Mind+Python】NFC录入信息和excel存储信息(一)