?? autochangemoneydlg.cpp
字號:
// AutoChangeMoneyDlg.cpp : implementation file
//
#include "stdafx.h"
#include "AutoChangeMoney.h"
#include "AutoChangeMoneyDlg.h"
#include "charge1.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()
/////////////////////////////////////////////////////////////////////////////
// CAutoChangeMoneyDlg dialog
CAutoChangeMoneyDlg::CAutoChangeMoneyDlg(CWnd* pParent /*=NULL*/)
: CDialog(CAutoChangeMoneyDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CAutoChangeMoneyDlg)
m_rest = 0;
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CAutoChangeMoneyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAutoChangeMoneyDlg)
DDX_Control(pDX, IDC_REST, m_crest);
DDX_Control(pDX, IDC_PUTMONEY, m_putmoney);
DDX_Control(pDX, IDC_CONSUME, m_consume);
DDX_Control(pDX, IDC_CODE, m_code);
DDX_Text(pDX, IDC_REST, m_rest);
DDX_Control(pDX, IDC_MSCOMM1, m_comm);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAutoChangeMoneyDlg, CDialog)
//{{AFX_MSG_MAP(CAutoChangeMoneyDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_CALCU, OnCalcu)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoChangeMoneyDlg message handlers
BOOL CAutoChangeMoneyDlg::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
return TRUE; // return TRUE unless you set the focus to a control
}
void CAutoChangeMoneyDlg::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 CAutoChangeMoneyDlg::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();
/////////////////////////////////////////////////
//設置串口隨界面打開
/////////////////////////////////////////////////
m_comm.SetCommPort(1); //
m_comm. SetInBufferSize(1024); //設置輸入緩沖區的大小,Bytes
m_comm. SetOutBufferSize(1024); //設置輸入緩沖區的大小,Bytes//
if(!m_comm.GetPortOpen()) //打開串口
m_comm.SetPortOpen(TRUE);
m_comm.SetInputMode(1); //設置輸入方式為二進制方式
m_comm.SetSettings("9600,n,8,1"); //設置波特率等參數
m_comm.SetRThreshold(1); //為1表示有一個字符引發一個事件
m_comm.SetInputLen(0);
///////////////////////////////////////////////////////
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CAutoChangeMoneyDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CAutoChangeMoneyDlg::OnOK()
{
// TODO: Add extra validation here
//CDialog::OnOK();
}
BOOL CAutoChangeMoneyDlg::PreTranslateMessage(MSG *pMsg)
{
HWND h1=::GetDlgItem(this->m_hWnd,IDC_CODE);
if((pMsg->hwnd==h1)&&(pMsg->message==WM_KEYDOWN))
if(pMsg->wParam==13)
OnEditEnter();
return CDialog::PreTranslateMessage(pMsg);
}
CString temp1,temp2,str;
unsigned int shiyuan =0,yiyuan =0,rest =0; //應該找的各面值的數量及總數
unsigned int count10 ,count01; //找零機中各面值的數量
BOOL m_flag =0; //標記是否可以進行找零
void CAutoChangeMoneyDlg::OnEditEnter()
{
_variant_t tenvalue,onevalue,havecharge;
m_code.GetWindowText(str);
if(str.IsEmpty())
return;
//連接數據庫
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=change;Data Source=XL-157342083604";
pConn->Open("","","",adConnectUnspecified);//同步連接數據庫
CString sql;
sql.Format("select *from change where id='%s'",str);
pRst->raw_Close();
//指向對應的行
pRst->Open((_variant_t)sql,_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);
while(!pRst->rsEOF)
{
m_consume.SetWindowText((_bstr_t)pRst->GetCollect("prc"));
m_putmoney.SetWindowText((_bstr_t)pRst->GetCollect("putmoney"));
tenvalue = pRst->GetCollect("tencount"); //讀數據庫中10元面值的人民幣有數量,并賦值給count10
onevalue = pRst->GetCollect("onecount"); //讀數據庫中1元面值的人民幣有數量,并賦值給count01
havecharge = pRst->GetCollect("flag"); //讀數據庫中的flag,判斷是否曾經已經找過零
pRst->MoveNext();
}
count10 = tenvalue.intVal;
count01 = onevalue.intVal;
m_consume.GetWindowText(temp1);
m_putmoney.GetWindowText(temp2);
if(havecharge.intVal)
{//如果本次記錄還沒有找零工作
m_rest=atoi(temp2)-atoi(temp1);
rest = m_rest;
UpdateData(FALSE);//從變量更新到控件
shiyuan=m_rest/10;//需要找出的10元面值的數量
yiyuan=m_rest%10;//需要找出的1元面值的數量
//根據找零機中10元和1元面值的數量對需要找出的各面值的數量進行調整
is(count10,count01,shiyuan,yiyuan,m_flag);
}
else
{//如果已經找過零
m_flag = 0;
MessageBox("重復找零失敗");
}
pRst->Close();
pConn->Close();
pRst.Release();
pConn.Release();
CoUninitialize();
}
void CAutoChangeMoneyDlg::OnCalcu()
{
// TODO: Add your control notification handler code here
unsigned int tenup,tenlow,oneup,onelow;
int m,t,i;
unsigned int *rcvval;
unsigned int rcv[100];
tenup = shiyuan/10; //應找10元面值數量的10位數
tenlow = shiyuan%10; //應找10元面值數量的個位數
oneup = yiyuan/10; //應找1元面值數量的10位數
onelow = yiyuan%10; //應找1元面值數量的個位數
if(m_flag == 1)
{//如果可以時行找零,則構造找零對象,并賦初值
m_flag = 0;
charge chargemoney(&m_comm, tenup,tenlow,oneup,onelow);
chargemoney.send(); //發送數據進行找零
m = chargemoney.recieve(); //m為第一個有效位的位置
t = chargemoney.getnumber(); //t為接收到的數組的總個數
rcvval = chargemoney.get(); //接收由找零機返回的數據
rcv[0] = 0;
for(i=0;i<t-m;i++)
rcv[i+1] = *(rcvval+i);
rcv[i]='\0';
updatebase(rcv); //對數據庫進行更新
isright(rest,rcv); //通過找零機返回的數據,進行判斷
}
else
MessageBox("無法找零");
/////////////////////////////////////////////
m_code.SetWindowText("");
m_crest.SetWindowText("");
m_consume.SetWindowText("");
m_putmoney .SetWindowText("");
m_rest=0;
m_code.SetFocus();
}
//根據找零機中10元和1元面值的數量對需要找出的各面值的數量進行調整
//a為找零機中10元面值的數量b為找零機中1元面值的數量
//c為應當找出的10元面值的數量d為應當找出的1元面值的數量
//mm_flag為m_flag的一個引用
void CAutoChangeMoneyDlg::is(unsigned int &a, unsigned int &b, unsigned int &c, unsigned int &d,BOOL &mm_flag)
{
if(b<d) //如果1元面值不足,則不進行找零
mm_flag = 0;
else
if(a>=c) //如果1元和10元面值足夠,則直接進行找零
mm_flag = 1;
else
if((c-a)*10+d<=b) //如果10元面值不足,1元面值足夠則調整找出10元和1元的數量進行找零
{
d = (c-a)*10+d;
c = a;
mm_flag = 1;
}
else
mm_flag = 0;
}
void CAutoChangeMoneyDlg::isright(int prest,unsigned int rcv5[])
{
int outmoney;
CString showout;
outmoney = (rcv5[7]-48)*100+(rcv5[8]-48)*10+(rcv5[11]-48)*10+(rcv5[12]-48);
showout.Format("找出%d元人民幣,找零不成功",outmoney);
if(outmoney == prest)
MessageBox("找零成功");
else
MessageBox(showout);
if(rcv5[14] == 48)
MessageBox("Status of Upper Cassette is abnormal");
if(rcv5[15] == 48)
MessageBox("Status of Lower Cassette is abnormal");
switch(rcv5[13])
{
// case 48:
// MessageBox("good");
// break;
// case 49:
// MessageBox("Normal stop");
// break;
case 50:
MessageBox("Pickup error");
break;
case 51:
MessageBox("JAM at CHK1,2 Sensor");
break;
case 52:
MessageBox("Overflow bill");
break;
case 53:
MessageBox("JAM at EXIT Sensor or EJT Sensor");
break;
case 54:
MessageBox("JAM at DIV Sensor");
break;
case 55:
MessageBox("Undefined command");
break;
case 56:
MessageBox("Upper Bill- End");
break;
case 58:
MessageBox("Counting Error (between CHK3,4 Sensor and DIV Sensor)");
break;
case 59:
MessageBox("Note request error");
break;
case 60:
MessageBox("Counting Error (between DIV Sensor and EJT Sensor)");
break;
case 61:
MessageBox("Counting Error (between EJT Sensor and EXIT Sensor)");
break;
case 63:
MessageBox("Reject Tray is not recognized");
break;
case 64:
MessageBox("Lower Bill-End");
break;
case 65:
MessageBox("Motor Stop");
break;
case 66:
MessageBox("JAM at Div Sensor");
break;
case 67:
MessageBox("Timeout (From DIV Sensor to EJT Sensor)");
case 68:
MessageBox("Over Reject");
break;
case 69:
MessageBox("Upper Cassette is not recognized");
break;
case 70:
MessageBox("Lower Cassette is not recognized");
break;
case 71:
MessageBox("Dispensing timeout");
break;
case 72:
MessageBox("JAM at EJT Sensor");
break;
case 73:
MessageBox("Diverter solenoid or SOL Sensor error");
break;
case 74:
MessageBox("SOL Sensor error");
break;
case 76:
MessageBox("JAM at CHK3,4 Sensor");
break;
case 78:
MessageBox("Purge error (Jam at Div Sensor)");
break;
default :
break;
}
///////////////////////////////////////////////////////////////
// 根據通信協議加入代碼判斷
///////////////////////////////////////////////////////////////
}
//數據庫更新,對找零機中各面值所剩數量進行更新,并把找零結果寫進數據庫
void CAutoChangeMoneyDlg::updatebase(unsigned int rcv2[])
{
int tenmoney,onemoney,out10,out01;
tenmoney = (rcv2[5]-48)*10+(rcv2[6]-48); //從錢柜中打出的10元面值的數量
onemoney = (rcv2[9]-48)*10+(rcv2[10]-48); //從錢柜中打出的1元面值的數量
out10 = (rcv2[7]-48)*10+(rcv2[8]-48);
out01 = (rcv2[11]-48)*10+(rcv2[12]-48);
CoInitialize(NULL);
_ConnectionPtr pConn(__uuidof(Connection));
_RecordsetPtr pRst(__uuidof(Recordset));
_CommandPtr pCmd(__uuidof(Command));
pConn->ConnectionString="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=change;Data Source=XL-157342083604";
pConn->Open("","","",adConnectUnspecified);//同步連接數據庫
pRst->raw_Close();
CString str1[5];
str1[0].Format("update change set onecount= onecount-'%d'",onemoney);
str1[1].Format("update change set tencount= tencount-'%d'",tenmoney);
str1[2].Format("update change set tenout= '%d' where id = '%s'",out10,str);
str1[3].Format("update change set oneout= '%d' where id = '%s'",out01,str);
pRst->Open((_variant_t)str1[0],_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);
pRst->Open((_variant_t)str1[1],_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);
pRst->Open((_variant_t)str1[2],_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);
pRst->Open((_variant_t)str1[3],_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);
// if(shiyuan!=0||yiyuan!=0)
// {
// str1[4].Format("update change set flag= 0 where id = '%s'",str);
// pRst->Open((_variant_t)str1[4],_variant_t((IDispatch*)pConn),adOpenDynamic,adLockOptimistic,adCmdText);
// }
// CoUninitialize();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -