📄 kdmcard.cpp
字号:
/*********************************************************************
* Designed by xiaohuiyang 2002.03.15 E-mail: yxh2001@21cn.com *
*********************************************************************/
// KDM4787.cpp: implementation of the CKDMCard class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "KDMCard.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
int _Debug_KDE=1;
void TraceDebug(char *pchData, int len)
{
FILE *fp;
CTime tCurr;
char chCurr[20];
if(_Debug_KDE == 0) return;
tCurr = CTime::GetCurrentTime();
wsprintf((char *) chCurr, "%02d-%02d %02d:%02d:%2d",\
tCurr.GetMonth(), tCurr.GetDay(), tCurr.GetHour(),tCurr.GetMinute(),tCurr.GetSecond());
char FileName[30];
memset(FileName, '\0', 30);
sprintf(FileName, "debug%02d%02d.log", tCurr.GetMonth(), tCurr.GetDay());
fp=fopen(FileName,"a+");
fprintf(fp,"---%s---\n", chCurr);
fprintf(fp,"Data=[%s] Len=[%d]\n",pchData,len);
fclose(fp);
}
CKDMCard::CKDMCard()
{
gTimeOut = 1;
mSerialPort = 0;
mExtPort = 0;
}
CKDMCard::~CKDMCard()
{
gCom.AsynClose();
}
//----初始化请求响应
void CKDMCard::ENQ(char *pchSend)
{
sprintf(pchSend, "%c%c%c",0x05, 0x0D ,0x00);
}
//--正确响应
void CKDMCard::ACK(char *pchSend)
{
sprintf(pchSend, "%c%c%c",0x06, 0x0D ,0x00);
}
//--错误响应
void CKDMCard::NAK(char *pchSend)
{
sprintf(pchSend, "%c%c%c",0x15, 0x0D ,0x00);
}
short CKDMCard::KDM4787_BCC(char *buffer, int count)
{
int i, bcccalc;
bcccalc = 0;
for(i = 1; i < count; i++)
{
bcccalc ^= buffer[i];
}
return bcccalc;
}
short CKDMCard::KDM_Initial()//初始化命令
{
char chSend[128] ,chSend1[128],chState[20];
int i=0, bcc, j;
memset(chSend, '\0', 128);
memset(chSend1,'\0', 128);
memset(chState, '\0', 20);
//'初始化命令' 'C' 30H pm 33H 32H 34H 30H Fm Pd Wv Sh Ds Ty Cp
int nSendTimes=0;
int nInitTimes=0;
INITAGAIN:
sprintf(chSend,"%cC0032403000010%c",0x02, 0x03);
bcc = KDM4787_BCC(chSend, strlen(chSend));
sprintf(chSend1, "%s%c%c", chSend, bcc, 0x0D);
gCom.AsynSendData(chSend1,strlen(chSend1));
if(!gCom.sReadByEndChar(0x0d, gTimeOut))
{
return _ERR_KDE_INIT1;
}
TraceDebug(gCom.chRcvbuf, strlen(gCom.chRcvbuf));
if(gCom.chRcvbuf[0]!=0x06)
{
nSendTimes++;
Sleep((DWORD)200); //加延时
ResetKDM();
if(nSendTimes>=2)
{
return _ERR_KDE_INIT1;
}
TraceDebug("KDE Initial again!", 0);
goto INITAGAIN;
}
memset(chSend, '\0', 128);
ENQ(chSend);
gCom.AsynSendData(chSend,strlen(chSend));
if(!gCom.sReadByEndChar(0x0d, gTimeOut))
{
return _ERR_KDE_INIT2;
}
j = 0;
for (i=0;i<gCom.nRcvlen;i++)
{
if(gCom.chRcvbuf[i]!=0x02)
{
if(gCom.chRcvbuf[i]==0x03)
break;
chState[j] = gCom.chRcvbuf[i];
j++;
}
}
if(chState[0] != 'P')
{
nInitTimes++;
ResetKDM();
if(nInitTimes>=2)
{
return _ERR_KDE_STATUS;
}
TraceDebug("Receive Error KDE Initial again!", 0);
goto INITAGAIN;
}
if((chState[4] != '1')&&(chState[4] != '2'))
return _KDE_NOCARD;
else if(chState[4]=='2')
return _KDE_CARD_INSIDE;
return _KDE_CARD_ATGATE;
}
short CKDMCard::KDMStatusRequest()
{
//磁卡用异步通讯
char chSend[256], chSend1[256],chState[20];
int bcc, i, j;
memset(chSend, '\0', 256);
memset(chSend1, '\0', 256);
//'状态请求命令' 'C' 31H pm (0x30'pm 没有Se 0x31 'pm 存在Se)
sprintf(chSend, "%cC11%c", 0x02, 0x03);
bcc = KDM4787_BCC(chSend, strlen(chSend));
sprintf(chSend1, "%s%c%c", chSend, bcc, 0x0D);
gCom.AsynSendData(chSend1, strlen(chSend1));
if(gCom.sReadByEndChar(0x0d, gTimeOut) < 0)
{
return _ERR_KDE_STATUS1;
}
if(gCom.chRcvbuf[0]!=0x06)
return _ERR_KDE_STATUS1;
memset(chSend, '\0', 256);
ENQ(chSend);
gCom.AsynSendData(chSend,strlen(chSend));
if(!gCom.sReadByEndChar(0x0d, gTimeOut))
{
return _ERR_KDE_STATUS2;
}
j = 0;
for (i=0;i<gCom.nRcvlen;i++)
{
if(gCom.chRcvbuf[i]!=0x02)
{
if(gCom.chRcvbuf[i]==0x03)
break;
chState[j] = gCom.chRcvbuf[i];
j++;
}
}
if(chState[0] != 'P')
return _ERR_KDE_STATUS;
if((chState[4] != '1')&&(chState[4] != '2'))
return _KDE_NOCARD;
else if(chState[4]=='2')
return _KDE_CARD_INSIDE;
return _KDE_CARD_ATGATE;
}
//command should be 1 or 2
short CKDMCard::KDMEnable(short command)
{
//允许/禁止插卡
char chSend[256], chSend1[256],chState[20];
int bcc, i, j;
memset(chSend, '\0', 256);
memset(chSend1, '\0', 256);
if (command != 1 && command != 2)
{
return -100;
}
//'状态请求命令' 'C' 3AH pm (0x30'pm accept non-encoded card 0x31 'pm disable card entry 0x32 'pm enable card entry)
sprintf(chSend, "%cC:%d%c", 0x02, command, 0x03);
bcc = KDM4787_BCC(chSend, strlen(chSend));
sprintf(chSend1, "%s%c%c", chSend, bcc, 0x0D);
gCom.AsynSendData(chSend1, strlen(chSend1));
if(gCom.sReadByEndChar(0x0d, gTimeOut) < 0)
{
return _ERR_KDE_STATUS1;
}
if(gCom.chRcvbuf[0]!=0x06)
return _ERR_KDE_STATUS1;
memset(chSend, '\0', 256);
ENQ(chSend);
gCom.AsynSendData(chSend,strlen(chSend));
if(!gCom.sReadByEndChar(0x0d, gTimeOut))
{
return _ERR_KDE_STATUS2;
}
j = 0;
for (i=0;i<gCom.nRcvlen;i++)
{
if(gCom.chRcvbuf[i]!=0x02)
{
if(gCom.chRcvbuf[i]==0x03)
break;
chState[j] = gCom.chRcvbuf[i];
j++;
}
}
if(chState[0] != 'P')
return _ERR_KDE_STATUS;
if((chState[4] != '1')&&(chState[4] != '2'))
return _KDE_NOCARD;
else if(chState[4]=='2')
return _KDE_CARD_INSIDE;
return _KDE_CARD_ATGATE;
}
short CKDMCard::zSelectExtPort()
{
char chCommand[256];
int nLen,ret;
if(mExtPort<1 || mExtPort>5)
return 0;
if(!gCom.AsynOpen())
return -1;
memset(chCommand,'\0',256);
switch (mExtPort)
{
case 1:
strcpy(chCommand,"\x1b%A");
break;
case 2:
strcpy(chCommand,"\x1b%B");
break;
case 3:
strcpy(chCommand,"\x1b%C");
break;
case 4:
strcpy(chCommand,"\x1b%D");
break;
case 5:
strcpy(chCommand,"\x1b%K");
break;
}
nLen = strlen(chCommand);
gCom.AsynSendData(chCommand,strlen(chCommand));
ret = gCom.sReadByLen(3,1);
if(ret < 3)
return -3;
return 0;
}
/************************
扩口器选择:
入口参数:A,B,C,D,K(注意用大写)
*************************/
void CKDMCard::zSetExtPort(short ExtPort)
{
mExtPort = ExtPort;
}
short CKDMCard::zSetPort(short SerialPort)
{
int baudrate;
char parity[2];
char bytesize[2];
char stopbits[2];
gCom.AsynClose();
mSerialPort = SerialPort;
baudrate = GetPrivateProfileInt("KDE4787","BaudRate", 9600, "KDDEV.INI");
GetPrivateProfileString("KDE4787", "Parity", "N", parity, 2, "KDDEV.INI");
GetPrivateProfileString("KDE4787", "ByteSize", "8", bytesize, 2, "KDDEV.INI");
GetPrivateProfileString("KDE4787", "StopBits", "1", stopbits, 2, "KDDEV.INI");
gCom.SetupParam(mSerialPort, baudrate, parity[0], bytesize[0], stopbits[0]);
return 0;
}
/*************************************
返回:
正确:0
其他:错误
**************************************/
short CKDMCard::gOpenKDMCom()
{
short ret;
ret =zSelectExtPort();
if(ret!=0)
{
return ret;
}
if(mSerialPort <=0) return _NO_DEVICE;
if(!gCom.AsynOpen())
{
return -1;
}
return 0;
}
// Mode 1 -- IC卡
// 0 -- 磁卡
short CKDMCard::KDM_EntryCard(short Mode)
{
char chSend[256],chSend1[256],chState[20];
int bcc, i, j;
memset(chSend, '\0', 256);
memset(chSend1, '\0',256);
memset(chState, '\0', 20);
//'进卡命令' 'C' 32H pm (0x30'pm磁卡 0x31 'IC卡并对IC卡上电 0x32'IC卡不对IC卡上电)
sprintf(chSend, "%cC2%d%c", 0x02,Mode, 0x03);
bcc = KDM4787_BCC(chSend, strlen(chSend));
sprintf(chSend1, "%s%c%c", chSend, bcc, 0x0D);
if(!gCom.AsynSendData(chSend1, strlen(chSend1)))
{
return _ERR_KDE_ENTRY1;
}
if(!gCom.sReadByEndChar(0x0d, gTimeOut))
{
return _ERR_KDE_ENTRY1;
}
if(gCom.chRcvbuf[0]!=0x06)
return _ERR_KDE_ENTRY1;
memset(chSend, '\0', 256);
ENQ(chSend);
if(!gCom.AsynSendData(chSend,strlen(chSend)))
{
return _ERR_KDE_ENTRY2;
}
if(!gCom.sReadByEndChar(0x0d, gTimeOut))
{
return _ERR_KDE_ENTRY2;
}
j = 0;
int ret;
for (i=0;i<gCom.nRcvlen;i++)
{
if(gCom.chRcvbuf[i]!=0x02)
{
if(gCom.chRcvbuf[i]==0x03)
break;
chState[j] = gCom.chRcvbuf[i];
j++;
}
}
if(chState[0] != 'P')
return _ERR_KDE_STATUS;
if((chState[4] != '1')&&(chState[4] != '2'))
return _KDE_NOCARD;
else if(chState[4]=='1')
ret = _KDE_CARD_ATGATE;
else
ret = _KDE_CARD_INSIDE;
return ret;
}
short CKDMCard::MagneticRead123(short mode, char *Trk1, char *Trk2, char *Trk3)
{
char chSend[256],chSend1[256],chState[20];
int bcc,i ,j;
char gTimeOut=2;
memset(chSend, '\0', 256);
memset(chSend1, '\0', 256);
memset(chState, '\0', 20);
//读磁卡指令' 'C' 36H pm 'pm 30H 仅卡移动(用于清洗卡) '31H 读第一轨
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -