一、任务需求
1、连接设备,读取RFID卡中员工编号,并向数据库中插入一条打卡记录
2、注册新卡,向RFID卡中写入新员工编号,并能在打卡机上读取出来
二、前期准备
1)打卡机读取界面
2)设备连接
三、打卡流程
1、获取串口及初始化串口
{
// 自动获取串口列表并显示到comboBoxCOMList中
string[] AllPort = SerialPort.GetPortNames();
//2.将一个组合框的内容清除
comboBoxCOMList.Items.Clear();
//3.将数组中的值添加到组合框中
for (int i = 0; i < AllPort.Length; i++)
{
comboBoxCOMList.Items.Add(AllPort[i]);
}
}
/// <summary>
/// 初始化串口参数
/// </summary>
private void InitSerialPort()
{
// 设置串口参数
serialPort1.BaudRate = 115200;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Parity = Parity.None;
}
2、打开串口
private void buttonOpenCOM_Click(object sender, EventArgs e)
{
// 串口已打开,此时需要关闭
if (serialPort1.IsOpen)
{
serialPort1.Close();
this.toolStripStatusLabel1.Text = "已关闭串口" + serialPort1.PortName.ToString();
buttonOpenCOM.Text = "打开";
return;
}
// 否则打开串口
else
{
serialPort1.PortName = comboBoxCOMList.Text;
InitSerialPort();
try
{
serialPort1.Open();
this.toolStripStatusLabel1.Text = "已打开串口" + serialPort1.PortName.ToString();
buttonOpenCOM.Text = "关闭";
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = "打开串口失败,原因:" + ex.Message;
return;
}
}
}
3、寻卡
if (!serialPort1.IsOpen)
{
this.toolStripStatusLabel1.Text = "请先打开串口";
return;
}
this.toolStripStatusLabel1.Text = "未找到有效的卡";
/////////////////////////////////////////////////////////////////////////////////////////////
// 寻卡,将RFID卡号读出来
string response = "";
serialPort1.Write(ISO15693Card.COMMAND_WRITE_REG);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_SET_AGC);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_SET_RECV_MODE);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_INVEN_CARD);// 寻卡
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
List<ISO15693Card> cards = ISO15693CardHandler.InventoryCard(response);
4、读取RFID卡内职员工号信息
private string ReadSingleBlock(string UID, string strBlockIndex)
{
// 格式化读写命令
string command = string.Format(ISO15693Card.COMMAND_READ_SINGLE_BLOCK, UID, strBlockIndex);
serialPort1.Write(command);// 写命令
Thread.Sleep(MILLISECOND_IN_SLEEP);// 睡眠一段时间
string response = "";
if (serialPort1.BytesToRead > 0)
{
response = serialPort1.ReadExisting();
string stuffId = ISO15693CardHandler.GenerateBlockData(response);
stuffId = stuffId.TrimStart('0');
this.toolStripStatusLabel1.Text = "读取到职员编号为" + stuffId;
return stuffId;
}
return null;
}
5、向数据库中插入该条员工打卡记录
// 读取卡上第00块的数据,获得职员编号
String stuffId = ReadSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00");
// 向数据库中插入一条打卡记录,插入成功后显示打卡成功
String date = DateTime.Now.ToString("yyyy - MM - dd");// 默认格式的日期和时间
String time = DateTime.Now.ToString("hh:mm:ss");
String employee_id = stuffId;
String machine_id = "1";
// 连接字符串,注意与实际环境保持一致
String connStr = ConfigurationManager.ConnectionStrings["KQ"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 连接数据库
sqlConn.Open();
// 构造命令
String sqlStr = "INSERT INTO record(employee_id, date, time, machine_id) VALUES (@employee_id,@date,@time,@machine_id)";
SqlCommand cmd = new SqlCommand(sqlStr, sqlConn);
// SQL字符串参数赋值
cmd.Parameters.Add(new SqlParameter("@employee_id", employee_id));
cmd.Parameters.Add(new SqlParameter("@date", date));
cmd.Parameters.Add(new SqlParameter("@time", time));
cmd.Parameters.Add(new SqlParameter("@machine_id", machine_id));
// 将命令发送给数据库
int res = cmd.ExecuteNonQuery();
// 根据返回值判断是否插入成功
if (res != 0)
{
MessageBox.Show("信息录入成功");
}
else
{
MessageBox.Show("信息录入失败");
}
}
catch (Exception exp)
{
MessageBox.Show("访问数据库错误:" + exp.Message);
}
finally
{
sqlConn.Close();
}
四、注册新员工卡流程
1、连接串口,初始化串口,打开串口(同上)
2、写入职员工号
private bool WriteSingleBlock(string UID, string strBlockIndex, string hexData)
{
// 格式化命令
string command = string.Format(ISO15693Card.COMMAND_WRITE_SINGLE_BLOCK, UID, strBlockIndex, hexData);
serialPort1.Write(command);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0)
{
string response = serialPort1.ReadExisting();
return true;
}
return false;
}
3、寻卡,向RFID卡中写入数据
private void bt_Register_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
this.toolStripStatusLabel1.Text = "请先打开串口";
return;
}
this.toolStripStatusLabel1.Text = "未找到有效的卡";
// '666'填充为'00000666'
string stuffId = this.tb_EmployeeId.Text.PadLeft(8, '0');
// '00000666'转变成'66060000'
stuffId = ISO15693CardHandler.CovertEndian(stuffId);
// 检查输入数据的错误
// ISO15693为32位,4字节,8字符
if (stuffId.Length != 8)
{
MessageBox.Show("请输入4字节的16进制数据!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 检查16进制字符错误
if (!ISO15693CardHandler.CheckValidHexBytes(stuffId))
{
MessageBox.Show("写入数据的16进制格式错误!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 寻卡,将RFID卡号读出来
string response = "";
serialPort1.Write(ISO15693Card.COMMAND_WRITE_REG);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_SET_AGC);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_SET_RECV_MODE);
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
serialPort1.Write(ISO15693Card.COMMAND_INVEN_CARD);// 寻卡
Thread.Sleep(MILLISECOND_IN_SLEEP);
if (serialPort1.BytesToRead > 0) response = serialPort1.ReadExisting();
List<ISO15693Card> cards = ISO15693CardHandler.InventoryCard(response);
// 向RFID卡第00块写入职员编号数据,无论成功与否,均应该在状态栏显示提示信息
bool stuffIdw = this.WriteSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00", stuffId);
this.toolStripStatusLabel1.Text = "已写入职员编号为" + stuffId;
}
网友评论