前言
刷卡上机,一听这个词,我就觉得好有难度,可事实上却不难实现。 我的小伙伴实现了这个功能,实现固然重要,但是复用对以后更有帮助。我便简单的研究了下这个功能......
实现思路
首先,你当然需要一个刷卡机和几张卡。我们的刷卡机是USB口的。其次,我们刷卡实现的是上机的功能,在我们的程序里也就是登录,所以我们接下来要对登录窗体进行操作。
<1>引用mwhrf_bj.dll、ReaderBLL和ReaderDAL文件。
请在你的VS中添加引用,浏览找到这几个文件。在这里笔者想说句抱歉的是,这三个文件是刷卡机的,不同的刷卡机应该有不同的文件名,此篇博客只给实现刷卡提供一个思路
mwhrf_bj.dll无法引用,直接放到应用程序-bin-Debug里
<2>新建刷卡功能类。
在U层新建一个类库,里面准备编写实现功能代码。这里需要注意的是需要添加命名空间using ReaderBLL;
public class MyTools{/// <summary>/// 从读卡器读取卡号的方法/// </summary>/// <returns></returns>public static string ReadCardNo(){string cardNo = "";string key = "";int sectors = 0;key = System.Configuration.ConfigurationManager.AppSettings["key"].ToString();sectors = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["sectors"].ToString());CardReader readCard = new CardReader(key, sectors);readCard.Connection();string strRead = readCard.Read();switch (strRead){case "1":try{readCard.Close();}catch{throw new Exception("寻卡失败,请检查前台读卡器是否完好");}break;case "2":readCard.Close();break;case "3":readCard.Close();break;default:if (readCard.Read().Length == 64){cardNo = readCard.Read().Substring(0, 32).ToString();readCard.Close();}else{throw new Exception("卡号有问题,可能卡损坏了!");}break;}return cardNo;}}
功能好理解,但好像上面代码中有个类不知道在哪里定义的,就是这个CardReader。它其实就是那个引用的dll文件,我们来看看这个文件里边写的是啥。
<3>做一个刷卡窗体FrmCardLogin。控件方面需要加一个timer。另外再多说一句,skinLabel是我们的皮肤,其实就是label。
<4>FrmCardLogin的代码。
这是小僧窗体里全部的代码。小僧也知道读者看着乱。就简单说下代码必要方法的逻辑。
①设置实体卡的编号和数据库对应,这样方便刷卡操作。
②设置Timer事件,用来读取卡号。这样,系统通过Timer事件感应到卡被贴上了,就可以继续接来下的处理了。
③上机操作成功后还需要更新Online表,计算收费之类的后续操作,这些操作里调用的各层方法都是以前用过的功能,这里就不再赘述。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace UI.Common
{public partial class FrmCardLogin : Form{private Dictionary<string, string> dic = new Dictionary<string, string>();public FrmCardLogin(){InitializeComponent();timer1.Interval = 10;timer1.Enabled = true;//设置卡的编号对应数据库中的卡号dic.Add("4AADF1485E0804008500AABBCCDDEEFF", "1");dic.Add("0598F148240804008500AABBCCDDEEFF", "2");dic.Add("ACE7BFD2260804006263646566676869", "3");}//应用单例模式private static FrmCardLogin fmpwd; //frmStModifyPassword是窗体名字public static FrmCardLogin GetInstance(){if (fmpwd == null || fmpwd.IsDisposed){fmpwd = new FrmCardLogin();}return fmpwd;}/// <summary>/// 切换为账号密码登录/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void skinLabel2_Click(object sender, EventArgs e){FrmLogin frmlogin = new FrmLogin();frmlogin.Show();this.Close();}private void timer1_Tick(object sender, EventArgs e){if (txtuserid.Text.Trim() != ""){return;}//开始读卡号string cardno = "";try{cardno = MyTools.ReadCardNo();}catch (Exception ex){timer1.Enabled = false;DialogResult result = MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OKCancel);if (result == DialogResult.OK){timer1.Enabled = true;}}txtuserid.Text = dic.ContainsKey(cardno) ? dic[cardno] : cardno;}private void skinTextBox1_Paint(object sender, PaintEventArgs e){}//定义全局变量userID,password,levelpublic static string level;public static int userID;public static string name;public static string password;private void txtuserid_TextChanged(object sender, EventArgs e){FrmLogin.userID =Convert.ToInt32(txtuserid.Text);//判断是否输入为空if (txtuserid.Text.Trim() == ""){MessageBox.Show("请刷卡!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);return;}//获取文本框ID,密码userID = Convert.ToInt32(txtuserid.Text.Trim());//判断账户是否存在Entity.UserEntity user = new Entity.UserEntity();user.UserID = Convert.ToInt32(txtuserid.Text);Facade.LoginFacade loginfacade = new Facade.LoginFacade();List<Entity.UserEntity> list1 = new List<Entity.UserEntity>();list1 = loginfacade.SelectCard(user);//如果不存在if (list1.Count <= 0 || list1[0].UserID != userID){MessageBox.Show("刷卡上机失败!");txtuserid.Text = "";return;}//如果存在else if (list1[0].UserID == userID){level = list1[0].Level; //获取用户级别name = list1[0].UserName;FrmLogin.level = level;//如果账户级别为一般用户if (list1[0].Level == "一般用户"){//判断账户是否已经登录Entity.OnlineEntity onlineuser = new Entity.OnlineEntity();onlineuser.CardNo = Convert.ToInt32(txtuserid.Text);Facade.LoginFacade onlinefacade = new Facade.LoginFacade();bool isOnline = new bool();isOnline = onlinefacade.OnlineUserID(onlineuser);//如果已登录if (isOnline == false){MessageBox.Show("该用户已登录!");return;}else{//实例化出学生表,准备比较余额Entity.StudentEntity student = new Entity.StudentEntity();student.CardNo = Convert.ToInt32(txtuserid.Text.Trim());Facade.LoginFacade selectcash = new Facade.LoginFacade();List<Entity.StudentEntity> list2 = new List<Entity.StudentEntity>();list2 = selectcash.QueryStuCash(student);//如果当前余额小于最低上机金额4元,无法上机if (list2[0].Cash < 4){MessageBox.Show("抱歉,您的余额不足,请先充值再登录!");return;}else{//更新Online表信息Facade.LoginFacade addonline = new Facade.LoginFacade();Entity.OnlineEntity online = new Entity.OnlineEntity();online.CardNo = Convert.ToInt32(txtuserid.Text);online.StudentNo = Convert.ToInt32(txtuserid.Text);online.StudentName = name;online.OnDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).ToString();online.OnTime = DateTime.Now.ToLongTimeString().ToString();online.OnDate = Convert.ToString(DateTime.Now.ToString("yyyy-MM-dd"));online.OnTime = Convert.ToString(DateTime.Now.ToLongTimeString());addonline.InsertOnline(online);}}}//如果账户级别为操作员或管理员if (list1[0].Level != "一般用户"){//判断账户是否已经登录Entity.OnWorkEntity onworkuser = new Entity.OnWorkEntity();onworkuser.UserID = Convert.ToInt32(txtuserid.Text);Facade.LoginFacade onworkfacade = new Facade.LoginFacade();bool isOnwork = new bool();isOnwork = onworkfacade.OnworkUserID(onworkuser);//如果已登录if (isOnwork == false){MessageBox.Show("该用户已登录");return;}else{//更新Onwork表信息Facade.LoginFacade addonwork = new Facade.LoginFacade();Entity.OnWorkEntity onwork = new Entity.OnWorkEntity();onwork.UserID = Convert.ToInt32(txtuserid.Text);onwork.UserName = name;onwork.Level = level;onwork.OnDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).ToString();onwork.OnTime = DateTime.Now.ToLongTimeString().ToString();onwork.OnDate = Convert.ToString(DateTime.Now.ToString("yyyy-MM-dd"));onwork.OnTime = Convert.ToString(DateTime.Now.ToLongTimeString().ToString());onwork.Computer = System.Environment.MachineName;addonwork.InsertOnwork(onwork);}}//跳转到主窗体this.Hide();// this.DialogResult = System.Windows.Forms.DialogResult.OK;FrmMain frm = new FrmMain();frm.Show();}}
<5>请记得在你的配置文件(App.config)里面<appSettings>这里<appSettings>加上如下代码
<appSettings><!--UI层的读卡器 --><add key="sectors" value="0" /><add key="key" value="FFFFFFFFFFFF" /></appSettings>
看看效果,没插卡系统自动检测寻卡失败~