首页 > 代码库 > MIFARE系列8——D8M1.exe

MIFARE系列8——D8M1.exe

软件名:D8M1.exe
更新时间:2014.06.28
操作系统:windowAll
外部设备:D8读卡器


D8M1可以对MIFARE块读写操作,支持1K,4K。检验KEY后返回SAK,QTAQ,UID。基于D8读卡器提供的dcrf32.dll开发。



D8型IC卡读写器是开发IC卡相关产品及系统集成必备的前端处理设备,可读写双界面射频智能卡或单独的非接触式射频卡,通过RS232串行接口或USB口实现同PC机及相关设备的连接,USB采用无驱技术,可方便用户的安装使用,它对多卡的支持可用于安全发卡及那些安全要求较高的领域需要。



核心代码如下:


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 = http://www.mamicode.com/block.Mid(posn + 1);>
软件下载地址:http://download.csdn.net/detail/yxstars/7675429