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

📄 export.cpp

📁 对于银行卡卡的读取控制
💻 CPP
📖 第 1 页 / 共 2 页
字号:

// 读卡器
#include "stdafx.h"
#include "cardreader.h"
#include "export.h"
#include "kde_dll.h"
#include <afxmt.h>
#include <mbstring.h>


CString  g_ICRStatusMsg;//保存了事件状态的信息
UINT g_ICRStatusID = 0;//保存了事件状态的值,用于给用户返回状态
char  g_res[1024] = {{0}};//保存了结果
long g_len = 0;//保存了结果占用的空间

/*
BOOL g_bFlagEventProc;//在响应了回调函数后置标志

void CALLBACK ICR_EventProc (DWORD dwEvent, DWORD dwResult, DWORD dwAPIError, LPVOID lpvArgList)
{
	if (dwEvent != ICR_EVENT_ERROR)
	{
		if (ICR_GetResponse (g_res, 1024, &g_len) == ICR_SUCCESS)
		{
			if (dwEvent == ICR_EVENT_RCV_EOT)
			{
				g_ICRStatusMsg = "Got EOT";
				g_res[0] = 'E';
				g_res[1] = 'O';
				g_res[2] = 'T';
			}
			else
				g_ICRStatusMsg = "Got Response";
		}
		else
		{
			g_ICRStatusMsg = "ERROR: Can't Display Response";
		}
		g_ICRStatusID = dwEvent;
	}
	else
	{
		g_ICRStatusID = dwResult;
		switch (dwResult)
		{
		case ICR_RESULT_ERR_NOT_CONNECT:
			g_ICRStatusMsg = "ERROR: Not Connect to C/R";
			break;
		case ICR_RESULT_ERR_DISCONNECT:
			g_ICRStatusMsg = "ERROR: Disconnect to C/R";
			break;
		case ICR_RESULT_ERR_SND_NOT_CMD:
			g_ICRStatusMsg = "ERROR: Send Command is failed";
			break;
		case ICR_RESULT_ERR_SND_NOT_ENQ:
			g_ICRStatusMsg = "ERROR: Send ENQ is failed";
			break;
		case ICR_RESULT_ERR_SND_NOT_EOT:
			g_ICRStatusMsg = "ERROR: Send EOT is failed";
			break;
		case ICR_RESULT_ERR_RCV_NOT_ACK:
			g_ICRStatusMsg = "ERROR: Can't Receive ACK";
			break;
		case ICR_RESULT_ERR_RCV_NOT_RES:
			g_ICRStatusMsg = "ERROR: Can't Receive Response";
			break;
		case ICR_RESULT_ERR_RCV_NOT_EOT:
			g_ICRStatusMsg = "ERROR: Can't Receive EOR";
			break;
		case ICR_RESULT_ERR_RCV_NAK:
			g_ICRStatusMsg = "ERROR: Received NAK";
			break;
		case ICR_RESULT_ERR_RCV_BCC: 
			g_ICRStatusMsg = "ERROR: Bad Response(BCC ERR)";
			break;
		case ICR_RESULT_ERR_RCV_DLE:
			g_ICRStatusMsg = "ERROR: Bad Response(DLE ERR)";
			break;
		case ICR_RESULT_ERR_RCV_PARITY:
			g_ICRStatusMsg = "ERROR: Bad Response(Parity ERR)";
			break;
		case ICR_RESULT_ERR_RCV_FRAME:
			g_ICRStatusMsg = "ERROR: Bad Response(Frame ERR)";
			break;
		case ICR_RESULT_ERR_RCV_DATAFULL:
			g_ICRStatusMsg = "ERROR: Bad Response(Data Overflow)";
			break;
		case ICR_RESULT_ERR_RCV_BUFFERFULL:
			g_ICRStatusMsg = "ERROR: Bad Response(Buffer flow)";
			break;
		case ICR_RESULT_ERR_RCV_TIMEOUT:
			g_ICRStatusMsg = "ERROR: Receive Time out";
			break;
		case ICR_RESULT_ERR_RCV_IOERROR:
			g_ICRStatusMsg = "ERROR: I/O Error";
			break;
		case ICR_RESULT_ERR_API:
			g_ICRStatusMsg = "ERROR: Win API Error";
			break;
		default:
			g_ICRStatusMsg = "ERROR: Unknown ERROR; Report to Maintainer";
		}
	}
	g_bFlagEventProc = TRUE;
//	MessageBox(NULL, (LPCSTR)g_res, NULL, MB_OK);
}
*/



/*
* return :
 0 : 命令执行成功
 -2 :Error
 -3:timeout
*/
int mySendCommand(int type, char * cmd, int length)
{
	int ret = 0;
	ret = SendCommand(type, cmd, length, g_res, g_len);
	return ret ;
}

//initial c/r 
//初始化读卡器
extern "C" _declspec( dllexport) LRESULT  crInitialize()
{

	LRESULT result = 0;
	if ( COM_Open(COM1, CBR_9600,8,ONESTOPBIT,NOPARITY) == FALSE ) {// open failed
		result = CRERROR_INIT;//设备初始化错
		return result ;
	}
	
//	char * cmd;
	char cmd[8] = {{0}};
	cmd[0] = 0x30;
	cmd[1] = 0x30;
	cmd[2] = 0x30;
	cmd[3] = 0x30;
	cmd[4] = 0x30;
	cmd[5] = 0x30;
	cmd[6] = 0x30;
/*	cmd[7] = 0x30;
	cmd[8] = 0x30;
	cmd[9] = 0x30;
	cmd[10] = 0x30;
	cmd[11] = 0x30;
	cmd[12] = 0x30;
	cmd[13] = 0x30;
*/
//	cmd = "C000000";
	int a = 0;
	int b = 0 ;
	int errCode = 0;
	b = KDE_Initialize(INIT_EJECT, cmd, errCode);
	//SendCommand(INIT_EJECT, &cmd[0], 14, &g_res[0], g_len);

//	mySendCommand(cmd, 3, 20000);


	/*	测试问题所在
		int  cm = 0 ;
		CString t,tmp;
		t.Format("g_res = %s\n",g_res);
		tmp.Format("cm = %d",cm);
		t += tmp;
		MessageBox(NULL,t,"命令执行情况",MB_OK);
	*/
		/******************对返回信息进行判断--开始*******************************/
/*		if (g_res[0] == 'P' && g_res[1] == '0' && g_res[2] == '0' && g_res[3] == '0' && g_res[4] =='1') {
	#if _DEBUG
			MessageBox(NULL, "card init ok", NULL, MB_OK);
	#endif
			result = 0;//只要初始化成功,就返回0

		}
		else if (g_res[0] == 'P' && g_res[1] == '0' && g_res[2] == '0' && g_res[3] == '0' && g_res[4] =='0') {
	#if _DEBUG
			MessageBox(NULL, "no card init ok", NULL, MB_OK);
	#endif
			result = 0;// 无卡OK,只要初始化成功,就返回0
		}
		else {
	#if _DEBUG
			MessageBox(NULL, "init fail", NULL, MB_OK);
	#endif
			result = CRERROR_INIT;//设备初始化错
		}*/
		/******************对返回信息进行判断--结束*******************************/
		memset(g_res, 0, 1024);
		return result;//Result;
}

/*-------------------*
INPUT: 
  uControlCode:
	CRC_ENTRY		*
	CRC_REMOVE		*
	CRC_SWALLOW		*
	CRC_ENABLE		*
	CRC_DISABLE-----*/

extern "C" _declspec( dllexport) LRESULT  crControl(IN USHORT uControlCode)
{
	LRESULT   dwReturn ;
	switch (uControlCode)
	{
		case CRC_ENTRY:// 进卡
		{ 
			dwReturn = InTakeCard();
			break;
		}
		case CRC_REMOVE://出卡
		{ 
			dwReturn = TakeOutCard();
			break;
		}
		case CRC_SWALLOW:// 吞卡
		{ 
			dwReturn = SwallowCard();
			break;
		}

		case CRC_ENABLE:// 允许进卡
		{ 
			dwReturn = PermitIn();
			break;
		}
		case CRC_DISABLE:// 禁止进卡
		{ 
			dwReturn = UnPermitIn();
			break;
		}
		default : 
			break;
	}

	return dwReturn ;
}

// 检测卡类型,硬件不能提供此功能
extern "C" _declspec( dllexport) LRESULT  crCheckCard(IN ULONG ulCardType)
{
//	MessageBox(NULL,"不能提供此功能","信息",MB_OK);
	return CRERROR_UNSUPPORT ;
}




extern "C" _declspec( dllexport) LRESULT  crGetStatus()
{
	char cmd[8] = {{0}};
	cmd[0] = '0';
	cmd[1] = '0';
	cmd[2] = '0';
	cmd[3] = '0';
	cmd[4] = '0';
	cmd[5] = '0';
	cmd[6] = '0';

	int  cm = 0 ;

//	int ret = 0;
//	ret = KDE_StatusCheck(STAT_REPORT, g_res, cm, 3000);

	cm = mySendCommand(STAT_REPORT, cmd, 7);

	LRESULT result = 0;

/*	测试问题所在
	CString t,tmp;
	t.Format("g_res = %s\n",g_res);
	tmp.Format("cm = %d",cm);
	t += tmp;
	MessageBox(NULL,t,"命令执行情况",MB_OK);
*/
	/******************对返回信息进行判断--开始*******************************/
	if (strncmp((LPCSTR)g_res, "P1000", 5) == 0) {
#ifdef _DEBUG
//		MessageBox(NULL, "无卡!", "信息", MB_OK);
#endif
		result = CRERROR_EMPTY;
	}
	else if (strncmp((LPCSTR)g_res, "P1002", 5) == 0) {
#ifdef _DEBUG
		MessageBox(NULL, "有卡在里面!", "信息", MB_OK);
#endif
		result = CRERROR_INSIDE;
	}
	else if (strncmp((LPCSTR)g_res, "P1001", 5) == 0) {
#ifdef _DEBUG
//		MessageBox(NULL, "卡在门边!", "信息", MB_OK);
#endif
		result = CRERROR_GATE;
	}
	else {
#ifdef _DEBUG
		MessageBox(NULL, "查询状态不成功!", "信息", MB_OK);
#endif
		result = CRERROR_STATUS;
	}
	/******************对返回信息进行判断--结束*******************************/
	memset(g_res, 0, 1024);
	return result;//Result;
}


extern "C" _declspec( dllexport) LRESULT   crUnInit()// no CMD for available use
{
	LRESULT result = 0;

	result = crGetStatus();
	switch (result ) {

	case CRERROR_EMPTY:
		COM_Close();
		break;

	case CRERROR_INSIDE:
		if ( TakeOutCard() == 0) { // 出卡成功
			COM_Close();
		}
		break;

	case CRERROR_GATE:
		COM_Close();
		break;

	case CRERROR_STATUS:
		//goto STATUS;
		break;
	}

	return 0;
}

extern "C" _declspec( dllexport) LRESULT    crWrite(IN ULONG ulSegIndex,IN ULONG ulOffset , IN ULONG size, IN PUCHAR pDataBuffer,OUT PULONG writeSize)
{
	/*switch (ulSegIndex)
	{
	case	CRTRACK1:// 写第一磁道
		WriteTrack1(ulSegIndex,ulOffset,size,pDataBuffer,writeSize);
		break;

	case	CRTRACK2:// 写第2磁道
		WriteTrack2(ulSegIndex,ulOffset,size,pDataBuffer,writeSize);
		break;

	case	CRTRACK3:// 写第3磁道
		WriteTrack3(ulSegIndex,ulOffset,size,pDataBuffer,writeSize);
		break;

	default:
		break;

	}*/
//	MessageBox(NULL,"目前硬件不支持此功能!","信息",MB_OK);
	return CRERROR_UNSUPPORT ;
}

// 读多磁道还是单磁道的卡??????????????????????????????????
extern "C" _declspec( dllexport) LRESULT 	crRead(IN ULONG ulSegIndex , IN ULONG ulOffset,IN ULONG ulSize,OUT PUCHAR pDataBuffer,OUT  PULONG ulReadSize)
{
	LRESULT  result = 0;
	int TempReadSize;
	TempReadSize=0;
	
//	CString t,m;
//	t.Format("%d",(int)ulSegIndex);
//	MessageBox(NULL,t,NULL,MB_OK);

	switch (ulSegIndex)
	{
	case	CRTRACK1:// 读第一磁道
			result = ReadTrack1(CRTRACK1,0,ulSize,pDataBuffer,TempReadSize);
			break;
		

	case	CRTRACK2:// 读第2磁道
		
			result = ReadTrack2(CRTRACK2,0,ulSize,pDataBuffer,TempReadSize);
//			m.Format("%s",pDataBuffer);
//			AfxMessageBox(m,NULL,MB_OK);
			break;
		

	case	CRTRACK3:// 读第3磁道
		
			result = ReadTrack3(CRTRACK3,0,ulSize,pDataBuffer,TempReadSize);	
	//		m.Format("%s",pDataBuffer);
	//		AfxMessageBox(m,NULL,MB_OK);

			break;
		
	}
   *ulReadSize=TempReadSize;
	return result;
}

extern "C" __declspec(dllexport) LRESULT   GetDeviceDesc(OUT PDEVICEDESC pDeviceDesc)
{
	char a[32]={0};// "";
	char b[32] = {0};//"";
	char c[32]= {0};//"ORMON V2BF";
	char d[4] = "";

	strcpy(a,"CardReader");
	strcpy(b,"KDE Reader");
	strcpy(c,"KDE");
	pDeviceDesc->cbSize = sizeof(DEVICEDESC);
	pDeviceDesc->usVendorID = 0;
	pDeviceDesc->usProductID = 0;
	pDeviceDesc->szDeviceID = 1002;
	memcpy(pDeviceDesc->szDevice,a,32);
	memcpy(pDeviceDesc->szDeivceName,b,32);
	memcpy (pDeviceDesc->szManufacturer,c,32);
	pDeviceDesc->cbExtSize=0;
	memcpy(pDeviceDesc->pExtInfo,d,strlen(d));

	return 0;

}



// 进卡
LRESULT  InTakeCard()
{
	char cmd[8] = {{0}};
	cmd[0] = '0';
	cmd[1] = '0';
	cmd[2] = '0';
	cmd[3] = '0';
	cmd[4] = '0';
	cmd[5] = '0';
	cmd[6] = '0';

	int  cm = 0 ;

	LRESULT  value = 0;
	cm = mySendCommand(ENTRY_MS, cmd, 7);
	/******************对返回信息进行判断--开始*******************************/
	if ( cm == 1 ) {// success

		if (g_res[0] == 'P' && g_res[1] == '2' && g_res[2] == '0' && g_res[3] == '0' && g_res[4] == '0') {
			while (1) {
				if ( crGetStatus() == CRERROR_INSIDE) {
		#if _DEBUG
			MessageBox(NULL, "进卡成功!", "信息", MB_OK);
		#endif
					value = 0;
					break;
				} 
				Sleep(200);
			}
		}
		else if (g_res[0] == 'P' && g_res[1] == '2' && g_res[2] == '0' && g_res[3] == '0' && g_res[4] == '2') {
			while (1) {
				if ( crGetStatus() == CRERROR_INSIDE) {
		#if _DEBUG
			MessageBox(NULL, "重进卡成功!", "信息", MB_OK);
		#endif
					value = 0;
					break;
				} 
				Sleep(200);
			}
		}
	}
	else if (cm == -2) {
	#if _DEBUG
		MessageBox(NULL, "进卡不成功!", "信息", MB_OK);
		value = CRERROR_CARDINPUT;
	#endif
	}
	else if ( cm == -3 ) {//命令执行不成功,超时
	#if _DEBUG
		MessageBox(NULL, "进卡超时!", "信息", MB_OK);
	#endif
		value = CRERROR_ENTRY_CARRY;
	}
	else {
	#if _DEBUG
			MessageBox(NULL, "进卡不成功!", "信息", MB_OK);
	#endif
			value = CRERROR_CARDINPUT;
	}
	/******************对返回信息进行判断--结束*******************************/
	memset(g_res, 0, 1024);
	return value;//Result;
}

// 出卡
LRESULT  TakeOutCard()
{
	char cmd[8] = {{0}};
	cmd[0] = '0';
	cmd[1] = '0';
	cmd[2] = '0';
	cmd[3] = '0';
	cmd[4] = '0';

⌨️ 快捷键说明

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