本次开发主要做了从串口分别获取读者卡号和图书卡号,实现图书管理,图书借阅,读者管理等功能。开发语言是C#,开发环境使用Visual Studio 2015。数据库采用sql Server。
RFID选择
读者卡跟图书卡均采用125K,一般称之为低频卡,典型的代表智能卡是EM公司与HID公司的125K智能卡。这类卡基本上都是只读、无源卡,识别距离在10cm左右。
应答器采用e5551应答器芯片
e5551芯片是Atmel公司生产的非接触式、无源、可读写、具有防碰撞能力的RFID器件,中心工作频率为 125K。具有以下主要特性:低功耗、低工作电压;非接触能量供给和读写数据;工作频率范围为100~150 kHz; EEPROM存储器容量为264位,分为8块,每块33位;具有7块用户数据,每块32位,共224位;
实现功能:
应用主界面: 管理员端、读者端
管理员端:(演示功能为添加图书)
读者端:实现图书借阅与图书归还
(逻辑实现)
(实现结果)
借还实现代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Threading;
namespace MyRFID
{
public partial class frmBorrowBook : Form
{
BaseClass.BaseInfo baseinfo = new MyRFID.BaseClass.BaseInfo();//创建BaseInfo类的对象
BaseClass.BaseInfo.cBorrowSituation BorrowSituation = new MyRFID.BaseClass.BaseInfo.cBorrowSituation();//创建cBorrowSituation类的对象
BaseClass.BaseInfo.cBook Book = new MyRFID.BaseClass.BaseInfo.cBook();//创建cBook类
public delegate void Displaydelegate(byte[] InputBuf);
Byte[] OutputBuf = new Byte[4];
public Displaydelegate disp_reader;
public Displaydelegate disp_book;
public frmBorrowBook()
{
InitializeComponent();
}
void Comm_ReaderReceived(object sender, SerialDataReceivedEventArgs e)
{
Byte[] InputBuf = new Byte[4];
try
{
if (serialReader.IsOpen)
{
serialReader.Read(InputBuf, 0, serialReader.BytesToRead); //读取缓冲区的数据直到“}”即0x7D为结束符
//InputBuf = UnicodeEncoding.Default.GetBytes(strRD); //将得到的数据转换成byte的格式
System.Threading.Thread.Sleep(50);
this.Invoke(disp_reader, InputBuf);
}
}
catch (TimeoutException ex) //超时处理
{
MessageBox.Show(ex.ToString());
}
}
void Comm_BookReceived(object sender, SerialDataReceivedEventArgs e)
{
Byte[] InputBuf = new Byte[4];
try
{
if (serialBook.IsOpen)
{
serialBook.Read(InputBuf, 0, serialBook.BytesToRead); //读取缓冲区的数据直到“}”即0x7D为结束符
//InputBuf = UnicodeEncoding.Default.GetBytes(strRD); //将得到的数据转换成byte的格式
System.Threading.Thread.Sleep(50);
this.Invoke(disp_book, InputBuf);
}
}
catch (TimeoutException ex) //超时处理
{
MessageBox.Show(ex.ToString());
}
}
public void DispReader(byte[] InputBuf)
{
//textBox1.Text = Convert.ToString(InputBuf);
string result = string.Empty;
string readerno = string.Empty;
string selectReader = "";
string selectBorrow = "";
BaseOperate bop = new BaseOperate();
SqlDataReader reader;
DataSet ds = new DataSet();
ASCIIEncoding encoding = new ASCIIEncoding();
for (int i = 0; i < InputBuf.Length; i++)//逐字节变为16进制字符,以%隔开
{
result += Convert.ToString(InputBuf[i], 16);
}
readerno = int.Parse(result, System.Globalization.NumberStyles.AllowHexSpecifier).ToString();
selectReader = "select * from readerInfo where readerNo='" + readerno + "'";
reader = bop.getread(selectReader);
while (reader.Read())
{
readerNo.Text = reader.GetString(0);
readerName.Text = reader.GetString(1);
}
selectBorrow = "select bookNo 书号,bookName 书名,author 作者, publisher 出版社,borrowDate 借出时间,returnDate 应归还时间 from borrowSituation where readerNo='" + readerno + "'";
ds=bop.getds(selectBorrow,"borrowSituation");
dataGridView1.DataSource = null;
dataGridView1.DataSource = ds.Tables["borrowSituation"];
}
public void DispBook(byte[] InputBuf)
{
//textBox1.Text = Convert.ToString(InputBuf);
string result = string.Empty;
string bookNo = string.Empty;
string bookName = "";
string author = "";
string publisher = "";
string borrowDate = "";
string returnDate = "";
string sqlselect = "";
BaseOperate bop = new BaseOperate();
SqlDataReader reader;
ASCIIEncoding encoding = new ASCIIEncoding();
for (int i = 0; i < InputBuf.Length; i++)//逐字节变为16进制字符,以%隔开
{
result += Convert.ToString(InputBuf[i], 16);
}
bookNo = int.Parse(result, System.Globalization.NumberStyles.AllowHexSpecifier).ToString();
//获取该图书的信息
sqlselect = "select * from bookInfo where bookNo='" + bookNo + "'";
reader = bop.getread(sqlselect);
while (reader.Read())
{
bookName = reader.GetString(1);
author = reader.GetString(2);
publisher = reader.GetString(3);
borrowDate = DateTime.Now.Date.ToShortDateString();
returnDate = DateTime.Now.AddDays(30).ToShortDateString();
}
sqlselect = "select * from borrowSituation where bookNo='" + bookNo + "'";
reader = bop.getread(sqlselect);
while (reader.Read())
{
readerNo.Text = reader.GetString(0);
readerName.Text = reader.GetString(1);
}
//借书操作
if (!reader.HasRows)
{
if (readerNo.Text != "")
{
string insert = "";
insert = "insert into borrowSituation values('" + readerNo.Text + "','" + readerName.Text
+ "','" + bookNo + "','" + bookName + "','" + author + "','" + publisher + "','" + borrowDate +
"','" + returnDate + "')";
bop.getcom(insert);
string selectBorrow = "select bookNo 书号,bookName 书名,author 作者, publisher 出版社,borrowDate 借出时间,returnDate 应归还时间 from borrowSituation where readerNo='" + readerNo.Text + "'";
DataSet ds = new DataSet();
ds = bop.getds(selectBorrow, "borrowSituation");
dataGridView1.DataSource = null;
dataGridView1.DataSource = ds.Tables["borrowSituation"];
}
else
MessageBox.Show("请先放入读者卡,在放入书籍");
}
//还书操作
else
{
string sqlDelete = "";
string sqlInsert = "";
sqlDelete = "delete from borrowSituation where bookNo='" + bookNo + "'";
bop.getcom(sqlDelete);
sqlInsert= "insert into returnSituation values('" + readerNo.Text + "','" + readerName.Text
+ "','" + bookNo + "','" + bookName + "','" + author + "','" + publisher + "','" + borrowDate +
"','" + DateTime.Now.ToShortDateString() + "')";
bop.getcom(sqlInsert);
string selectBorrow = "select bookNo 书号,bookName 书名,author 作者, publisher 出版社,borrowDate 借出时间,returnDate 应归还时间 from borrowSituation where readerNo='" + readerNo.Text + "'";
DataSet ds = new DataSet();
ds = bop.getds(selectBorrow, "borrowSituation");
dataGridView1.DataSource = null;
dataGridView1.DataSource = ds.Tables["borrowSituation"];
}
}
private void frmBorrowBook_Load(object sender, EventArgs e)
{
serialReader.DataReceived += new SerialDataReceivedEventHandler(Comm_ReaderReceived);
serialBook.DataReceived += new SerialDataReceivedEventHandler(Comm_BookReceived);
disp_reader = new Displaydelegate(DispReader);
disp_book = new Displaydelegate(DispBook);
serialReader.Open();
serialBook.Open();
}
private void close_Click(object sender, EventArgs e)
{
serialBook.Close();
serialReader.Close();
this.Close();
}
}
}
程序源码及数据库下载地址: http://download.csdn.net/detail/kevin_iot/9724026