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

📄 datasvr.cpp

📁 一个串口通信的程序
💻 CPP
字号:
// DataSvr.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "DataSvr.h"

#include "MainFrm.h"
#include "DataSvrDoc.h"
#include "DataSvrView.h"
#include <locale.h>
#include "Splash.h"
#include "glovar.h"
#include "inifile.h"

//#include "ntservice.h"
#include "service.h"
#include "HistoryForm.h"

#include "lsock.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDataSvrApp

BEGIN_MESSAGE_MAP(CDataSvrApp, CWinApp)
	//{{AFX_MSG_MAP(CDataSvrApp)
	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()

/////////////////////////////////////////////////////////////////////////////
// CDataSvrApp construction

CDataSvrApp::CDataSvrApp()
//	: CNTService(TEXT("DataSvrService"), TEXT("DataSvrService"))
//	, m_hStop(0)
{
	m_bFirstInstance = TRUE;	// 假定为第一个实列
	m_bShutDown = FALSE;
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDataSvrApp object

CDataSvrApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDataSvrApp initialization
UINT MyThreadProc( LPVOID pParam )
{
//	theApp.RegisterService(__argc, __argv);
    return 0;   // thread completed successfully
}

CLSock *m_LSock;

BOOL CDataSvrApp::InitInstance()
{
	if (!AfxSocketInit())
	{
		AfxMessageBox("Windows sockets initialization failed.");
		return FALSE;
	}
//	if( SV_ServiceRegister( REGISTER_SERVICE ) ) {
//		SV_StartService( REGISTER_SERVICE );
//	}
//	AfxBeginThread(MyThreadProc, 0);
	// 如果不是第一个实例, 则直接返回
	if( IsFirstInstance() == FALSE )
		return FALSE;

	TCHAR exeFullPath[MAX_PATH]; 
	DWORD R=GetLastError();
	GetModuleFileName(NULL,exeFullPath,MAX_PATH);
	CString strpath;
	strpath=exeFullPath;
	int lasti=-1;
	while(1)
	{
		int found=strpath.Find('\\',lasti+1);
		if(found==-1)break;
		lasti=found;
	}
	SYS_PATH=strpath.Left(lasti+1);

	// CG: The following block was added by the Splash Screen component.	{		CCommandLineInfo cmdInfo;		ParseCommandLine(cmdInfo);		CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);	}
	// CG: The following block was inserted by 'Status Bar' component.
	{
		//Set up date and time defaults so they're the same as system defaults
		setlocale(LC_ALL, "");
	}

	AfxEnableControlContainer();

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

	SetRegistryKey(_T("DATASVR_EXE"));

	LoadOptions();

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CDataSvrDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CDataSvrView));
	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_SHOW);
	m_pMainWnd->UpdateWindow();


	m_LSock=new CLSock;
	int m_nPort=24321;
	m_LSock->Create(m_nPort);
	m_LSock->Listen();

	// 设置主窗口标题
	CString strCaption;
	strCaption="动力量系统数据采集服务程序";
	m_pMainWnd->SetWindowText( (LPCTSTR)strCaption );

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// 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)
		// No message handlers
	//}}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()

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

/////////////////////////////////////////////////////////////////////////////
// CDataSvrApp message handlers


BOOL CDataSvrApp::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following lines were added by the Splash Screen component.	if (CSplashWnd::PreTranslateAppMessage(pMsg))		return TRUE;
	CMainFrame *pFrame = (CMainFrame*)AfxGetMainWnd();
	  if (pFrame) {
		if (pFrame->m_Framework.ProcessMessage(pMsg))
		  return TRUE;
	  }
	return CWinApp::PreTranslateMessage(pMsg);
}

int CDataSvrApp::ExitInstance() 
{
	// 只有第一个实列做一些清除工作
	if( m_bFirstInstance ) {
		SaveOptions();
		EndRT();		
		deleteyb();		
		deletecd();		
		deletecs();		
		deletert();	
//		this->Stop();
	}
	return CWinApp::ExitInstance();
}

BOOL CDataSvrApp::IsFirstInstance()
{
	// 搜索 lpszClassName == gcszWindowClass(="BaoSight_DataSvrEx") 的应用实例
	CWnd* pwndFirst = CWnd::FindWindow((LPCSTR)gcszWindowClass, (LPCSTR)NULL );

	if( pwndFirst ) {
		// 其他实例已经在运行 - 激活它
		CWnd* pwndPopup = pwndFirst->GetLastActivePopup();								   
		pwndFirst->SetForegroundWindow();
		if( pwndFirst->IsIconic() )
			pwndFirst->ShowWindow( SW_SHOWNORMAL );
		if( pwndFirst != pwndPopup )
			pwndPopup->SetForegroundWindow();

		m_bFirstInstance = FALSE;			
	}
	else
		// 当前为应用的第一个实例
		m_bFirstInstance = TRUE;

	return m_bFirstInstance;

}

void CDataSvrApp::SaveOptions()
{
	CIniFile inifile;
	inifile.SetPath(SYS_PATH+"datasvr.ini");
	inifile.SetValueI("DATASVR","AUTORUN",m_autoRun);
	inifile.WriteFile();
	inifile.Reset();
	if(m_autoRun==0)
	{
//		this->RemoveService();
		SV_StartService( UNREGISTER_SERVICE );
	}
	else if(m_autoRun==1)
	{
//		this->InstallService();
		SV_StartService( REGISTER_SERVICE );
	}
	else
	{
		if(m_bShutDown=true)
		{
//			this->InstallService();
			SV_StartService( REGISTER_SERVICE );
		}
		else
		{
//			this->InstallService();
			SV_StartService( UNREGISTER_SERVICE );
		}
	}
}

void CDataSvrApp::LoadOptions()
{
	CIniFile inifile;
	inifile.SetPath(SYS_PATH+"datasvr.ini");
	if(inifile.ReadFile())
	{
		m_autoRun=inifile.GetValueI("DATASVR","AUTORUN");
	}
	else
	{
		m_autoRun=2;
		inifile.SetValueI("DATASVR","AUTORUN",m_autoRun);
		inifile.WriteFile();
	}
	inifile.Reset();
	readyb();
	readcd();
	readcs();
	readrt();
	if(!BeginRT())
	{
		AfxMessageBox("BeginRT 错误!");
	}
}
/*
void CDataSvrApp::Run( DWORD s, LPTSTR * d) 
{
	// report to the SCM that we're about to start
	ReportStatus(SERVICE_START_PENDING);
	
	m_hStop = ::CreateEvent(0, TRUE, FALSE, 0);

	// You might do some more initialization here.
	// Parameter processing for instance ...
	
	// report SERVICE_RUNNING immediately before you enter the main-loop
	// DON'T FORGET THIS!
	ReportStatus(SERVICE_RUNNING);

	// enter main-loop
	// If the Stop() method sets the event, then we will break out of
	// this loop.
	while( ::WaitForSingleObject(m_hStop, 10) != WAIT_OBJECT_0 ) {
		// popup a small message box every 10 seconds
//		::MessageBox(0, TEXT("Hi, here is your very first MFC based NT-service"), TEXT("MFC SampleService"), MB_OK);
		::Sleep( 10000 );
	}

	if( m_hStop )
		::CloseHandle(m_hStop);
}
void CDataSvrApp:: Stop() {
	// report to the SCM that we're about to stop
	// Note that the service might Sleep(), so we have to tell
	// the SCM
	//	"The next operation may take me up to 11 seconds. Please be patient."
	ReportStatus(SERVICE_STOP_PENDING, 11000);

	if( m_hStop )
		::SetEvent(m_hStop);
}
*/

⌨️ 快捷键说明

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