D8M1可以对MIFARE块读写操作,支持1K,4K。检验KEY后返回SAK,QTAQ,UID。基于D8读卡器提供的dcrf32.dll开发。
核心代码如下:
void CD8M1Dlg::Show(CString str) { time = CTime::GetCurrentTime(); CString strTime = time.Format("%H:%M:%S"); int len = editShow.GetWindowTextLength(); editShow.SetSel(len, len); editShow.ReplaceSel(strTime + " ----> " + str + _T("\r\n")); } void CD8M1Dlg::Beer() { UCHAR SONG_TONE[] = { 212, 212, 190, 212, 159, 169, 212, 212, 190, 212, 142, 159, 212, 212, 106, 126, 159, 169, 190, 119, 119, 126, 159, 142, 159, 0 }; //生日快乐歌的音符频率表,不同频率由不同的延时来决定 UCHAR SONG_LONG[] = { 9, 3, 12, 12, 12, 24, 9, 3, 12, 12, 12, 24, 9, 3, 12, 12, 12, 12, 12, 9, 3, 12, 12, 12, 24, 0 }; //生日快乐歌节拍表,节拍决定每个音符的演奏长短 int len = 6; for (int i = 0; i < len; i++){ dc_beep(icdev, SONG_LONG[i]/3); Sleep(SONG_TONE[i]); } } void CD8M1Dlg::OnBnClickedInitcom() { //初始化串口1, icdev = dc_init(100, 9600); if ((int)icdev <= 0){ Show("Init Com Error!"); } else{ Show("Init Com OK!"); } dc_beep(icdev, 1);//蜂鸣时间,单位是10毫秒 Sleep(30); dc_beep(icdev, 1); Sleep(60); dc_beep(icdev, 1); // Beer(); } void CD8M1Dlg::OnBnClickedLoadKey() { //下载密码 CString keys; GetDlgItemText(IDC_EDIT_KEY, keys); int pos = keys.Find("\r\n"); UCHAR uKey[6]; while (pos != -1){ CString key = keys.Left(pos).Trim(); keys = keys.Mid(pos+2); unsigned char _SecNr = StrToIntA(key.Left(2)); CString keyN = key.Mid(3); if (_SecNr > 40){ Show("扇区号有误,大于40扇区..."); } memset(uKey, 0, 6); Alg::HexToAsc(keyN, uKey); int st = dc_load_key(icdev, GetKeyMode(), _SecNr, uKey); if (st != 0){ Show(IntToStr(_SecNr) + ": 扇区密钥写入失败..."); return; } pos = keys.Find("\r\n"); } Show("载入KEY成功...\r\n"); } void CD8M1Dlg::OnBnClickedVerifyCard() { //寻卡函数, // int st = dc_card(icdev, 0, &cardsnr); CString linstr; unsigned short tagType; int st = dc_request(icdev, 0, &tagType); if (st != 0) { Show("Find Card Error!"); return; } else { Show("Find Card Ok!"); linstr.Format("%04d", tagType); Show("ATQA:" + linstr); } st = dc_anticoll(icdev, 0, &cardsnr); if (st != 0) { Show("Find Card Error!"); return; } else { linstr.Format("%X", cardsnr); Show("UID:" + linstr); } UCHAR size; st = dc_select(icdev, cardsnr, &size); if (st != 0) { Show("select Card Error!"); return; } else { linstr.Format("%X", size); Show("SAK:" + linstr); } //核对密码 int sec = GetSecNr(); int keyMode = GetKeyMode(); for (int i = 0; i < sec; i++){ st = dc_authentication(icdev, keyMode, i); if (st != 0){ Show(IntToStr(i) + ": Auth Card Error..."); return; } } Show("核对密码完成...\r\n"); } void CD8M1Dlg::OnBnClickedRead() { EditBlock.SetWindowTextA(""); ClearShow(); int adrs = GetBlockNr(); CString sData; UCHAR pData[32]; unsigned long _NSnr; for (int i = 0; i < adrs; i++){ //int st = dc_read(icdev, i, data); if (IsCheckW()){//only read block 3 if ((i < 128) && ((i+1) % 4 != 0)){ continue; } if ((i > 128) && ((i+1) % 16 != 0)){ continue; } } memset(pData, 0, 32); int st = dc_HL_read(icdev, 0, i, cardsnr, pData, &_NSnr); Alg::AscToHex(pData, 16, sData); if (st != 0){ Show(IntToStr(i) + ": 读取失败..."); continue; } else{ Show(IntToStr(i) + ": 读取成功," + sData); } int len = EditBlock.GetWindowTextLength(); EditBlock.SetSel(len, len); EditBlock.ReplaceSel(IntToStr(i) + ":" + sData + _T("\r\n")); } Show("读取块完成...\r\n"); } void CD8M1Dlg::OnBnClickedWrite() { ClearShow(); CString blocks; int adrs = GetBlockNr(); GetDlgItemText(IDC_EDIT_BLOCK, blocks); int pos = blocks.Find("\r\n"); UCHAR uData[16]; while (pos != -1){ CString block = blocks.Left(pos).Trim(); blocks = blocks.Mid(pos + 2); int posn = block.Find(':'); if (posn == -1){ Show("块地址值有误," + block); return; } unsigned char adr = StrToIntA(block.Left(posn)); CString data = block.Mid(posn + 1); if (data.GetLength() != 32){ Show("块值长度有误," + block); } if (adr > adrs){ Show("块地址越界:" + IntToStr(adr)); return; } Alg::HexToAsc(data, uData); int st = dc_HL_write(icdev, 0, adr, &cardsnr, uData); if (st != 0){ Show(IntToStr(adr) + ": 写入失败..."); } else{ Show(IntToStr(adr) + ": 写入成功,写入值 " + data); } pos = blocks.Find("\r\n"); } Show("写入块完成...\r\n"); } void CD8M1Dlg::OnClose() { dc_exit(icdev); CDialogEx::OnClose(); }
文/闫鑫原创 转载请注明出处http://blog.csdn.net/yxstars/article/details/38108709