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

📄 readpage.cpp

📁 RFID的源代码
💻 CPP
字号:
// ReadPage.cpp : implementation file
//

#include "stdafx.h"
#include "RFID.h"
#include "ReadPage.h"
#include "SYSCTL.h"

#include <PM.h>
#include <Msgqueue.h>
#include "Pwinuser.h"

#define QUEUE_ENTRIES   3
#define MAX_NAMELEN     200
#define QUEUE_SIZE      (QUEUE_ENTRIES * (sizeof(POWER_BROADCAST) + MAX_NAMELEN))

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


CWinThread* AutoScanWinThread;
CWinThread* hPowerWinThread;
CWinThread* hSuspendThread;
UINT ContinueModeFunc(LPVOID pParam);//for Continue Mode 
UINT SusThreadFunc(LPVOID pvarg); //for suspend detect
/////////////////////////////////////////////////////////////////////////////
// CReadPage property page

IMPLEMENT_DYNCREATE(CReadPage, CPropertyPage)

CReadPage::CReadPage() : CPropertyPage(CReadPage::IDD)
{
	//{{AFX_DATA_INIT(CReadPage)
	//}}AFX_DATA_INIT
}

CReadPage::~CReadPage()
{
}

void CReadPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CReadPage)
	DDX_Control(pDX, IDC_Block10, m_BlockList10);
	DDX_Control(pDX, IDC_Block1, m_BlockList);
	DDX_Control(pDX, IDC_DataList, m_DataList);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CReadPage, CPropertyPage)
	//{{AFX_MSG_MAP(CReadPage)
	ON_BN_CLICKED(IDC_Open, OnBtnOpen)
	ON_BN_CLICKED(IDC_Close, OnBtnClose)
	ON_BN_CLICKED(IDC_ReadUID, OnReadUID)
	ON_BN_CLICKED(IDC_TagSysInfo, OnTagSysInfo)
	ON_BN_CLICKED(IDC_TagData, OnTagData)
	ON_BN_CLICKED(IDC_ReadBlock, OnReadBlock)
	ON_BN_CLICKED(IDC_Start, OnStart)
	ON_BN_CLICKED(IDC_Stop, OnStop)
	ON_BN_CLICKED(IDC_Clear, OnClear)
	ON_BN_CLICKED(IDC_Save, OnSave)
	ON_BN_CLICKED(IDC_Read14443, OnRead14443)
	ON_BN_CLICKED(IDC_Reset, OnBtnReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CReadPage message handlers
BOOL CReadPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
    TCHAR* strNum[] ={L"0",L"1",L"2",L"3",L"4",L"5",L"6",L"7",L"8",
	L"9"};
	TCHAR* strNum10[] ={L"0",L"1",L"2"};
	for(int i = 0 ; i<10;i++)
	{
		m_BlockList.AddString(*(strNum+i));
		if(i<3)
		{
			m_BlockList10.AddString(*(strNum10+i));
		}
	}
     m_BlockList.SetCurSel(0);
	 m_BlockList10.SetCurSel(0);

	 hPowerWinThread=AfxBeginThread(SusThreadFunc,this,THREAD_PRIORITY_NORMAL,0,0,NULL);
	 CloseHandle( hPowerWinThread);
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CReadPage::OnBtnOpen() 
{
/*	HMODULE hSYSCTL = LoadLibrary(_T("SYSCTL.dll"));	
	
	if( hSYSCTL )
	{		
		PFN_SET_RFID_POWER SET_RFID_POWER=(PFN_SET_RFID_POWER)GetProcAddress(hSYSCTL, _T("SysSetRFIDPower"));
		BOOL T =SET_RFID_POWER(TRUE);			
		FreeLibrary(hSYSCTL);
 
		if( fOpenDev == 0 )
			Amic_DeviceClose();
	
		fOpenDev = Amic_DeviceOpen("COM1:");		
		MessageBox(_T("COM 7 Opened and Power is ON"));
	}
	else
		MessageBox(_T("Load DLL file fail"));

	//if( hSYSCTL )
	//	FreeLibrary(hSYSCTL);
	/*
	if (fOpenDev == 1) 
	{
		fOpenDev = Amic_DeviceOpen("COM7:");
		if(fOpenDev==0);
		MessageBox(_T("Device Open"),L"RFIDDEMO");
	}
	else if (fOpenDev == 0) 
	{

		MessageBox(_T("Device Already Open"),L"RFIDDEMO");

	}	
	*/	
	DualA_Power_On();


	if (fOpenDev == 1) 
	{
		fOpenDev = Amic_DeviceOpen("COM6:");
		if(fOpenDev==0);
		MessageBox(_T("Device Open"),L"RFIDDEMO");
	}
	else if (fOpenDev == 0) 
	{

		MessageBox(_T("Device Already Open"),L"RFIDDEMO");

	}
}

void CReadPage::OnBtnClose() 
{
	HMODULE hSYSCTL = LoadLibrary(_T("SYSCTL.dll"));
	if ( hSYSCTL )
	{
		/*
		PFN_SET_RFID_POWER SET_RFID_POWER=(PFN_SET_RFID_POWER)GetProcAddress(hSYSCTL, _T("SysSetRFIDPower"));
		SET_RFID_POWER(FALSE);
		FreeLibrary(hSYSCTL);

		Amic_ContinuousModeOFF();//off ContiuneMode
		fContinueMode = false;
		Amic_DeviceClose();
		fOpenDev = 1;
		MessageBox(_T("COM7 closed and Power is OFF."),L"RFIDDEMO");		
		*/
		Amic_ContinuousModeOFF();//off ContiuneMode
		Amic_DeviceClose();

		PFN_SET_RFID_POWER SET_RFID_POWER=(PFN_SET_RFID_POWER)GetProcAddress(hSYSCTL, _T("SysSetRFIDPower"));
		SET_RFID_POWER(FALSE);
		FreeLibrary(hSYSCTL);

		fContinueMode = false;
		fOpenDev = 1;

		MessageBox(_T("COM7 closed and Power is OFF."),L"RFIDDEMO");
	}
	else
		MessageBox(_T("Load DLL file fail"));
}

void CReadPage::OnReadUID() 
{

	if(fOpenDev == 0&&fContinueMode ==FALSE)
	{
		CString readUID;

		if(Amic_Inventory()==0)
		{
			Sleep(500);
			readUID = Amic_GetInventoryData();

			int UIDLen = readUID.GetLength();
			if(UIDLen!=0)
			{

				for(int i = 0;i<(UIDLen/16);i++)
				{
					m_DataList.InsertString(0,readUID.Mid(i*16,16));


				}
				PlaySound(L"windows\\ScanSuccess.wav", NULL, SND_ASYNC | SND_FILENAME);
			}
			

		}
		Amic_ClearAll15693Data();
	}	
}

void CReadPage::OnTagSysInfo() 
{
	if(fOpenDev == 0&&fContinueMode ==FALSE)
	{
		CString err_code;
		CString response_flag;
		CString uid;
		CString dsfid;
		CString afi;
		CString block_data;
		CString security_status;
		CString vicc_mem_size;  
		CString ic_ref;        
		CString str_msg;
		int info_flag; 

		//get card uid for function
		//send request
		Amic_GetCardUIDRequest();
		Sleep(200);
		uid = Amic_GetUID();

		//clear all  data 
		Amic_ClearAll15693Data();
		info_flag = Amic_GetSysInfoRequest(false,uid);

		Sleep(300);
		uid = Amic_GetUID();
		dsfid = Amic_GetDSFIDData();
		afi = Amic_GetAFIData();
		vicc_mem_size = Amic_GetVICCMemSize();
		ic_ref = Amic_GetICRef();

		if(info_flag == 0)
		{
			str_msg = str_msg +CString("ID=")+uid;
			m_DataList.InsertString(0,str_msg);
			m_DataList.InsertString(1,CString("DSFID=")+dsfid);
			m_DataList.InsertString(2,CString("AFI=")+afi);
			m_DataList.InsertString(3,CString("MemSize =")+vicc_mem_size);
			m_DataList.InsertString(3,CString("ICrefence=")+ic_ref);
			PlaySound(L"windows\\ScanSuccess.wav", NULL, SND_SYNC | SND_FILENAME);
		}		
		//clear all 15693 data 
		Amic_ClearAll15693Data();

	}
		
}

void CReadPage::OnTagData() 
{
	if(fOpenDev == 0&&fContinueMode ==FALSE)
	{
		CString str_msg;
		CString err_code;
		CString resp_flag;
		CString strAllBlockData;
	
		Amic_DumpTagRequest(false,0,err_code);



		strAllBlockData = Amic_GetTagData112();
		str_msg = str_msg+CString("AllData=")+strAllBlockData;
	
		AfxMessageBox(str_msg);
		
		Amic_ClearAll15693Data();
	}
		
}

void CReadPage::OnReadBlock() 
{

 if(fOpenDev==0&&fContinueMode ==FALSE)
 {
	CString uid;
	CString response_flag;
	CString security_status;
	CString block_data;
	int nBlockList,nBlockList10,nReadRlockNum;
	int fReadBlk=0;
	//get which block to be read
	nBlockList = m_BlockList.GetCurSel();
	nBlockList10 = m_BlockList10.GetCurSel();
	nReadRlockNum = nBlockList10*10 + nBlockList-1;
	//get card uid for function

	//send request
	Amic_GetCardUIDRequest();
    Sleep(200);
	uid = Amic_GetUID();
	//clear all  data 
	Amic_ClearAll15693Data();


	if(nReadRlockNum>=0&&nReadRlockNum<28)
	{
		fReadBlk = Amic_ReadBlockRequest(nReadRlockNum,false,false,uid);


	
		if (fReadBlk ==0)
		{
			block_data = Amic_GetBlockData();
			if(fReadBlk == 0)
			m_DataList.InsertString(0,block_data);
			PlaySound(L"windows\\ScanSuccess.wav", NULL, SND_SYNC | SND_FILENAME);
		    goto Clear_Data;

		}else
		{	
		//error 
			AfxMessageBox(_T("error"));
			goto Clear_Data;
	
		}
	}
		else
	{
	 AfxMessageBox(L"Please Slect correct block num");
	}
	Clear_Data:
    //clear all data
	Amic_ClearAll15693Data();

		
 }		
}

UINT SusThreadFunc(LPVOID pvarg)
{
    HANDLE hMsgQ;             
    MSGQUEUEOPTIONS Options;	
    
    DWORD dwNumberOfBytesRead;
    DWORD dwErr;  
    DWORD flags;
    
    BYTE buf[QUEUE_SIZE];
    memset(&Options, 0, sizeof(Options));
    
    Options.dwSize = sizeof(MSGQUEUEOPTIONS);
    Options.dwFlags = 0;
    Options.dwMaxMessages = QUEUE_ENTRIES;
    Options.cbMaxMessage = sizeof(POWER_BROADCAST)+ MAX_NAMELEN;
    Options.bReadAccess = TRUE;
        
    hMsgQ = CreateMsgQueue(NULL,&Options);
    if (!hMsgQ)
    {
        RETAILMSG(1, (TEXT("pwrtest:CreateMsgQueue failed\r\n")));
        return 0;
    }

    //Register power notification events
    //if (!RequestPowerNotifications(hMsgQ, POWER_NOTIFY_ALL )) 
    if (!RequestPowerNotifications(hMsgQ, PBT_RESUME )) 
    {
        RETAILMSG(1, (TEXT("pwrtest:Register Power notification events failed\r\n")));
        return 0;
    }
    
           
    while(1) 
    {
        if (!ReadMsgQueue(hMsgQ,
                          &buf,
                          QUEUE_SIZE,
                          &dwNumberOfBytesRead,
                          INFINITE,
                          &flags))
        {
            ERRORMSG(1, (TEXT("pwrtest:ReadMsgQueue failed\r\n")));
            dwErr = GetLastError();
            switch (dwErr)
            {
                case ERROR_INSUFFICIENT_BUFFER:
                    ERRORMSG(1, (TEXT("pwrtest:ERROR_INSUFFICIENT_BUFFER\r\n")));
                    break;
                case ERROR_PIPE_NOT_CONNECTED:
                    ERRORMSG(1, (TEXT("pwrtest:ERROR_PIPE_NOT_CONNECTED\r\n")));
                    break;
                case ERROR_TIMEOUT:
                    ERRORMSG(1, (TEXT("pwrtest:EEOR_TIMEOUT\r\n")));
                    break;
                default:
                    ERRORMSG(1, (TEXT("pwrtest:ERROR_UNKNOWN\r\n")));
           }
        }
        else if(dwNumberOfBytesRead >= sizeof(POWER_BROADCAST)) 
        { 
            PPOWER_BROADCAST pB = (PPOWER_BROADCAST)&buf; 
            if (pB->Message == PBT_RESUME)
            {                            
                RETAILMSG(1, (TEXT("\n\rRFID->PBT_RESUME\n\r")));
			  if(fOpenDev == 0)
			  {
             	Amic_ContinuousModeOFF();
				fContinueMode = FALSE;
				//release the Handle for control
				Amic_DeviceClose();
				//for Key function enable
				//	UnregisterKeyNotification(Ctl_ScanKey_Notify);
				Sleep(500);
				//All restart after resume
				//DualA_Power_On();

				HMODULE hSYSCTL = LoadLibrary(_T("SYSCTL.dll"));
	
				if( hSYSCTL )
				{
					PFN_SET_RFID_POWER SET_RFID_POWER=(PFN_SET_RFID_POWER)GetProcAddress(hSYSCTL, _T("SetRFIDPower"));
					SET_RFID_POWER(TRUE);
					FreeLibrary(hSYSCTL);
				}
				
					

				Sleep(50);
				Amic_DeviceOpen("COM6:");
				fOpenDev = 0;
				if(fContinueSuspend ==FALSE)
				{
              		Amic_ContinuousModeOFF();//off ContiuneMode
					fContinueMode = FALSE;
				}
				else if(fContinueSuspend ==TRUE)
				{
					Sleep(3000);
					Amic_ContinuousModeON();//on ContiuneMode
					HWND* hwnd = (HWND*)pvarg;
					hSuspendThread=AfxBeginThread(ContinueModeFunc,hwnd,THREAD_PRIORITY_NORMAL);
			
				}
			  }
            }                                                                                                                
         
          
                   
        }
        
        
    }
return true;
}

UINT ContinueModeFunc(LPVOID pParam) //for Continue Mode Scan
{
	CReadPage* ContinueModeDlg = (CReadPage*)pParam;
	CString uid;
	CString strMouse ; //@,0x40
	strMouse ="@"; //@,0x40 

 	fContinueMode =TRUE;
	while(fContinueMode)
	{
		uid = Amic_GetContinuousModeData();
//		Sleep(150);
		if (uid.GetLength() !=0 )
		{

		if (uid.Left(1).Compare(strMouse) ==0)
		{

			uid = uid.Mid(3,16);
			ContinueModeDlg->m_DataList.InsertString(0,uid);
		}
//		Amic_ClearAll15693Data();
		}
	}

Sleep(20);
	
	return TRUE;   
}

void CReadPage::OnStart() 
{
	//Sleep(600);
	 flagConti = Amic_ContinuousModeON();
    fContinueSuspend = TRUE;
	if(flagConti==0)
	{
		 fContinueMode =TRUE;
//		Sleep(300);
		AutoScanWinThread = AfxBeginThread(ContinueModeFunc,this,THREAD_PRIORITY_NORMAL,0,0,NULL);	
	}	
}

void CReadPage::OnStop() 
{
	fContinueMode =FALSE;
	fContinueSuspend = FALSE;
	Amic_ContinuousModeOFF();	
}

void CReadPage::OnClear() 
{
	m_DataList.ResetContent();
	Amic_ClearAll15693Data();	
}

void CReadPage::OnSave() 
{
	  CFile myFile;
	  CFileDialog SaveFileDlg(FALSE,NULL,NULL,OFN_OVERWRITEPROMPT,L"Text Files (*.txt)|*.txt|");
      int iRet = SaveFileDlg.DoModal();
      CString l_strFileName;
      l_strFileName = SaveFileDlg.GetPathName();

	
	  if(iRet == IDOK)
	  {
			if( !myFile.Open( l_strFileName, CFile::modeCreate
				| CFile::modeReadWrite) )
			 AfxMessageBox(TEXT("File Open Error"));

			CString strBuf;
			CHAR chrBuf[MAX_PATH];
			memset(chrBuf,0,MAX_PATH);

			for(int i = 0;i<m_DataList.GetCount();i++)
			{
				m_DataList.GetText( i, strBuf.GetBuffer(m_DataList.GetTextLen( i )) );
				strBuf.ReleaseBuffer();

				sprintf(chrBuf,"%S",strBuf+"\n");
				myFile.Write(chrBuf,strlen(chrBuf));		
			}
			m_DataList.ResetContent();
	  }		
}

void CReadPage::OnRead14443() 
{
/*	CString UID = "";
	int n14443 = Amic_Get14443();
	Sleep(400);
	UID=Amic_Get14443Val();
	
	int Len = UID.GetLength();
	if(Len!=0)
	{
		m_DataList.InsertString(0,UID);
		PlaySound(L"windows\\ScanSuccess.wav", NULL, SND_ASYNC | SND_FILENAME);	
	}
	Amic_ClearAll15693Data();
	UID = "";
*/
	CString UID = "";
	int n14443 = Amic_Get14443();
	Sleep(400);
	UID=Amic_Get14443Val();
	
	int Len = UID.GetLength();
	if(Len!=0)
	{
		m_DataList.InsertString(0,UID);
		PlaySound(L"windows\\ScanSuccess.wav", NULL, SND_ASYNC | SND_FILENAME);	
	}
	Amic_ClearAll15693Data();
	UID = "";
}

void CReadPage::OnBtnReset() 
{
	CString fakeUID = "1111111111111111";
		if(Amic_ReaderResetRequest()==0)
		 AfxMessageBox(L"Reader Reset");
		else
		AfxMessageBox(L"Reset fail");

			
}

void CReadPage::OnReset()
{
	if( fOpenDev == 0 )
	{
		CReadPage::OnBtnClose();
	}
}

void CReadPage::OnOK()
{
	if( fOpenDev == 0 )
	{
		CReadPage::OnBtnClose();
	}
}

⌨️ 快捷键说明

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