一、任務(wù)需求
1莉钙、連接設(shè)備廓脆,讀取RFID卡中員工編號(hào),并向數(shù)據(jù)庫中插入一條打卡記錄
2磁玉、注冊(cè)新卡停忿,向RFID卡中寫入新員工編號(hào),并能在打卡機(jī)上讀取出來
二蚊伞、前期準(zhǔn)備
1)打卡機(jī)讀取界面
2)設(shè)備連接
三席赂、打卡流程
1、獲取串口及初始化串口
{
// 自動(dòng)獲取串口列表并顯示到comboBoxCOMList中
string[] AllPort = SerialPort.GetPortNames();
//2.將一個(gè)組合框的內(nèi)容清除
comboBoxCOMList.Items.Clear();
//3.將數(shù)組中的值添加到組合框中
for (int i = 0; i < AllPort.Length; i++)
{
comboBoxCOMList.Items.Add(AllPort[i]);
}
}
/// <summary>
/// 初始化串口參數(shù)
/// </summary>
private void InitSerialPort()
{
// 設(shè)置串口參數(shù)
serialPort1.BaudRate = 115200;
serialPort1.DataBits = 8;
serialPort1.StopBits = StopBits.One;
serialPort1.Parity = Parity.None;
}
2时迫、打開串口
private void buttonOpenCOM_Click(object sender, EventArgs e)
{
// 串口已打開颅停,此時(shí)需要關(guān)閉
if (serialPort1.IsOpen)
{
serialPort1.Close();
this.toolStripStatusLabel1.Text = "已關(guān)閉串口" + serialPort1.PortName.ToString();
buttonOpenCOM.Text = "打開";
return;
}
// 否則打開串口
else
{
serialPort1.PortName = comboBoxCOMList.Text;
InitSerialPort();
try
{
serialPort1.Open();
this.toolStripStatusLabel1.Text = "已打開串口" + serialPort1.PortName.ToString();
buttonOpenCOM.Text = "關(guān)閉";
}
catch (Exception ex)
{
this.toolStripStatusLabel1.Text = "打開串口失敗,原因:" + ex.Message;
return;
}
}
}
3掠拳、尋卡
if (!serialPort1.IsOpen)
{
this.toolStripStatusLabel1.Text = "請(qǐng)先打開串口";
return;
}
this.toolStripStatusLabel1.Text = "未找到有效的卡";
/////////////////////////////////////////////////////////////////////////////////////////////
// 尋卡癞揉,將RFID卡號(hào)讀出來
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卡內(nèi)職員工號(hào)信息
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);// 睡眠一段時(shí)間
string response = "";
if (serialPort1.BytesToRead > 0)
{
response = serialPort1.ReadExisting();
string stuffId = ISO15693CardHandler.GenerateBlockData(response);
stuffId = stuffId.TrimStart('0');
this.toolStripStatusLabel1.Text = "讀取到職員編號(hào)為" + stuffId;
return stuffId;
}
return null;
}
5溺欧、向數(shù)據(jù)庫中插入該條員工打卡記錄
// 讀取卡上第00塊的數(shù)據(jù)喊熟,獲得職員編號(hào)
String stuffId = ReadSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00");
// 向數(shù)據(jù)庫中插入一條打卡記錄,插入成功后顯示打卡成功
String date = DateTime.Now.ToString("yyyy - MM - dd");// 默認(rèn)格式的日期和時(shí)間
String time = DateTime.Now.ToString("hh:mm:ss");
String employee_id = stuffId;
String machine_id = "1";
// 連接字符串姐刁,注意與實(shí)際環(huán)境保持一致
String connStr = ConfigurationManager.ConnectionStrings["KQ"].ConnectionString;
SqlConnection sqlConn = new SqlConnection(connStr);
try
{
// 連接數(shù)據(jù)庫
sqlConn.Open();
// 構(gòu)造命令
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字符串參數(shù)賦值
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));
// 將命令發(fā)送給數(shù)據(jù)庫
int res = cmd.ExecuteNonQuery();
// 根據(jù)返回值判斷是否插入成功
if (res != 0)
{
MessageBox.Show("信息錄入成功");
}
else
{
MessageBox.Show("信息錄入失敗");
}
}
catch (Exception exp)
{
MessageBox.Show("訪問數(shù)據(jù)庫錯(cuò)誤:" + exp.Message);
}
finally
{
sqlConn.Close();
}
四芥牌、注冊(cè)新員工卡流程
1、連接串口聂使,初始化串口壁拉,打開串口(同上)
2、寫入職員工號(hào)
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卡中寫入數(shù)據(jù)
private void bt_Register_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
this.toolStripStatusLabel1.Text = "請(qǐng)先打開串口";
return;
}
this.toolStripStatusLabel1.Text = "未找到有效的卡";
// '666'填充為'00000666'
string stuffId = this.tb_EmployeeId.Text.PadLeft(8, '0');
// '00000666'轉(zhuǎn)變成'66060000'
stuffId = ISO15693CardHandler.CovertEndian(stuffId);
// 檢查輸入數(shù)據(jù)的錯(cuò)誤
// ISO15693為32位,4字節(jié)宿礁,8字符
if (stuffId.Length != 8)
{
MessageBox.Show("請(qǐng)輸入4字節(jié)的16進(jìn)制數(shù)據(jù)!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 檢查16進(jìn)制字符錯(cuò)誤
if (!ISO15693CardHandler.CheckValidHexBytes(stuffId))
{
MessageBox.Show("寫入數(shù)據(jù)的16進(jìn)制格式錯(cuò)誤蔬芥!", "錯(cuò)誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
// 尋卡梆靖,將RFID卡號(hào)讀出來
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塊寫入職員編號(hào)數(shù)據(jù),無論成功與否笔诵,均應(yīng)該在狀態(tài)欄顯示提示信息
bool stuffIdw = this.WriteSingleBlock(ISO15693CardHandler.CovertEndian(cards[0].ID), "00", stuffId);
this.toolStripStatusLabel1.Text = "已寫入職員編號(hào)為" + stuffId;
}