⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dlgrfidt2.cpp

📁 rfid在wince的平台的驱动
💻 CPP
字号:
// DlgRFIDT2.cpp : implementation file
//

#include "stdafx.h"
#include "capture.h"
#include "DlgRFIDT2.h"
#include "prompt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgRFIDT2 dialog


CDlgRFIDT2::CDlgRFIDT2(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgRFIDT2::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgRFIDT2)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CDlgRFIDT2::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgRFIDT2)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgRFIDT2, CDialog)
	//{{AFX_MSG_MAP(CDlgRFIDT2)
	ON_EN_CHANGE(IDC_EDITRFID, OnChangeEditrfid)
	ON_BN_CLICKED(IDC_BTNWRITE, OnBtnwrite)
	ON_BN_CLICKED(IDC_BTNREAD, OnBtnread)
//	ON_EN_CHANGE(IDC_EDIT_WRITEDATAINFO, OnChangeEditWritedatainfo)
	ON_EN_CHANGE(IDC_EDIT_RWDATALENINFO, OnChangeEditRwdataleninfo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgRFIDT2 message handlers

void CDlgRFIDT2::OnChangeEditrfid() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	//SetDlgItemText(IDC_EDITRFID, _T(""));
}

void CDlgRFIDT2::OnBtnwrite() 
{
	// TODO: Add your control notification handler code here
	SetDlgItemText(IDC_EDIT_USINGTIMEINFO, (CString)"");
	LARGE_INTEGER g_frequency;
	LARGE_INTEGER Start;
	LARGE_INTEGER End;
	double Time;
	QueryPerformanceFrequency(&g_frequency);//查询系统频率
	QueryPerformanceCounter(&Start);//查询系统运行滴答数,
	//每次执行QueryPerformanceCounter 得到的是你从开机到执行这个函数时的系统滴答数


	TCHAR m_wbuf[BUF_MAX_LEN];
	char m_szbuf[BUF_MAX_LEN];
	ZeroMemory(m_wbuf, BUF_MAX_LEN);
	ZeroMemory(m_szbuf, BUF_MAX_LEN);
	GetDlgItemText(IDC_EDITRFID, m_wbuf, MAX_PATH-1);
	WideCharToMultiByte(CP_ACP, 0, m_wbuf, -1, m_szbuf, MAX_PATH, NULL, NULL);

	int m_szbufLen = 0;
	char nBlockNO;

	// get block no	
	TCHAR m_wbufBlockNO[BUF_MAX_LEN];
	char m_szbufBlockNO[BUF_MAX_LEN];
	ZeroMemory(m_wbufBlockNO, BUF_MAX_LEN);
	ZeroMemory(m_szbufBlockNO, BUF_MAX_LEN);
	GetDlgItemText(IDC_EDIT_RWFROMBLOCKINF, m_wbufBlockNO, MAX_PATH-1);
	WideCharToMultiByte(CP_ACP, 0, m_wbufBlockNO, -1, m_szbufBlockNO, MAX_PATH, NULL, NULL);
	if (m_szbufBlockNO[0] == 0x00 && m_szbufBlockNO[1] == 0x00)
		nBlockNO = 0;
	nBlockNO = atoi(m_szbufBlockNO);
	if (nBlockNO > 63)
	{
		SetDlgItemText(IDC_EDITRFID, (CString)"");
		SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)"The block must be less than 63!");
		return;
	}
	if (nBlockNO < 0)
	{
		SetDlgItemText(IDC_EDITRFID, (CString)"");
		SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)"The block must be more than 0!");
		return;
	}

	m_szbufLen = m_iWriteDataLen = strlen(m_szbuf);
	char m_tempBuf[50] = {0};
	if (m_iWriteDataLen+ 4*nBlockNO > 256 )
	{
		SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)"The data must be less than 256 bytes!");
		return;
	}
	else
	{
		sprintf(m_tempBuf, "Write %d bytes to the T2 card!", m_iWriteDataLen);
		SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)m_tempBuf);
	}


	if (!m_ComMgr.IsOpen())
	{
		m_ComMgr.Open(3,38400,3000);
		Sleep(400);
	}
	if (m_ComMgr.IsOpen())
	{
		int m_iReadDataRe = 0;
		
		if(m_szbufLen+4*nBlockNO < 256)
			m_szbufLen += 1;

		memset(szSendBuf, 0x00, BUF_MAX_LEN);
		
		m_szbufLen++;
		szSendBuf[0]=0x02;
		szSendBuf[1]=m_szbufLen/256;
		szSendBuf[2]=(char)m_szbufLen;
		szSendBuf[3]=0x34;
		szSendBuf[4]=nBlockNO;// block_NO
		strcpy(szSendBuf+5, m_szbuf);
		m_iReadDataRe = m_ComMgr.Write(szSendBuf, m_szbufLen+4);
		memset(szSendBuf, 0x00, BUF_MAX_LEN);

		m_iReadDataRe = 4;
		while (m_iReadDataRe--)
		{
			m_ComMgr.ReadSync(szSendBuf, 1, 500);
			if (szSendBuf[0] == 0x02)
			{
				m_ComMgr.ReadSync(szSendBuf+1, 2, 200);
				nLength = szSendBuf[1] *256 + szSendBuf[2];
				m_ComMgr.ReadSync(szSendBuf+3, nLength+1, 1000);
				break;
			}		
		}
//		m_iReadDataRe = m_ComMgr.ReadSync(szSendBuf, 4, 3000);

		if (szSendBuf[3]==0x34)
		{
			beep();
			SetDlgItemText(IDC_EDITRFID, (CString)"Write data ok!");
		}
		else
			SetDlgItemText(IDC_EDITRFID, (CString)"Write data error!");

	
//		sprintf(szSendBuf+4+strlen(szSendBuf+4), "\r\nSend:%x %x\r\nRev: %x %x %x %x", 
//			m_szbufLen/256, m_szbufLen%256, szSendBuf[0],szSendBuf[1],szSendBuf[2],szSendBuf[3]);
//		SetDlgItemText(IDC_EDITRFID, (CString)(szSendBuf+4));

		QueryPerformanceCounter(&End);
		//前后两次滴答数的差除以系统频率就是时间差,单位是豪秒
		//Time = (double)(End.QuadPart-Start.QuadPart)/(double)(g_frequency.QuadPart)*1000.0;
		//前后两次滴答数的差除以系统频率就是时间差,单位是秒
		Time = (double)(End.QuadPart-Start.QuadPart)/(double)(g_frequency.QuadPart);
		
		memset(m_tempBuf, 0x00, 50);
		sprintf(m_tempBuf, "Write data using %f s", Time);
		SetDlgItemText(IDC_EDIT_USINGTIMEINFO, (CString)m_tempBuf);

	//	Sleep(1000);
//		m_ComMgr.Close();
//		m_ComMgr.Open(4,38400,3000);
//		m_ComMgr.Close();
	}
}

void CDlgRFIDT2::OnBtnread() 
{
	// TODO: Add your control notification handler code here
	SetDlgItemText(IDC_EDIT_USINGTIMEINFO, (CString)"");
	LARGE_INTEGER g_frequency;
	LARGE_INTEGER Start;
	LARGE_INTEGER End;
	double Time;
	int nLength;
	char nBlockNO;

	// get block no	
	TCHAR m_wbufBlockNO[BUF_MAX_LEN];
	char m_szbufBlockNO[BUF_MAX_LEN];
	ZeroMemory(m_wbufBlockNO, BUF_MAX_LEN);
	ZeroMemory(m_szbufBlockNO, BUF_MAX_LEN);
	GetDlgItemText(IDC_EDIT_RWFROMBLOCKINF, m_wbufBlockNO, MAX_PATH-1);
	WideCharToMultiByte(CP_ACP, 0, m_wbufBlockNO, -1, m_szbufBlockNO, MAX_PATH, NULL, NULL);
	if (m_szbufBlockNO[0] == 0x00 && m_szbufBlockNO[1] == 0x00)
		nBlockNO = 0;
	nBlockNO = atoi(m_szbufBlockNO);
	if (nBlockNO > 63)
	{
		SetDlgItemText(IDC_EDITRFID, (CString)"");
		SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)"The block must be less than 63!");
		return;
	}
	if (nBlockNO < 0)
	{
		SetDlgItemText(IDC_EDITRFID, (CString)"");
		SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)"The block must be more than 0!");
		return;
	}

	QueryPerformanceFrequency(&g_frequency);
	QueryPerformanceCounter(&Start);

	if (!m_ComMgr.IsOpen())
	{
		m_ComMgr.Open(3,38400,3000);
		Sleep(400);
	}
	if (m_ComMgr.IsOpen())
	{
		memset(szSendBuf, 0x00, BUF_MAX_LEN);

		szSendBuf[0]=0x02;// start
		szSendBuf[1]=0x00;// length
		szSendBuf[2]=0x03;// length
		szSendBuf[3]=0x32;// cmd
		szSendBuf[4]=nBlockNO;// block_NO

		nLength = 256;
		nLength -= nBlockNO*4;
		szSendBuf[5]=nLength/256;// data length
		szSendBuf[6]=nLength%256;// data length
		nLength = 7;

		int m_iReadDataRe = 0;	
		m_iReadDataRe =m_ComMgr.Write(szSendBuf, nLength);

		nLength = szSendBuf[5] * 256 + szSendBuf[6];
		memset(szSendBuf, 0x00, BUF_MAX_LEN);
		

		m_iReadDataRe = 4;
		while (m_iReadDataRe--)
		{
			m_ComMgr.ReadSync(szSendBuf, 1, 500);
			if (szSendBuf[0] == 0x02)
			{
				m_ComMgr.ReadSync(szSendBuf+1, 2, 200);
				nLength = szSendBuf[1] *256 + szSendBuf[2];
				m_ComMgr.ReadSync(szSendBuf+3, nLength+1, 1000);
				break;
			}		
		}		
//		m_iReadDataRe = m_ComMgr.ReadSync(szSendBuf, nLength+4, 2000);
		
		char m_tempBuf[50]={0};
		if (szSendBuf[3]==0x32)
		{
			beep();
			char m_readDataBuf[BUF_MAX_LEN] = {0};
			strcpy(m_readDataBuf, szSendBuf+4);
			m_iReadDataLen = strlen(m_readDataBuf);
			if (m_iReadDataLen >= 256)
				m_iReadDataLen = 256;
			
			sprintf(m_tempBuf, "Read %d bytes from the T2 card!", m_iReadDataLen);
			SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)m_tempBuf);
			
			// HEX
			if (1)
			{
				char cBuf[2048];				
				memset(cBuf, 0x00, sizeof(cBuf));
				for (int i = 0; i <(nLength+3)/4; i++)
				{
					sprintf(cBuf+strlen(cBuf), "BLock %02d: %02x %02x %02x %02x\r\n",
						   i+nBlockNO, szSendBuf[4+i*4], szSendBuf[5+i*4],
						   szSendBuf[6+i*4], szSendBuf[7+i*4])
				}

				SetDlgItemText(IDC_EDITRFID, (CString)cBuf);

			}
			else
			{
				// default for ASCII	
				SetDlgItemText(IDC_EDITRFID, (CString)(szSendBuf+4));
			}
		
			
		}
		else
		{
			m_iReadDataLen = 0;
			sprintf(m_tempBuf, "Read %d bytes from the T2 card!", m_iReadDataLen);
			SetDlgItemText(IDC_EDIT_RWDATALENINFO, (CString)m_tempBuf);
			SetDlgItemText(IDC_EDITRFID, (CString)"Read data error!");
		}

		QueryPerformanceCounter(&End);
		//Time = (double)(End.QuadPart-Start.QuadPart)/(double)(g_frequency.QuadPart)*1000.0;
		Time = (double)(End.QuadPart-Start.QuadPart)/(double)(g_frequency.QuadPart);
		memset(m_tempBuf, 0x00, 50);
		sprintf(m_tempBuf, "Read data using %f s", Time);
		SetDlgItemText(IDC_EDIT_USINGTIMEINFO, (CString)m_tempBuf);

//		m_ComMgr.Close();
//		m_ComMgr.Open(4,38400,3000);
//		m_ComMgr.Close();
	}

}

void CDlgRFIDT2::OnChangeEditWritedatainfo() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CDlgRFIDT2::OnChangeEditRwdataleninfo() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -