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

📄 autochangemoneydlg.cpp

📁 使用VC++编写的自动找币机的控制程序。 有sql2000数据库
💻 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 + -