📄 testdemodlg.cpp
字号:
// TestDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TestDemo.h"
#include "TestDemoDlg.h"
#include ".\testdemodlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma pack(1)
#pragma pack()
HINSTANCE hCardDll = NULL;
static bool s_comopen = false;
// 导入函数
typedef int (__stdcall* LPConnectMF)(int nPortNo, int nBaudRate);
typedef int (__stdcall* LPCloseMF)();
typedef int (__stdcall* LPControlBuzzer)();
typedef int (__stdcall* LPReadCardMainKey2)(short wantFlag);
typedef int (__stdcall* LPSMT_ReadStuempNo)(unsigned char stuemp_no[21]);
LPConnectMF ConnectMF = NULL;
LPCloseMF CloseMF = NULL;
LPControlBuzzer ControlBuzzer = NULL;
LPReadCardMainKey2 ReadCardMainKey2 = NULL;
LPSMT_ReadStuempNo SMT_ReadStuempNo = NULL;
int LoadCom()
{
if (hCardDll == NULL)
{
hCardDll = LoadLibrary("CardDll.dll");
if (!hCardDll)
return -1;
ConnectMF = (LPConnectMF)GetProcAddress(hCardDll, "ConnectMF");
CloseMF = (LPCloseMF)GetProcAddress(hCardDll, "CloseMF");
ControlBuzzer = (LPControlBuzzer)GetProcAddress(hCardDll, "ControlBuzzer");
SMT_ReadStuempNo = (LPSMT_ReadStuempNo)GetProcAddress(hCardDll, "SMT_ReadStuempNo");
ReadCardMainKey2 = (LPReadCardMainKey2)GetProcAddress(hCardDll, "ReadCardMainKey2");
if (
!(ConnectMF) ||
!(CloseMF) ||
!(ControlBuzzer) ||
!(SMT_ReadStuempNo) ||
!(ReadCardMainKey2)
)
{
FreeLibrary(hCardDll);
hCardDll = NULL;
return -2;
}
}
return 0;
}
void CloseCom()
{
if(hCardDll)
{
FreeLibrary(hCardDll);
hCardDll = NULL;
}
}
// Unsigned char* To char*
void ucStrToStr(unsigned char *ucStr, int ucStrLen, char *Str)
{
int j = 0;
for (int i = 0; i < ucStrLen; ++i)
{
j = ucStr[i];
sprintf(&Str[2 * i], "%02X", j);
}
}
int dec2bcd(unsigned char *sDecStr,unsigned char *sBcdStr,int bcd_len)
{
int i;
unsigned char lch,hch;
for(i=0;i<bcd_len;i++)
{
lch=sDecStr[2*i]-'0';
hch=sDecStr[2*i+1]-'0';
lch=lch<<4;
hch=hch&0x0F;
lch=lch&0xF0;
sBcdStr[i]=hch|lch;
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDemoDlg dialog
CTestDemoDlg::CTestDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDemoDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDemoDlg)
/*DDX_Control(pDX, IDC_EDIT2, m_edit_Balance);*/
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDemoDlg, CDialog)
//{{AFX_MSG_MAP(CTestDemoDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_BTN_COM, OnOpenCom)
//}}AFX_MSG_MAP
ON_BN_CLICKED(ID_EXIT, OnBnClickedExit)
ON_BN_CLICKED(IDC_READ_AUTHCARD, OnBnClickedReadAuthcard)
ON_BN_CLICKED(ID_READ_CARDINFO, OnBnClickedReadCardinfo)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDemoDlg message handlers
BOOL CTestDemoDlg::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
if (LoadCom())
{
::AfxMessageBox("动态库加载失败");
return FALSE;
}
((CComboBox*)GetDlgItem(IDC_COM_BAND))->SetCurSel(0);
((CComboBox*)GetDlgItem(IDC_COM_PORT))->SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDemoDlg::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 CTestDemoDlg::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 CTestDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDemoDlg::OnOpenCom()
{
// TODO: Add your control notification handler code here
int ret = 0;
CString err_msg = "";
if (s_comopen)
{
ret = CloseMF();
if (ret == 0)
{
s_comopen = false;
GetDlgItem(ID_BTN_COM)->SetWindowText("打开串口");
}
else
{
err_msg.Format("关闭串口失败--[%d]", ret);
AfxMessageBox(err_msg);
}
}
else
{
int band = ((CComboBox*)GetDlgItem(IDC_COM_BAND))->GetCurSel();
int port = ((CComboBox*)GetDlgItem(IDC_COM_PORT))->GetCurSel() + 1;
if(port < 1) port = 1;
if(band < 0) band = 0;
// baud设置参看文档
if (ret = ConnectMF(port, band))
{
err_msg.Format("打开串口失败--[%d]", ret);
AfxMessageBox(err_msg);
}
GetDlgItem(ID_BTN_COM)->SetWindowText("关闭串口");
ControlBuzzer();
s_comopen = true;
}
return ;
}
void CTestDemoDlg::OnBnClickedExit()
{
// TODO: 在此添加控件通知处理程序代码
if (s_comopen)
{
CloseMF();
}
OnCancel();
}
void CTestDemoDlg::OnBnClickedReadAuthcard()
{
// TODO: 在此添加控件通知处理程序代码
int ret = 0;
BYTE ucCardFlag = 1; // 复旦授权卡标志
char msg[1025] = "";
CString err_msg = "";
if (s_comopen)
{
ret = ReadCardMainKey2(ucCardFlag);
if (ret)
{
err_msg.Format("加载密钥失败--[%d]", ret);
AfxMessageBox(err_msg);
return ;
}
AfxMessageBox("加载密钥成功");
}
else
{
AfxMessageBox("串口未有打开");
}
return ;
}
void CTestDemoDlg::OnBnClickedReadCardinfo()
{
// TODO: 在此添加控件通知处理程序代码
// TODO: 在此添加控件通知处理程序代码
int ret = 0;
unsigned char stuemp_no[22] = "";
CString cs = "";
CString err_msg = "";
if (s_comopen)
{
ret = SMT_ReadStuempNo(stuemp_no);
if (ret)
{
err_msg.Format("读取学工号失败[%d]", stuemp_no);
AfxMessageBox(err_msg);
}
SetDlgItemText(ID_EDIT_STUEMP, (char *)stuemp_no);
cs.Format("学工号 %s\n", stuemp_no);
AfxMessageBox(cs);
}
else
{
AfxMessageBox("串口未有打开");
}
return ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -