本文參考手冊:《TFT 系列離線通訊開發包開發手冊》 主要內容: 1、定時同步考勤記錄到資料庫 2、用戶信息、指紋操作 3、考勤記錄批量操作 正文 1、定時同步考勤記錄 1-1、定義全局變數 1 #region 窗體變數 2 string USERID = "";//同步到資料庫的參數 3 stri ...
本文參考手冊:《TFT 系列離線通訊開發包開發手冊》
主要內容:
1、定時同步考勤記錄到資料庫
2、用戶信息、指紋操作
3、考勤記錄批量操作
正文
1、定時同步考勤記錄
1-1、定義全局變數
1 #region 窗體變數 2 string USERID = "";//同步到資料庫的參數 3 string CHARGE_TIME = "";//同步到資料庫的參數 4 string VERIFYMODE = "";//同步到資料庫的參數 5 string WORKCODE = "";//同步到資料庫的參數 6 string PASSWORD = "";//同步到資料庫的參數 7 string PRIVILEGE = "";//同步到資料庫的參數 8 string ENABLE = "";//同步到資料庫的參數 9 System.Timers.Timer atimer = new System.Timers.Timer(); //定時任務 10 #endregion 11 12 #region 考勤機變數 13 public CZKEMClass axCZKEM1 = new CZKEMClass(); 14 private bool bIsConnected = false; 15 private int iMachineNumber = 1; 16 17 string sdwEnrollNumber = "";//用戶ID 18 string sName = "";//姓名 19 string sPassword = "";//密碼 20 int iPrivilege = 0; 21 bool bEnabled = false; 22 string sCardnumber = "";//卡號 23 int dwWorkCode = 0; 24 int dwVerifyMode; 25 int dwInOutMode; 26 int dwYear;//刷卡時間 27 int dwMonth; 28 int dwDay; 29 int dwHour; 30 int dwMinute; 31 int dwSecond; 32 string verifyMode = ""; 33 string privilege = ""; 34 string enabled = ""; 35 #endregionView Code
1-2、連接考勤機
1 #region 考勤機功能:連接 2 private void btnConnect_Click(object sender, EventArgs e) 3 { 4 if (txtIP.Text.Trim() == "" || txtPort.Text.Trim() == "")//考勤機的IP地址與埠 5 { 6 MessageBox.Show("IP and Port cannot be null", "Error"); 7 return; 8 } 9 int idwErrorCode = 0; 10 11 Cursor = Cursors.WaitCursor; 12 if (btnConnect.Text == "DisConnect") 13 { 14 axCZKEM1.Disconnect(); 15 bIsConnected = false; 16 btnConnect.Text = "Connect"; 17 lblState.Text = "Current State:DisConnected"; 18 Cursor = Cursors.Default; 19 return; 20 } 21 22 bIsConnected = axCZKEM1.Connect_Net(txtIP.Text, Convert.ToInt32(txtPort.Text)); 23 if (bIsConnected == true) 24 { 25 btnConnect.Text = "DisConnect"; 26 btnConnect.Refresh(); 27 lblState.Text = "Current State:Connected"; 28 iMachineNumber = 1;//In fact,when you are using the tcp/ip communication,this parameter will be ignored,that is any integer will all right.Here we use 1. 29 axCZKEM1.RegEvent(iMachineNumber, 65535);//Here you can register the realtime events that you want to be triggered(the parameters 65535 means registering all) 30 } 31 else 32 { 33 axCZKEM1.GetLastError(ref idwErrorCode); 34 MessageBox.Show("Unable to connect the device,ErrorCode=" + idwErrorCode.ToString(), "Error"); 35 } 36 Cursor = Cursors.Default; 37 } 38 #endregionView Code
1-3、配置定時任務
(這裡定時任務用到的是System.Timers.Timer,我在使用過程中遇到一個問題:開啟定時任務創建一個線程後調用atimer.Stop()無法關閉,只有關閉整個應用程式才會結束這個線程。如果知道怎麼解決的朋友還請告知^ ^~)
1 #region 定時任務配置 2 public void Timing_Task() 3 { 4 atimer.Enabled = true; 5 atimer.Interval = 60000; //執行間隔時間,單位為毫秒; 這裡實際間隔為1分鐘 6 atimer.Elapsed += new System.Timers.ElapsedEventHandler(getLogData); 7 } 8 public void Timing_Task_Star() 9 { 10 atimer.Enabled = true; 11 atimer.Start(); 12 } 13 public void Timing_Task_Close() 14 { 15 atimer.Elapsed -= new System.Timers.ElapsedEventHandler(getLogData); 16 atimer.Stop(); 17 atimer.Dispose(); 18 } 19 #endregionView Code
1-4、同步程式
1 #region 執行定時任務(同步數據到資料庫) 2 private void getLogData(object source, ElapsedEventArgs e) 3 { 4 if (bIsConnected == false) 5 { 6 MessageBox.Show("Please connect the device first!", "Error"); 7 return; 8 } 9 Cursor = Cursors.WaitCursor; 10 //先禁用機器 11 axCZKEM1.EnableDevice(iMachineNumber, false); 12 //獲取所有用戶的考勤記錄到緩存區 13 axCZKEM1.ReadAllGLogData(iMachineNumber); 14 while (axCZKEM1.SSR_GetGeneralLogData(iMachineNumber, out sdwEnrollNumber, out dwVerifyMode, out dwInOutMode, out dwYear, out dwMonth, out dwDay, out dwHour, out dwMinute, out dwSecond, ref dwWorkCode))//get user information from memory 15 { 16 switch (dwVerifyMode) 17 { 18 case 0: 19 verifyMode = "密碼驗證"; 20 break; 21 case 1: 22 verifyMode = "指紋驗證"; 23 break; 24 case 2: 25 verifyMode = "卡驗證"; 26 break; 27 default: 28 verifyMode = "其他驗證"; 29 break; 30 } 31 //迴圈獲取用戶信息 32 axCZKEM1.SSR_GetUserInfo(iMachineNumber, sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled); 33 if (axCZKEM1.GetStrCardNumber(out sCardnumber))//get the card number from the memory 34 { 35 switch (iPrivilege) 36 { 37 case 0: 38 privilege = "普通用戶"; 39 break; 40 case 3: 41 privilege = "管理員"; 42 break; 43 default: 44 privilege = "其他許可權"; 45 break; 46 } 47 switch (bEnabled) 48 { 49 case true: 50 enabled = "啟用"; 51 break; 52 case false: 53 enabled = "禁用"; 54 break; 55 } 56 USERID = sdwEnrollNumber; 57 CHARGE_TIME = dwYear.ToString() + "-" + dwMonth.ToString() + "-" + dwDay.ToString() + " " + dwHour.ToString() + ":" + dwMinute.ToString() + ":" + dwSecond.ToString(); 58 VERIFYMODE = verifyMode; 59 WORKCODE = dwWorkCode.ToString(); 60 //NAME = sName+ "\0"; 61 //NAME = NAME.Replace("\0", ""); 62 PASSWORD = sPassword; 63 PRIVILEGE = privilege; 64 ENABLE = enabled; 65 string ID = dwYear.ToString() + dwMonth.ToString() + dwDay.ToString() + dwHour.ToString() + dwMinute.ToString() + dwSecond.ToString(); 66 DateTime now = DateTime.Now; 67 string thisyear = now.Year.ToString(); 68 string thismonth = now.Month.ToString(); 69 string thisday = now.Day.ToString(); 70 if (dwYear.ToString() == thisyear && dwMonth.ToString() == thismonth && dwDay.ToString() == thisday) 71 { 72 //取時間為今天的刷卡記錄,若ID不存在則插入 73 //INSERT WHEN NOT EXISTS (SELECT T.ID FROM TABLE T WHERE T.T_ID = '??') THEN INTO TABLE(...) VALUES (...) 74 } 75 } 76 } 77 axCZKEM1.EnableDevice(iMachineNumber, true);//enable the device 78 Cursor = Cursors.Default; 79 80 checkDing();//檢測是否超時 81 } 82 #endregionView Code
2、用戶信息/指紋
2-1、獲取考勤機內已註冊用戶到list
(註意:如果考勤機內沒有註冊指紋信息,代碼裡面有查詢指紋,那麼載入會變得非常慢。要提前註釋掉查詢指紋的代碼!)
1 #region 考勤機功能:獲取用戶 2 private void btnGetStrCardNumber_Click(object sender, EventArgs e) 3 { 4 if (bIsConnected == false) 5 { 6 MessageBox.Show("Please connect the device first!", "Error"); 7 return; 8 } 9 lvCard.Items.Clear(); 10 lvCard.BeginUpdate(); 11 Cursor = Cursors.WaitCursor; 12 //先禁用機器 13 axCZKEM1.EnableDevice(iMachineNumber, false);//disable the device 14 //獲取所有用戶的記錄到緩存區 15 axCZKEM1.ReadAllUserID(iMachineNumber);//獲取所有信息到緩存區(提高效率) 16 axCZKEM1.ReadAllTemplate(iMachineNumber);//獲取所有指紋信息到緩存區(提高效率) 17 while (axCZKEM1.SSR_GetAllUserInfo(iMachineNumber, out sdwEnrollNumber, out sName, out sPassword, out iPrivilege, out bEnabled))//get user information from memory 18 { 19 //迴圈獲取用戶信息 20 if (axCZKEM1.GetStrCardNumber(out sCardnumber))//get the card number from the memory 21 { 22 ListViewItem list = new ListViewItem(); 23 list.Text = sdwEnrollNumber; 24 list.SubItems.Add(sName); 25 list.SubItems.Add(sCardnumber); 26 list.SubItems.Add(iPrivilege.ToString()); 27 list.SubItems.Add(sPassword); 28 if (bEnabled == true) 29 { 30 list.SubItems.Add("true"); 31 } 32 else 33 { 34 list.SubItems.Add("false"); 35 } 36 37 for (int dwFingerIndex = 0; dwFingerIndex < 10; dwFingerIndex++) 38 { 39 if (axCZKEM1.SSR_GetUserTmpStr(iMachineNumber, sdwEnrollNumber, dwFingerIndex, out dwTmpData, out dwTmpLength)) 40 { 41 list.SubItems.Add(dwTmpData); 42 } 43 else 44 { 45 list.SubItems.Add(""); 46 } 47 } 48 49 lvCard.Items.Add(list); 50 } 51 } 52 53 axCZKEM1.EnableDevice(iMachineNumber, true);//enable the device 54 lvCard.EndUpdate(); 55 Cursor = Cursors.Default; 56 } 57 #endregionView Code
2-2、註冊單個用戶到考勤機
1 #region 考勤機功能:註冊用戶 2 private void btnSetStrCardNumber_Click(object sender, EventArgs e) 3 { 4 if (bIsConnected == false) 5 { 6 MessageBox.Show("Please connect the device first!", "Error"); 7 return; 8 } 9 //下發用戶到機器 10 //1、設置卡號 11 //string CardNo = System.Convert.ToInt32(txtCardNo.Text, 16).ToString();//轉換16進位到十進位 12 axCZKEM1.SetStrCardNumber(txtCardNo.Text); 13 //2、設置用戶信息 14 axCZKEM1.SSR_SetUserInfo(iMachineNumber, "用戶ID", "用戶姓名", "密碼", 0, true); 15 } 16 #endregionView Code
2-3、批量註冊用戶
1 #region 考勤機功能:批量註冊用戶 2 private void btnSetStrCardNumbers_Click(object sender, EventArgs e) 3 { 4 if (XCMessageBox.Show("確定?", XCMessageBox.Buttons.OKCancel, XCMessageBox.Icon.Question) == DialogResult.OK) 5 { 6 System.Data.DataTable dt = getEmp();//getEmp()獲取資料庫中需要註冊的用戶 7 string[] useruid = new string[dt.Rows.Count]; 8 string[] useruname = new string[dt.Rows.Count]; 9 string[] cardid = new string[dt.Rows.Count]; 10 for (int i = 0; i < dt.Rows.Count; i++) 11 { 12 DataRow dr = dt.Rows[i]; 13 useruid[i] = Convert.ToString(dr["T_USERID"]); 14 useruname[i] = Convert.ToString(dr["T_USERNAME"]); 15 cardid[i] = Convert.ToString(dr["CARDID"]); 16 //16進位轉10進位 17 //string CardNo = System.Convert.ToInt32(cardid[i], 16).ToString(); 18 axCZKEM1.SetStrCardNumber(cardid[i]); 19 //2、設置用戶信息 20 bool a = axCZKEM1.SSR_SetUserInfo(iMachineNumber, useruid[i], useruname[i], useruid[i], 0, true); 21 } 22 } 23 } 24 #endregionView Code
2-4、刪除用戶
1 #region 考勤機功能:刪除用戶 2 private void btnDelStrCardNumber_Click(object sender, EventArgs e) 3 { 4 if (bIsConnected == false) 5 { 6 MessageBox.Show("Please connect the device first!", "Error"); 7 return; 8 } 9 if (XCMessageBox.Show("確定?", XCMessageBox.Buttons.OKCancel, XCMessageBox.Icon.Question) == DialogResult.OK) 10 { 11 axCZKEM1.SSR_DeleteEnrollData(iMachineNumber, "用戶ID", 12); 12 } 13 } 14 #endregionView Code
2-5、增刪指紋
(這裡有一點要註意:考勤機演算法不同的話,增加指紋所調用的方法不同,使用前先查看考勤機演算法)
1 #region 考勤機功能:增刪指紋 2 private void btnDelTmp_Click(object sender, EventArgs e) 3 { 4 if (bIsConnected == false) 5 { 6 MessageBox.Show("Please connect the device first!", "Error"); 7 return; 8 } 9 if (XCMessageBox.Show("確定?", XCMessageBox.Buttons.OKCancel, XCMessageBox.Icon.Question) == DialogResult.OK) 10 { 11 bool a = axCZKEM1.SSR_DelUserTmp(iMachineNumber, "用戶ID", "(int)第幾個指紋"); 12 //bool a = axCZKEM1.SSR_DeleteEnrollDataExt(iMachineNumber, txtEmpNo.Text, 13);//刪除所有指紋 13 } 14 } 15 16 private void btnAddTmp_Click(object sender, EventArgs e) 17 { 18 if (bIsConnected == false) 19 { 20 MessageBox.Show("Please connect the device first!", "Error"); 21 return; 22 } 23 System.Data.DataTable dt = new System.Data.DataTable(); 24 for (int i = 0; i < 10; i++) 25 { 26 27 //迴圈獲取十個指紋 28 dt = getEmpTmp(i.ToString());//獲取資料庫中第i個指紋 29 for (int j = 0; j < dt.Rows.Count; j++) 30 { 31 32 //迴圈資料庫中的用戶數 33 USERID = dt.Rows[j]["資料庫中用戶ID"].ToString(); 34 dwTmpData = dt.Rows[j]["資料庫中第i個指紋"].ToString(); 35 bool a = axCZKEM1.SetUserTmpExStr(iMachineNumber, USERID, i, 1, dwTmpData); 36 } 37 } 38 39 //獲取演算法版本 40 /*string sValue; 41 axCZKEM1.GetSysOption(1, "~ZKFPVersion", out sValue); 42 txtInfo.Text = sValue;*/ 43 } 44 #endregionView Code
2-6、清空考勤機所有註冊的用戶
1 #region 考勤機功能:清空考勤機用戶 2 private void btnDel_Click(object sender, EventArgs e) 3 { 4 if (bIsConnected == false) 5 { 6 MessageBox.Show("Please connect the device first!", "Error"); 7 return