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

📄 portset.cpp

📁 本人用VC6.0开发的单片机通过串口与PC机通信的上位机,通信协议在说明文档中已经写出,用户在单片机上编写串口函数时按照通信协议,便可以很方便通过此软件与PC机通信.
💻 CPP
字号:
// PortSet.cpp : implementation file
//

#include "stdafx.h"
#include "OxcygenTest.h"
#include "PortSet.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPortSet dialog


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


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


BEGIN_MESSAGE_MAP(CPortSet, CDialog)
	//{{AFX_MSG_MAP(CPortSet)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPortSet message handlers

void CPortSet::OnOK() 
{
	// TODO: Add extra validation here
	CString str1,str2,str3,str4,str5,str0;

	CFile file("comset.ini",CFile::modeCreate|CFile::modeWrite);
	CFile file_slaveadd("slaveadd.ini",CFile::modeCreate|CFile::modeWrite);
	CFile file_startch("startch.ini",CFile::modeCreate|CFile::modeWrite);
	
	GetDlgItemText(IDC_EDIT_START_CHAR,str0);
	file_startch.Write(str0,strlen(str0));

	GetDlgItemText(IDC_EDIT_SLAVE_ADD,str0);
	file_slaveadd.Write(str0,strlen(str0));

    GetDlgItemText(IDC_COMBO_PORT,str0);
    if(str0=="串口1")   str1="0";
	if(str0=="串口2")   str1="1";
	if(str0=="串口3")   str1="2";
    if(str0=="串口4")   str1="3";
	if(str0=="串口5")   str1="4";
    if(str0=="串口6")   str1="5";
    if(str0=="串口7")   str1="6";
    if(str0=="串口8")   str1="7";
    if(str0=="串口9")   str1="8";
    if(str0=="串口10")  str1="9";
	file.Write(str1,strlen(str1));

	//-----------------------------------------------------------------
    GetDlgItemText(IDC_COMBO_BOADRATE,str0);
    if(str0=="19200")   str2="0";
	if(str0=="14400")   str2="1";
	if(str0=="9600")    str2="2";
    if(str0=="4800")    str2="3";
	if(str0=="2400")    str2="4";
	if(str0=="1800")    str2="5";
	file.Write(str2,strlen(str2));

 //---------------------------------------------------------------------
	GetDlgItemText(IDC_COMBO_CHECK,str0);
	if(str0=="奇校验")   str3="0";
	if(str0=="偶校验")   str3="1";
	if(str0=="无校验")   str3="2";
	file.Write(str3,strlen(str3));

//----------------------------------------------------------------------
	GetDlgItemText(IDC_COMBO_DATANUM,str0);
	if(str0=="9位")    str4="0";
	if(str0=="8位")    str4="1";
	if(str0=="7位")    str4="2";
    if(str0=="6位")    str4="3";
	if(str0=="5位")    str4="4";
	if(str0=="4位")    str4="5";  
	file.Write(str4,strlen(str4));

//-----------------------------------------------------------------------
	GetDlgItemText(IDC_COMBO_STOP,str0);
	if(str0=="1.5位")  str5="0";
	if(str0=="1位")    str5="1"; 
	file.Write(str5,strlen(str5));
	file.Close();		
	CDialog::OnOK();
}

BOOL CPortSet::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COMBO_BOADRATE))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COMBO_CHECK))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COMBO_DATANUM))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COMBO_STOP))->SetCurSel(0);

    CString n;  
    CString p[24];   
    CFile file("comset.ini",CFile::modeRead);
	char *pBuf;
	DWORD dwFileLen;
	dwFileLen=file.GetLength();
	pBuf=new char[dwFileLen+1];   
	pBuf[dwFileLen]=0;
	file.Read(pBuf,dwFileLen);
	file.Close();
    
    n=pBuf;
    p[0]=n.Mid(0,1);   
    p[1]=n.Mid(1,1); 
    p[2]=n.Mid(2,1);   
    p[3]=n.Mid(3,1); 
	p[4]=n.Mid(4,1);  
	
    ((CComboBox*)GetDlgItem(IDC_COMBO_PORT))->SetCurSel(atoi(p[0]));
	((CComboBox*)GetDlgItem(IDC_COMBO_BOADRATE))->SetCurSel(atoi(p[1]));
	((CComboBox*)GetDlgItem(IDC_COMBO_CHECK))->SetCurSel(atoi(p[2]));
	((CComboBox*)GetDlgItem(IDC_COMBO_DATANUM))->SetCurSel(atoi(p[3]));
	((CComboBox*)GetDlgItem(IDC_COMBO_STOP))->SetCurSel(atoi(p[4]));
	delete [] pBuf;

	//在从机地址编辑框显示数据
    CFile file_slaveadd("slaveadd.ini",CFile::modeRead);
	dwFileLen=file_slaveadd.GetLength();
	pBuf=new char[dwFileLen+1];   
	pBuf[dwFileLen]=0;
	file_slaveadd.Read(pBuf,dwFileLen);
	file_slaveadd.Close();   
    n=pBuf;
	GetDlgItem(IDC_EDIT_SLAVE_ADD)->SetWindowText(n);
	delete [] pBuf;
	
	//在起始字节编辑框显示数据
	CFile file_startch("startch.ini",CFile::modeRead);
	dwFileLen=file_startch.GetLength();
	pBuf=new char[dwFileLen+1];   
	pBuf[dwFileLen]=0;
	file_startch.Read(pBuf,dwFileLen);
	file_startch.Close();   
    n=pBuf;
	GetDlgItem(IDC_EDIT_START_CHAR)->SetWindowText(n);
	delete [] pBuf;
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CPortSet::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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