📄 testdlg.cpp
字号:
// testDlg.cpp : implementation file
//
#include "stdafx.h"
#include "test.h"
#include "testDlg.h"
#include "casecodedll.h"
#include <string>
#include <process.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg dialog
CTestDlg::CTestDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTestDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTestDlg)
m_szRet = _T("");
m_szBillTypes = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CTestDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTestDlg)
DDX_Control(pDX, IDC_PORT, m_PortList);
DDX_Text(pDX, IDC_RICHEDIT1, m_szRet);
DDX_Text(pDX, IDC_BILL_TYPES, m_szBillTypes);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
//{{AFX_MSG_MAP(CTestDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_TEST, OnTest)
ON_BN_CLICKED(IDC_RESET, OnReset)
ON_BN_CLICKED(IDC_ENABLE_BILLTYPES, OnEnableBilltypes)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
ON_BN_CLICKED(IDC_START, OnStart)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_BN_CLICKED(IDC_GET_ID, OnGetId)
ON_BN_CLICKED(IDC_GET_BILLTYPES, OnGetBilltypes)
ON_BN_CLICKED(IDC_GETTABLE, OnGettable)
ON_BN_CLICKED(IDC_CONNECT, OnConnect)
ON_BN_CLICKED(IDC_B2B_Mode, OnB2BMode)
ON_BN_CLICKED(IDC_ACCEPT, OnAccept)
ON_BN_CLICKED(IDC_RETURN, OnReturn)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestDlg message handlers
BOOL CTestDlg::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 (NAK == m_Dll.ConnectToDrv(2, 9600)) {
// MessageBox("error connect to com1");
/// }
m_PortList.SetCurSel(0);
m_szBillTypes.Format("%s", "000000000000000000000000");
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTestDlg::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 CTestDlg::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 CTestDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTestDlg::OnTest()
{
// TODO: Add your control notification handler code here
CCaseCodeDll dll;
dll.test();
}
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
m_Dll.CloseConnection();
CDialog::OnOK();
}
void CTestDlg::OnReset()
{
// TODO: Add your control notification handler code here
INT nRet = m_Dll.Reset();
CString msg;
msg.Format("\r\nMethod:Reset return = %d", nRet);
m_szRet += msg;
switch(nRet) {
case ACK:
msg.Format("\r\n%s", "Reset 执行成功");
break;
case CSS_INVALID_CMD:
msg.Format("\r\n%s", "Reset 是无效的命令");
break;
case NAK:
msg.Format("\r\n%s", "Reset 执行失败");
break;
default:
msg = "";
break;
}
UpdateData();
m_szRet += msg;
UpdateData(FALSE);
}
void CTestDlg::OnEnableBilltypes()
{
// TODO: Add your control notification handler code here
UpdateData();
unsigned char types[BILL_TYPES_LEN] = {'0'};
for (INT i = 0; i < min(BILL_TYPES_LEN, m_szBillTypes.GetLength()); i++) {
types[i] = m_szBillTypes.GetAt(i);
}
INT nRet = m_Dll.EnableBillTypes(types);
CString msg;
msg.Format("\r\nMethod:EnableBillTypes return = %d", nRet);
m_szRet += msg;
switch(nRet) {
case ACK:
msg.Format("\r\n%s", "EnableBilltypes 执行成功");
break;
case CSS_INVALID_CMD:
msg.Format("\r\n%s", "EnableBilltypes 是无效的命令");
break;
case NAK:
msg.Format("\r\n%s", "EnableBilltypes 执行失败");
default:
msg = "";
break;
}
m_szRet += msg;
UpdateData(FALSE);
}
void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
m_szRet = "";
UpdateData(FALSE);
}
void CTestDlg::OnStart()
{
// TODO: Add your control notification handler code here
unsigned char nMore;
unsigned char nRet;
CString szMsg;
UpdateData();
m_szRet += "\n请放入纸币";
UpdateData(FALSE);
unsigned char cPreStat = 99;
g_bStopPoll = false;
MSG msg;
while (!g_bStopPoll) {
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (ACK == m_Dll.Poll()) {
nRet = m_Dll.GetResponse(nMore);
if ((cPreStat != nRet)) {
cPreStat = nRet;
switch(nRet) {
case CCS_INITIALIZE:
szMsg = "初始化";
break;
case CCS_IDLING:
szMsg = "空闲中";
break;
case CCS_ACCEPTING:
szMsg = "0x15 正在接收纸币";
break;
case CCS_STACKING:
szMsg = "0x17 纸币在缓冲区中";
break;
case CCS_RETURNING:
szMsg.Format("%s - More=%d", "0x18 退出纸币,直到用户取回,本状态才会改变", nMore);
break;
case CCS_REJECTING:
szMsg.Format("%s - More=%d", "Rejecting 0x1c", nMore);
break;
case CCS_DISABLED:
szMsg = "0x19 纸币器被禁用,在初始化后,自动设为此状态";
break;
case CCS_HOLDING:
szMsg = "纸币被保持在缓冲区中";
break;
case CCS_BUSY:
szMsg = "设备忙,无法相应命令";
break;
case CCS_STACKED:
szMsg.Format("Bill Stacked bill=%d", nMore);
break;
case CCS_RETURNED:
szMsg.Format("Bill Returned bill=%d", nMore);
break;
case CCS_ESCROW:
szMsg.Format("Escrow position bill=%d", nMore);
break;
default:
szMsg.Format("%x", nRet);
break;
}
Update(TRUE);
m_szRet += "\r\n";
m_szRet += szMsg;
Update(FALSE);
}// end if (cPreStat != nRet
}
else
{
szMsg = "Poll error";
Update(TRUE);
m_szRet += "\r\n";
m_szRet += szMsg;
Update(FALSE);
}
//if ( (CCS_BUSY == nRet) || (CCS_DISABLED == nRet) || (CCS_STACKED == nRet) || (CCS_RETURNED == nRet) || (CCS_ESCROW == nRet) )
// break;
}
}
void CTestDlg::OnStop()
{
// TODO: Add your control notification handler code here
g_bStopPoll = true;
}
void CTestDlg::OnGetId()
{
// TODO: Add your control notification handler code here
unsigned char pn[PN_LEN + 1] = {0};
unsigned char sn[SN_LEN + 1] = {0};
unsigned char an[AN_LEN + 1] = {0};
INT nRet = m_Dll.GetIdentification(pn, sn, an);
CString msg;
switch(nRet) {
case ACK:
msg.Format("\r\n%s", "GetIdentification 执行成功");
break;
case CSS_INVALID_CMD:
msg.Format("\r\n%s", "GetIdentification 是无效的命令");
break;
case NAK:
msg.Format("\r\n%s", "GetIdentification 执行失败");
default:
break;
}
UpdateData();
m_szRet += msg;
msg.Format("\r\npn:%s\r\nsn:%s\r\nan:%s", pn, sn, an);
m_szRet += msg;
UpdateData(FALSE);
}
void CTestDlg::OnGetBilltypes()
{
// TODO: Add your control notification handler code here
unsigned char types[STATUS_LEN + 1] = {0};
INT nRet = m_Dll.GetStatus(types);
CString msg;
switch(nRet) {
case ACK:
msg.Format("\r\n%s", "GetStatus 执行成功");
break;
case CSS_INVALID_CMD:
msg.Format("\r\n%s", "GetStatus 是无效的命令");
break;
default:
msg.Format("\r\n%s", "GetStatus 执行失败");
break;
}
UpdateData();
m_szRet += msg;
msg.Format("\r\nStatus:%s", types);
m_szRet += msg;
UpdateData(FALSE);
}
void CTestDlg::Update(BOOL bFlag)
{
UpdateData(bFlag);
}
void CTestDlg::OnGettable()
{
// TODO: Add your control notification handler code here
unsigned char types[BILL_TYPES_LEN + 1] = {0};
INT nRet = m_Dll.GetBillTable(types);
CString msg;
switch(nRet) {
case ACK:
msg.Format("\r\n%s%d", "GetBillTable 执行成功; 返回数据长度", m_Dll.GetLastResponseLen());
break;
case CSS_INVALID_CMD:
msg.Format("\r\n%s", "GetBillTable 是无效的命令");
break;
default:
msg.Format("\r\n%s", "GetBillTable 执行失败");
break;
}
UpdateData();
m_szRet += msg;
msg.Format("\r\BillTable:%s", types);
m_szRet += msg;
UpdateData(FALSE);
}
void CTestDlg::OnConnect() {
UpdateData();
m_szRet += "\r\n";
if (0 != m_Dll.ConnectToDrv((m_PortList.GetCurSel() + 1), 9600)) {
m_szRet += "error connecto com";
}
else {
m_szRet += "connect ok";
}
UpdateData(FALSE);
}
void CTestDlg::OnB2BMode()
{
// TODO: Add your control notification handler code here
UpdateData();
INT nRet = m_Dll.EnableB2BMode();
CString msg;
switch(nRet) {
case ACK:
msg.Format("\r\n%s%d", "GetBillTable 执行成功; 返回数据长度", m_Dll.GetLastResponseLen());
break;
case CSS_INVALID_CMD:
msg.Format("\r\n%s", "GetBillTable 是无效的命令");
break;
default:
msg.Format("\r\n%s", "GetBillTable 执行失败");
break;
}
m_szRet += msg;
UpdateData(FALSE);
}
void CTestDlg::OnAccept()
{
// TODO: Add your control notification handler code here
m_Dll.AcceptBill();
}
void CTestDlg::OnReturn()
{
// TODO: Add your control notification handler code here
m_Dll.ReturnBill();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -