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

📄 rfid_14443_demo.cpp

📁 基于TI TRF7960的RFID读卡器主机端源代码
💻 CPP
字号:
/** @file
* RFID_14443_Demo.cpp : Defines the entry point for the application.
*
* INTEL CONFIDENTIAL
* Copyright 2006 Intel Corporation All Rights Reserved. 
* The source code contained or described herein and all documents related
* to the source code ("Material") are owned by Intel Corporation or its
* suppliers or licensors. Title to the Material remains with Intel Corporation
* or its suppliers and licensors. The Material contains trade secrets and
* proprietary and confidential information of Intel or its suppliers and licensors.
* The Material is protected by worldwide copyright and trade secret laws and
* treaty provisions. No part of the Material may be used, copied, reproduced,
* modified, published, uploaded, posted, transmitted, distributed, or disclosed
* in any way without Intel抯 prior express written permission.
*
* No license under any patent, copyright, trade secret or other intellectual
* property right is granted to or conferred upon you by disclosure or delivery
* of the Materials, either expressly, by implication, inducement, estoppel or
* otherwise. Any license under such intellectual property rights must be express
* and approved by Intel in writing.
*
*/

#include "stdafx.h"
#include "RFID_14443_Demo.h"
#include "RFIDHandler.h"

#define MAX_LOADSTRING 100
#define COMM_PORT_SIZE 10

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

TCHAR szErrComPort[MAX_LOADSTRING];			
TCHAR szErrTitle[MAX_LOADSTRING];
TCHAR szErrComPortAccess[MAX_LOADSTRING];

TCHAR szUidLabel[MAX_LOADSTRING];
TCHAR szUidVal[MAX_LOADSTRING];
TCHAR szPupiLabel[MAX_LOADSTRING];
TCHAR szPupiVal[MAX_LOADSTRING];


RFIDHandler g_RFIDHandler;
WCHAR		ComPort[COMM_PORT_SIZE];
const int count = 10;

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK DemoDialogMsgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
INT_PTR CALLBACK ComPortDialogMsgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_RFID_14443_DEMO, szWindowClass, MAX_LOADSTRING);

	LoadString(hInstance, IDS_ERR_COM_PORT, szErrComPort, MAX_LOADSTRING);
	LoadString(hInstance, IDS_ERR_TITLE, szErrTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDS_ERR_ACCESS_COM_PORT, szErrComPortAccess, MAX_LOADSTRING);

	LoadString(hInstance, IDC_UID_LABEL, szUidLabel, MAX_LOADSTRING);
	LoadString(hInstance, ID_GETUID_VAL, szUidVal, MAX_LOADSTRING);
	LoadString(hInstance, IDC_PUPI_LABEL, szPupiLabel, MAX_LOADSTRING);
	LoadString(hInstance, ID_GETPUPIS_VAL, szPupiVal, MAX_LOADSTRING);

	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RFID_14443_DEMO));

	if (wcslen(lpCmdLine) != 0)
	{
		wcscpy(ComPort, lpCmdLine);
		g_RFIDHandler.SetCommPort(lpCmdLine);
	}
	else
	{
		DialogBox(hInst,MAKEINTRESOURCE(IDD_COMPORT),0,(DLGPROC)ComPortDialogMsgHandler);
		if(wcslen(ComPort) == 0)
		{
			MessageBox(NULL, szErrComPort, szErrTitle, MB_OK | MB_ICONASTERISK);
		}
		else
		{
			g_RFIDHandler.SetCommPort(ComPort);
		}
	}

	DialogBox(hInst,MAKEINTRESOURCE(IDD_DEMODIALOG),0,(DLGPROC)DemoDialogMsgHandler);
	// Main message loop:
	while (GetMessage(&msg, NULL, 0, 0))
	{
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RFID_14443_DEMO));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= MAKEINTRESOURCE(IDC_RFID_14443_DEMO);
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   
   if (!hWnd)
   {
      return FALSE;
   }

   //ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

void UpdateUidList(vector<string> UidList, HWND ComboBox)
{
	while( SendMessage(ComboBox,
			CB_DELETESTRING,
			0,
			0) > 0)
			;
	SendMessage(ComboBox,
			CB_SETCURSEL,
			0,
			0);
		
	vector<string>::const_iterator it = UidList.begin();
	
	while(it != UidList.end())
	{
		WCHAR buf[300];
		memset(buf,'\0',sizeof(WCHAR) * 300);
		MultiByteToWideChar(
			CP_THREAD_ACP,				// code page
			MB_PRECOMPOSED,				// character-type options
			(string (*it)).c_str(),		// string to map
			(string (*it)).length(),	// number of bytes in string
			buf,	// wide-character buffer
			300		// size of buffer
			);

			
		SendMessage(ComboBox,
			CB_ADDSTRING,
			0,
			reinterpret_cast<LPARAM>((LPWSTR)buf));

		if (it == UidList.begin())
		{
			SendMessage(ComboBox,
			CB_SETCURSEL,
			0,
			0);
		}
		it++;
	}
}

INT_PTR CALLBACK DemoDialogMsgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	RF_Status Status = RF_DeviceUnResponsive;
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		{
			SetWindowText(GetDlgItem(hDlg, IDC_UIDs_LABEL), szUidLabel);
			SetWindowText(GetDlgItem(hDlg, IDGETUIDS), szUidVal);
			SendMessage( GetDlgItem(hDlg, IDC_14443A), BM_SETCHECK, wParam, 0);
			if ( true == g_RFIDHandler.PowerUpRFIDReader() )
			{
				Status = g_RFIDHandler.DeviceReady();
				if (RF_Success == Status)
				{
					Status = g_RFIDHandler.SetProtocol(RF_ISO14443A);
					if (RF_Success == Status)
					{
						vector<string> Uids;
						Status = g_RFIDHandler.GetUids(Uids);
						UpdateUidList(Uids, GetDlgItem(hDlg, IDC_UIDLIST));
					}
				}
				g_RFIDHandler.ClosePort();
			}
			else
			{
				wstring err = wstring(szErrComPortAccess) + ComPort;
				MessageBox (NULL, err.c_str(), szErrTitle, MB_OK);
				PostQuitMessage(0);
			}
		}
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
			HWND comboBox;
			case IDGETUIDS:
				{
					PROTOCOL SelectedProtocol;

					if (BST_CHECKED == IsDlgButtonChecked(hDlg, IDC_14443A))
					{
						SelectedProtocol = RF_ISO14443A;
					}
					else
					{
						SelectedProtocol = RF_ISO14443B;
					}

					if ( true == g_RFIDHandler.PowerUpRFIDReader() )
					{
						Status = g_RFIDHandler.DeviceReady();
						if (RF_Success == Status)
						{

							Status = g_RFIDHandler.SetProtocol(SelectedProtocol);
							if (RF_Success == Status)
							{
								vector<string> Uids;
								int i = 0;
								Status = g_RFIDHandler.GetUids(Uids);
								while ((RF_UidNotFound == Status) && (i < count))
								{
									Sleep(100);
									Status = g_RFIDHandler.GetUids(Uids);
									i++;
								}
								UpdateUidList(Uids, GetDlgItem(hDlg, IDC_UIDLIST));
							}
						}
						g_RFIDHandler.ClosePort();
					}
					else
					{
						wstring err = wstring(szErrComPortAccess) + ComPort;
						MessageBox (NULL, err.c_str(), szErrTitle, MB_OK);
					}
				}
				break;
			case IDC_14443A:
					SetWindowText(GetDlgItem(hDlg, IDC_UIDs_LABEL), szUidLabel);
					SetWindowText(GetDlgItem(hDlg, IDGETUIDS), szUidVal);
					comboBox = GetDlgItem(hDlg, IDC_UIDLIST);
					while( SendMessage(comboBox,
						CB_DELETESTRING,
						0,
						0) > 0)
						;
					SendMessage(comboBox,
						CB_SETCURSEL,
						0,
						0);
				break;
			case IDC_14443B:
					SetWindowText(GetDlgItem(hDlg, IDC_UIDs_LABEL), szPupiLabel);
					SetWindowText(GetDlgItem(hDlg, IDGETUIDS), szPupiVal);
					comboBox = GetDlgItem(hDlg, IDC_UIDLIST);
					while( SendMessage(comboBox,
						CB_DELETESTRING,
						0,
						0) > 0)
						;
					SendMessage(comboBox,
						CB_SETCURSEL,
						0,
						0);
				break;
			case IDEXIT:
					PostQuitMessage(0);
				break;
		}
		break;
	}
	return (INT_PTR)FALSE;
}


INT_PTR CALLBACK ComPortDialogMsgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		switch(LOWORD(wParam))
		{
		case IDOK:
			GetWindowText(GetDlgItem(hDlg, IDC_COMPORT),ComPort,COMM_PORT_SIZE);
		case IDCANCEL:
				EndDialog(hDlg, LOWORD(wParam));
				return (INT_PTR)TRUE;
			break;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

⌨️ 快捷键说明

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