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

📄 oil.cpp

📁 本人工作中的一个软件开发实例。里面包含了数据库
💻 CPP
字号:
// OIL.cpp : Defines the class behaviors for the application.
//

#include "StdAfx.h"
#include "OIL.h"

#include "MainFrm.h"
#include "OILDoc.h"
#include "OILView.h"
#include "Login.h"
#include "SerialPort.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COILApp

BEGIN_MESSAGE_MAP(COILApp, CWinApp)
	//{{AFX_MSG_MAP(COILApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COILApp construction

COILApp::COILApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only COILApp object

COILApp   theApp;

/////////////////////////////////////////////////////////////////////////////
// COILApp initialization

BOOL COILApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	//只启动一个进程
	HANDLE m_hMutex=CreateMutex(NULL,TRUE,m_pszAppName);
	if(GetLastError()==ERROR_ALREADY_EXISTS) 
	{   
		AfxMessageBox("油库发油程序只能启动一次");
		return false;
	}

	//单文档模板
	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(COILDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(COILView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_MAXIMIZE);
	m_pMainWnd->UpdateWindow();

	//初始化com库,连接数据库
	if(AfxOleInit()==0)      //CoInitialize(NULL)!=0 Failure
	{
		AfxMessageBox("初始化COM库失败");
		return false;
	}

	//连接数据库
	try{		
		m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
		m_pConnection->ConnectionTimeout=30;///设置超时时间为30秒
		m_pConnection->Open("Provider=SQLOLEDB;Data Source=tw;Initial Catalog=MWYK;User Id=sa;Password=sa;","","",adModeUnknown);
    }
	catch(_com_error *e){
		AfxMessageBox(e->ErrorMessage());
		return FALSE;
	}
	catch(...)
	{
		AfxMessageBox("数据库连接错误");
		return false;
	}

	//用户登陆
	CLogin cLogin;
	int    iRet;
	iRet=cLogin.DoModal();
	if(iRet==IDCANCEL)
	{
		return false;
	}

	//创建监控界面
	m_pMonitor=new CMonitor;

	//初始化设备类型
    

	//初始化通信模式
	theGlobal.nMode=Terminal;
	
	//初始化串口参数


	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CListBox	m_ListBox;
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL
	CSerialPort		    *m_pSerialPorts;
// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual void OnOK();
	afx_msg void OnSend();
	virtual BOOL OnInitDialog();
    afx_msg LONG OnCommunication(WPARAM, LPARAM);
	afx_msg void OnDestroy();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	m_pSerialPorts=NULL;
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDC_LIST_DATA, m_ListBox);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void COILApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// COILApp message handlers


int COILApp::ExitInstance() 
{
	// TODO: Add your specialized code here and/or call the base class

	// 断开数据库的连接
	if(m_pConnection!=NULL&&m_pConnection->State)
		 m_pConnection->Close();
	m_pConnection=NULL;

	//放COM库
	::CoUninitialize();

	//删除监控界面
    if(m_pMonitor!=NULL)
          delete m_pMonitor;

	return CWinApp::ExitInstance();
}


//此函数用于加密
//pwd为密加密或解密的字符串
//in为true表示加密数据,为false为解密。
CString COILApp::Crypt(CString pwd, bool in)
{
    char temp[1000];
	if(in)
	{
		for(int i=0;i<pwd.GetLength();i++)
			temp[i]=pwd[i]-2-i;
		temp[i]='\0';
	}
	else
	{
		for(int i=0;i<pwd.GetLength();i++)
			temp[i]=pwd[i]+2+i;
		temp[i]='\0';
	}
	CString str;
	str.Format("%s",temp);
	return str;
}

void CAboutDlg::OnOK() 
{
	// TODO: Add extra validation here

	CDialog::OnOK();
}

void CAboutDlg::OnSend() 
{
	// TODO: Add your control notification handler code here
	char buf[100];
	memset(&buf, 0, sizeof(buf));
	GetDlgItemText(IDC_DATA, buf, sizeof(buf));
	m_pSerialPorts->WriteToPort(buf);
}

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	if(m_pSerialPorts==NULL)
	{
		m_pSerialPorts=new CSerialPort;
		m_pSerialPorts->InitPort(this,2,9600);
		m_pSerialPorts->StartMonitoring();
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

LONG CAboutDlg::OnCommunication(WPARAM ch, LPARAM port)
{
	//收到字符
		CString RcvStr;
		RcvStr += (char)ch;
		m_ListBox.AddString(RcvStr);
	//回发数据
		if(ch=='*')
		{
		   char    szSend[50];
		   strncpy(szSend,RcvStr,RcvStr.GetLength());
		   TRACE("回发数据%s",szSend);
           m_pSerialPorts->WriteToPort(szSend);
		   memset(szSend,0,50);
		}
	return 0;
}

void CAboutDlg::OnDestroy() 
{
	CDialog::OnDestroy();

	// TODO: Add your message handler code here
	m_pSerialPorts->ExitMonitoring();
    delete m_pSerialPorts;
	m_pSerialPorts=NULL;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -