?? export.cpp
字號:
// 讀卡器
#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 + -