www.pudn.com > ReadTypeAB.rar > ReadTypeABDlg.cpp
// ReadTypeABDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ReadTypeAB.h"
#include "ReadTypeABDlg.h"
#include "comm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CReadTypeABDlg dialog
CReadTypeABDlg::CReadTypeABDlg(CWnd* pParent /*=NULL*/)
: CDialog(CReadTypeABDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CReadTypeABDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CReadTypeABDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CReadTypeABDlg)
DDX_Control(pDX, IDC_LIST1, m_lstNum);
DDX_Control(pDX, IDC_COMBO1, m_comPort);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CReadTypeABDlg, CDialog)
//{{AFX_MSG_MAP(CReadTypeABDlg)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_CLOSE, OnClose)
ON_BN_CLICKED(IDC_READ_TYPEA, OnReadTypea)
ON_BN_CLICKED(IDC_READ_TYPEB, OnReadTypeb)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CReadTypeABDlg message handlers
BOOL CReadTypeABDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 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
CenterWindow(GetDesktopWindow()); // center to the hpc screen
// TODO: Add extra initialization here
m_comPort.AddString(_T("COM1"));
m_comPort.AddString(_T("COM2"));
m_comPort.AddString(_T("COM3"));
m_comPort.AddString(_T("COM4"));
m_comPort.AddString(_T("COM5"));
m_comPort.AddString(_T("COM6"));
m_comPort.AddString(_T("COM7"));
m_comPort.AddString(_T("COM8"));
m_comPort.AddString(_T("COM9"));
m_comPort.SetCurSel(0);
GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
GetDlgItem(IDC_READ_TYPEA)->EnableWindow(FALSE);
GetDlgItem(IDC_READ_TYPEB)->EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
HANDLE hComm = NULL;
void CReadTypeABDlg::OnConnect()
{
// TODO: Add your control notification handler code here
CString strPort;
m_comPort.GetLBText(m_comPort.GetCurSel(), strPort);
char port[10];
memset(port,0,10);
#ifdef _WIN32_WCE
WideCharToMultiByte(CP_ACP, 0, strPort, -1, port, 10, 0, 0);
#else
strcpy(port,strPort);
#endif
CloseComm(hComm);
BOOL bOpen = OpenComm(hComm, port,19200);
if (!bOpen)
{
MessageBox(_T("Cannot open com port!"));
return;
}
GetDlgItem(IDC_CONNECT)->EnableWindow(FALSE);
GetDlgItem(IDC_CLOSE)->EnableWindow(TRUE);
GetDlgItem(IDC_READ_TYPEA)->EnableWindow(TRUE);
GetDlgItem(IDC_READ_TYPEB)->EnableWindow(TRUE);
}
void CReadTypeABDlg::OnClose()
{
// TODO: Add your control notification handler code here
BOOL bClose = CloseComm(hComm);
hComm = NULL;
GetDlgItem(IDC_CONNECT)->EnableWindow(TRUE);
GetDlgItem(IDC_CLOSE)->EnableWindow(FALSE);
GetDlgItem(IDC_READ_TYPEA)->EnableWindow(FALSE);
GetDlgItem(IDC_READ_TYPEB)->EnableWindow(FALSE);
}
void CReadTypeABDlg::OnReadTypea()
{
// TODO: Add your control notification handler code here
ASSERT(hComm);
int nRead;
unsigned char buff[1024];
BOOL bWrite;
//写置TypeB命令
memset(buff,0,1024*sizeof(unsigned char));
strcpy((char*)buff, "60030186412B");
buff[12] = 0x03;
bWrite = WriteComm(hComm, buff,13);
//读应答数据
memset(buff, 0, 1024*sizeof(unsigned char));
nRead = ReadComm(hComm, buff, 1024);
if (nRead <= 0)
ASSERT(false);
//写命令
memset(buff, 0, 1024 * sizeof(unsigned char) );
//0x60 0x04 0x01 0x61 0x00 0x00 0x04 0x03
strcpy((char*)buff,"60040161000004");
buff[14] = 0x03;
bWrite = WriteComm(hComm,buff,15);
ASSERT(bWrite);
//读卡号
memset(buff, 0, 1024 * sizeof(unsigned char) );
nRead = ReadComm(hComm,buff, 1024);
if (nRead <= 0)
return;
//没有读到卡片
if (strncmp((const char*)buff, "6005", 4)!=0)
{
MessageBox(_T("No card Detected.."), _T("Error"), MB_ICONERROR);
return;
}
//已经读出了卡号buff[4..11];
//编码
char hex_num[100];
memset(hex_num, 0, 100);
strncpy(hex_num,(const char*)buff+4,8);
CString str;
str = hex_num;
MessageBeep(MB_OK);
m_lstNum.InsertString(0,str);
}
void CReadTypeABDlg::OnReadTypeb()
{
// TODO: Add your control notification handler code here
ASSERT(hComm);
BOOL bWrite;
int nRead;
unsigned char buff[1024];
//写置TypeB命令
memset(buff,0,1024*sizeof(unsigned char));
strcpy((char*)buff, "60030186422C");
buff[12] = 0x03;
bWrite = WriteComm(hComm, buff,13);
//读应答数据
memset(buff, 0, 1024*sizeof(unsigned char));
nRead = ReadComm(hComm, buff, 1024);
if (nRead <= 0)
{
MessageBox(_T("No Response!"),_T("Error"),MB_ICONERROR);
return;
}
//写命令
memset(buff, 0, 1024 * sizeof(unsigned char) );
strcpy((char*)buff,"60020188EB");
buff[10] = 0x03;
bWrite = WriteComm(hComm,buff,11);
ASSERT(bWrite);
//读卡号
memset(buff, 0, 1024 * sizeof(unsigned char) );
nRead = ReadComm(hComm,buff, 1024);
if (nRead <= 0)
{
MessageBox(_T("No Response!"),_T("Error"),MB_ICONERROR);
return;
}
//没有读到卡
if (strncmp((const char*)buff, "600D50", 6) != 0)
{
TCHAR ts[1024];
// MultiByteToWideChar(CP_ACP,0,(const char*)buff,-1,ts,1024);
MessageBox(ts);
MessageBox(_T("No card Detected.."), _T("Error"), MB_ICONERROR);
return;
}
//已经读出了卡号buff[6..13];
//编码
char hex_num[100];
memset(hex_num, 0, 100);
strncpy(hex_num, (const char*)buff+6, 8);
CString str;
str = hex_num;
MessageBeep(MB_OK);
m_lstNum.InsertString(0,str);
}