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

📄 config.cpp

📁 多文档界面设计配置文件读写串口操作串口封装类
💻 CPP
字号:
// Config.cpp : implementation file
//

#include "stdafx.h"
#include "SerialSun.h"
#include "Config.h"

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

/////////////////////////////////////////////////////////////////////////////
// CConfig dialog


CConfig::CConfig(CWnd* pParent /*=NULL*/)
	: CDialog(CConfig::IDD, pParent)
{
	//{{AFX_DATA_INIT(CConfig)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


void CConfig::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CConfig)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CConfig, CDialog)
	//{{AFX_MSG_MAP(CConfig)
	ON_BN_CLICKED(IDC_READCFPARITY, OnReadcfparity)
	ON_BN_CLICKED(IDC_WRITECF, OnWritecf)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CConfig message handlers

void CConfig::OnReadcfparity() 
{
	// TODO: Add your control notification handler code here
	CString  FilePath;
	FilePath+=".\\Config.ini";

	CString strSection       = "Section";
	CString strSectionKey    = "COM_PORT";
	CString strValue       = _T("");
	char inBuf[80];
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	SetDlgItemText(IDC_PORT,strValue);

	strSectionKey    = "COM_BAUDRATE";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	int num=atoi(inBuf);
	SetDlgItemInt(IDC_BAUDRATE,num,true);

	strSectionKey    = "COM_BITSIZE";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	SetDlgItemText(IDC_BITSIZE,strValue);

	strSectionKey    = "COM_STOPBIT";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	SetDlgItemText(IDC_STOPBIT,strValue);

	strSectionKey    = "COM_PARITY";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	SetDlgItemText(IDC_PARITY,strValue);

	strSectionKey    = "COM_TIMER";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	SetDlgItemText(IDC_TIMER,strValue);

	strSectionKey    = "RTUID";
    strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	SetDlgItemText(IDC_RTUID,strValue);
}

void CConfig::OnWritecf() 
{
	// TODO: Add your control notification handler code here
	writeconfig();
}
// 读配置文件
void CConfig::readconfig(void)
{
	CString  FilePath;
	FilePath+=".\\Config.ini";

	CString strSection       = "Section";
	CString strSectionKey    = "COM_PORT";
	CString strValue       = _T("");
	char inBuf[80];
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
    n_PORT = atoi(inBuf);

	strSectionKey    = "COM_BAUDRATE";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	int num=atoi(inBuf);
	n_BAUDRATE = atoi(inBuf);

	strSectionKey    = "COM_BITSIZE";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	n_BITSIZE = atoi(inBuf);

	strSectionKey    = "COM_STOPBIT";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	n_STOPBIT = atoi(inBuf);

	strSectionKey    = "COM_PARITY";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	n_PARITY = *inBuf;

	strSectionKey    = "COM_TIMER";
	strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	n_TIMER = atoi(inBuf);

	strSectionKey    = "RTUID";
    strValue       = _T("");
	GetPrivateProfileString (strSection,strSectionKey, NULL, inBuf, 80, FilePath); 
	strValue=inBuf;
	n_RTUID = inBuf;

}

// 写配置文件
void CConfig::writeconfig(void)
{
	char appPath[256];
	GetCurrentDirectory(256,appPath); //取得应用程序当前路径
	CString  FilePath;
	FilePath.Format("%s",appPath);
	FilePath+="\\Config.ini";
	
	CString strSection       = "Section";
    CString strSectionKey    = "COM_PORT";
    CString strValue       = _T("");
	GetDlgItemText(IDC_PORT,strValue);
	WritePrivateProfileString (strSection,strSectionKey,  
        strValue, FilePath); 



	strSectionKey    = "COM_BAUDRATE";
    strValue       = _T("");
	GetDlgItemText(IDC_BAUDRATE,strValue);
	WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath); 


	strSectionKey    = "COM_BITSIZE";
    strValue       = _T("");
	GetDlgItemText(IDC_BITSIZE,strValue);
	WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath); 

	strSectionKey    = "COM_STOPBIT";
    strValue       = _T("");
	GetDlgItemText(IDC_STOPBIT,strValue);
	WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath); 

	strSectionKey    = "COM_PARITY";
    strValue       = _T("");
	GetDlgItemText(IDC_PARITY,strValue);
	WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath); 


	strSectionKey    = "COM_TIMER";
    strValue       = _T("");
	GetDlgItemText(IDC_TIMER,strValue);
	WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath); 
	
	strSectionKey    = "RTUID";
    strValue       = _T("");
	GetDlgItemText(IDC_RTUID,strValue);
	WritePrivateProfileString (strSection,strSectionKey, 
        strValue, FilePath); 

}

⌨️ 快捷键说明

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