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

📄 cardreviewerdlg.cpp

📁 IC 卡读写操作VC版。有读写功能
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// CardReviewerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "CardReviewer.h"
#include "CardReviewerDlg.h"

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

#include ".\UtilStrOper\\StringOper.h"
#include "printer.h"
#include "math.h"
#pragma comment(lib, "dcic32.lib");

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

INT HexStrToBytes(char *strSou, BYTE *bytDes, int bytCount)
{
    INT i;
    BYTE HighByte,LowByte;
    
    for (i = 0; i < bytCount*2; i=i+2)
    {
        HighByte = strSou[i];
        LowByte  = strSou[i + 1];

        if  (HighByte >= 0x61 && HighByte <= 0x66)
            HighByte -= 0x57;
        else if (HighByte >= 0x41 && HighByte <= 0x46)
            HighByte -= 0x37;
        else if (HighByte >= 0x30 && HighByte <= 0x39)
            HighByte -= 0x30;

        if  (LowByte >= 0x61 && LowByte <= 0x66)
            LowByte -= 0x57;
        else if (LowByte >= 0x41 && LowByte <= 0x46)
            LowByte -= 0x37;
        else if (LowByte >= 0x30 && LowByte <= 0x39)
            LowByte -= 0x30;
            
        bytDes[i / 2] = (HighByte << 4) | LowByte;
    }
    
    return bytCount;
}

INT BytesToHexStr(char *strDes, BYTE *bytSou, int bytCount)
{
    INT i;
    BYTE HighByte, LowByte;
    
    for (i = 0; i < bytCount; i++)
    {
        HighByte = bytSou[i] >> 4;
        LowByte  = bytSou[i] & 0x0F;
        
        HighByte += 0x30;
        if (HighByte > 0x39)	strDes[i * 2] = HighByte + 0x07;
        else	strDes[i * 2] = HighByte;
        
        LowByte += 0x30;
        if (LowByte > 0x39) 	strDes[i * 2 + 1] = LowByte + 0x07;
        else	strDes[i * 2 + 1] = LowByte;
    }
    return bytCount * 2;
}

void strRevHexString(char *SouStr, int iLen, char *DesStr)
{
    int		i, j;
    
    if ( iLen % 2 != 0 ) exit(0);
    
    for (i=iLen-2, j=0; i>=0 ; i-=2,j+=2)
    {
        memcpy(&DesStr[j],&SouStr[i],2);
    }
}

long HexStrToLong(char *SouStr, int iLen)
{
    int i;
    long ret = 0;
    
    for (i=0; i<iLen; i++)
        ret = ret + (long)((SouStr[i]-0x30) * pow(16, iLen-i-1));
    return ret;
}

STDAPI StrXor(char* SouStr, int iLen, char* DesStr)
{
    int i;
    BYTE temp=0;
    BYTE temp1[255];
    
    if ( iLen % 2 != 0 ) exit(0);
    iLen = iLen / 2;
    
    HexStrToBytes(SouStr, temp1, iLen);
    for (i=0; i<iLen; i++) temp = temp ^ temp1[i];
    BytesToHexStr(DesStr, &temp, 1);

	return 0;
}
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_WM_CLOSE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCardReviewerDlg dialog

CCardReviewerDlg::CCardReviewerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CCardReviewerDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CCardReviewerDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCardReviewerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CCardReviewerDlg)
	DDX_Control(pDX, IDC_LIST_Analyse, m_ListAnalyse);
	DDX_Control(pDX, IDC_LISTTransRep, m_ListCtlTranRep);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CCardReviewerDlg, CDialog)
	//{{AFX_MSG_MAP(CCardReviewerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONPrintReport, OnBUTTONPrintReport)
	ON_BN_CLICKED(IDC_BUTTONUnBlock, OnBUTTONUnBlock)
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTONReadCard, OnBUTTONReadCard)
	ON_BN_CLICKED(IDC_BUTTONUnBlockCard, OnBUTTONUnBlockCard)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CCardReviewerDlg message handlers

BOOL CCardReviewerDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	m_ListCtlTranRep.InsertColumn(0,"序号",0,40,0);
	m_ListCtlTranRep.InsertColumn(1,"交易时间",0,70,0);
	m_ListCtlTranRep.InsertColumn(2,"原额",0,100,0);
	m_ListCtlTranRep.InsertColumn(3,"交易金额",0,100,0);
	m_ListCtlTranRep.InsertColumn(4,"类型",0,80,0);
	m_ListCtlTranRep.InsertColumn(5,"终端号",0,90,0);
	m_ListCtlTranRep.SetExtendedStyle(m_ListCtlTranRep.GetExtendedStyle() | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	// 读取相关的配置信息
	ReadSysConfig();
	
	m_bTrueIsOpen = false;
	m_bTrueIsReset = false;
	memset(m_szCardData, 0, sizeof(m_szCardData));

	m_hPOS = 0;
	if (!ConnectReader())
	{
		MessageBox("Card Reader Init Error!");
		exit(0);
	}
	CreatMakeCardThread();

	return TRUE;  // return TRUE  unless you set the focus to a control
}
// 读取配置文件信息
void CCardReviewerDlg::ReadSysConfig(void)
{
	char szFilePath[512] = {0};
	

	GetModuleFileName(NULL, szFilePath, sizeof(szFilePath));
	char *pszPos = strrchr(szFilePath, '\\');
	*pszPos = '\0';

	char szIniName[512] = {0};
	_snprintf(szIniName, sizeof(szIniName), "%s\\config.ini", szFilePath);
	
	// COM 端口号码
	this->m_ComPort = GetPrivateProfileInt("system", "ComPort", 1, szIniName);

	memset(m_szPrinterPort, 0, sizeof(m_szPrinterPort));
	GetPrivateProfileString("system", "PrintPort", "LPT1", 
			m_szPrinterPort, sizeof(m_szPrinterPort), szIniName);

}

void CCardReviewerDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCardReviewerDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCardReviewerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCardReviewerDlg::OnBUTTONPrintReport() 
{
	CPrinter p;
	CString msg;
	p.SetPort(m_szPrinterPort);

	char szDatas[1024] = {0};
	this->GetDlgItemText(IDC_EDITAnaRes, szDatas, sizeof(szDatas));

	if ('\0' == szDatas[0] && 0 >= m_ListAnalyse.GetCount())
	{
		MessageBox("请先读卡!");
		return ;
	}

	try
	{
		if (!p.Open())
		{
			MessageBox("打印机打开失败,请检查");
			return;
		}
		
		//打印头
		p.Write("   哈尔滨城市通-卡片校验报告");
		p.Write("\r");
		char szTimeNow[64] = {0};
		SYSTEMTIME st;
		GetLocalTime(&st);  
		_snprintf(szTimeNow, sizeof(szTimeNow) - 1, "检测时间:%04d%02d%02d", 
							st.wYear, st.wMonth, st.wDay);
		p.Write(szTimeNow);
		char szCardNo[64] = {0};
		this->GetDlgItemText(IDC_EDITCardNo, szCardNo, sizeof(szCardNo));
		char szPrintInfo[64] = {0};
		_snprintf(szPrintInfo, sizeof(szPrintInfo) - 1, "卡内号:%s", 
							szCardNo);
		p.Write(szPrintInfo);
		char szAmt[64] = {0};
		this->GetDlgItemText(IDC_EDITPurseAmt, szAmt, sizeof(szAmt));
		_snprintf(szPrintInfo, sizeof(szPrintInfo) - 1, "卡内余额:%s", 
							szAmt);
		p.Write(szPrintInfo);
		p.Write("\r");
		
		for (int nCur = 0; nCur < m_ListAnalyse.GetCount(); nCur++)
		{
			char szInfo[64];
			m_ListAnalyse.GetText(nCur, szInfo);
			p.Write(szInfo);
		}
		p.Write("\r");

		char *pszPos = szDatas;
		for (;;)
		{
			if ('\0' == *pszPos)
			{
				break;
			}
			char *pszSplit = strstr(pszPos, "\r\n");
			*pszSplit = '\0';
			p.Write(pszPos);
			pszPos += strlen(pszPos) + 2;
		}

		for(int i=0;i<3;i++)
		{
			p.Write("\r\n");
		}

		p.Close();
	}
	catch(...)
	{

	}
}

// 关闭读卡器
void CCardReviewerDlg::CloseReaderCom(void)
{
	IC_ExitComm(m_hPOS);
}
// SAM 卡复位
bool CCardReviewerDlg::ResetSAMCard(char (&szATR) [64])
{
	bool bRes = true;
	return bRes;
}
// 设置SAM速率
bool CCardReviewerDlg::SetSAMRate(bool bHign)
{
	return true;
}
int CCardReviewerDlg::SendApdu(int nInd, char *pszAPDU, char *pszResp)

{
	int st;
	unsigned char relen;
	unsigned char redata[256] = {0};
	st=IC_CpuApdu_Hex(m_hPOS,strlen(pszAPDU) / 2,(BYTE *)pszAPDU,&relen,redata);
	if (st!=0)
	{
		return -1;
	}
	if (NULL != pszResp)
	{
		memcpy(pszResp, redata, relen * 2);
	}
	return relen;
}
// 选择通讯通道
bool CCardReviewerDlg::SelectChannel(int nInx)
{
	return true;
}
bool CCardReviewerDlg::LoadKey(unsigned char* key, BYTE bSec)
{
    //CLA   INS     P1     P2     LC     DATA     LE
    //00     64     00     00     06     KEY      空
    char inData[23];
	if (0 != IC_Load_Keyhex(m_hPOS, 0, bSec, key))
	{
		return false;
	}

    return true;
}
bool CCardReviewerDlg::Authentic(int sector,int mode)
{
	if (0 != IC_Authentication(m_hPOS, 0, sector))
    {
        return false;
    }

    return true;
}

bool CCardReviewerDlg::WriteBlock(int sector,int block, unsigned char* pData)
{
	if (0 != IC_WriteMifare_Hex(m_hPOS, sector * 4 + block, pData))
	{
		return false;
	}
	return true;
}
bool CCardReviewerDlg::ReadBlock(int sector,int block, unsigned char* pData)
{
    //读相应卡区的数据。

⌨️ 快捷键说明

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