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

📄 scommtestdlg.cpp

📁 用串口按约定格式对不同设备配置参数,可以自选端口,波特率,有定时发送功能
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE); //读取编辑框内容
}

//DEL void CSCommTestDlg::OnEditchangePort() 
//DEL {
//DEL 	// TODO: Add your control notification handler code here
//DEL     UpdateData(FALSE); //读取编辑框内容
//DEL 	//m_sPORT.Format("The count was %d.",comport);
//DEL 	m_sPORT.Format("%10d",comport);
//DEL //	switch (m_sPORT){
//DEL 	 // case 1: 
//DEL 	//	  comport=1; break;
//DEL     //  case 2:
//DEL 	//	  comport=2; break;
//DEL 	 // default:printf("error\n");  
//DEL //	}
//DEL 	comint();
//DEL }

void CSCommTestDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	
	CDialog::OnClose();
}

void CSCommTestDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	OnButtonManualsend();
    CDialog::OnTimer(nIDEvent);
}

void CSCommTestDlg::OnCheckAutosend() 
{
	// TODO: Add your control notification handler code here
   m_bAutoSend=!m_bAutoSend;
   timerinit(0);
}


void CSCommTestDlg::comint()
{
  if(m_ctrlComm.GetPortOpen())
    m_ctrlComm.SetPortOpen(FALSE);

  if (m_comboxport.GetLBTextLen(m_comboxport.GetCurSel())>0)
    m_ctrlComm.SetCommPort(m_comboxport.GetCurSel()+1);
  else
	m_ctrlComm.SetCommPort(1);

  //m_ctrlComm.SetCommPort(comport); //选择com口
  if( !m_ctrlComm.GetPortOpen())
    m_ctrlComm.SetPortOpen(TRUE);//打开串口
  else
    AfxMessageBox("cannot open serial port");

  m_ctrlComm.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位 
  m_ctrlComm.SetInputMode(1); //1:表示以二进制方式检取数据
  m_ctrlComm.SetRThreshold(1); 
  //参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
  m_ctrlComm.SetInputLen(0); //设置当前接收区数据长度为0
  m_ctrlComm.GetInput();//先预读缓冲区以清除残留数据
}

void CSCommTestDlg::OnSelchangePort() 
{
	// TODO: Add your control notification handler code here
	comint();
}

void CSCommTestDlg::OnChangeEdittime() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
       UpdateData(TRUE); //读取编辑框内容
	   timerinit(atoi(m_sTime.GetBuffer(0)));
}

void CSCommTestDlg::timerinit(UINT settime)
{
	if(m_bAutoSend)
	{
		if(settime>0)
			settimeinit = settime;
			KillTimer(1);
			//SetTimer(1,1000,NULL);//时间为1000毫秒
			SetTimer(1,settimeinit,NULL);
	}
	else
	{
		KillTimer(1);  //取消定时
	}
}

void CSCommTestDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	comint();
}

void CSCommTestDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

void CSCommTestDlg::OnEditchangeBaud() 
{
	// TODO: Add your control notification handler code here
	
}

void CSCommTestDlg::OnEditchangeCombo1() 
{
	// TODO: Add your control notification handler code here
	
}


void CSCommTestDlg::OnEditchangeKindCombo() 
{
	// TODO: Add your control notification handler code here
	
}

void CSCommTestDlg::OnChangeIdEdit() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here

	UpdateData(TRUE);
}

void CSCommTestDlg::OnSelchangeKindCombo() 
{
	// TODO: Add your control notification handler code here
	if(m_KINDCOMBO.GetCurSel()==0)
	{
		
        m_IDEDIT=2;
		m_TEMPEDIT=30;
		m_HUMIEDIT=0;
		m_WINDEDIT=0;
		m_CHANGEEDIT=1;
		UpdateData(FALSE);
	}
	else if(m_KINDCOMBO.GetCurSel()==1)
	{
		m_IDEDIT=3;
		m_TEMPEDIT=0;
	    m_HUMIEDIT=0;
		m_WINDEDIT=16;
		m_CHANGEEDIT=1;
		UpdateData(FALSE);
	}
    
	else if(m_KINDCOMBO.GetCurSel()==2)
	{
		m_IDEDIT=4;
		m_TEMPEDIT=38;
		m_HUMIEDIT=80;
		m_WINDEDIT=0;
		m_CHANGEEDIT=1;
		UpdateData(FALSE);

}
}

void CSCommTestDlg::OnChangeWindEdit() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here

		UpdateData(TRUE);
}

void CSCommTestDlg::OnChangeTempEdit() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here

	UpdateData(TRUE);
}

void CSCommTestDlg::OnChangeHumiEdit() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
}

void CSCommTestDlg::OnChangeChangeEdit() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CDialog::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
}



void CSCommTestDlg::OnResetButton() 
{
	// TODO: Add your control notification handler code here
        m_IDEDIT=0;
		m_TEMPEDIT=0;
		m_HUMIEDIT=0;
		m_WINDEDIT=0;
		m_CHANGEEDIT=0;
		UpdateData(FALSE);
}


void CSCommTestDlg::OnYesButton() 
{
	// TODO: Add your control notification handler code here
	CString id;
  CString wind;
  CString temp;
  CString humi;
  CString change;
  
  id.Format("%d",m_IDEDIT);
    m_strTXData += "*";
   	m_strTXData += id;
   	m_strTXData += ' ';
  wind.Format("%d",m_WINDEDIT);
   	m_strTXData += wind;
	m_strTXData += ' ';
  temp.Format("%d",m_TEMPEDIT);
   	m_strTXData += temp;
	m_strTXData += ' ';
  humi.Format("%d",m_HUMIEDIT);
  m_strTXData += humi;
  m_strTXData += ' ';
  change.Format("%d",m_CHANGEEDIT);
   	m_strTXData += change;
	m_strTXData += ' ';
    UpdateData(FALSE); 
	m_ctrlComm.SetOutput(COleVariant(m_strTXData));//发送数据
}

void CSCommTestDlg::OnSelchangeBaud() 
{
	// TODO: Add your control notification handler code here
	
  if(m_ctrlComm.GetPortOpen())
    m_ctrlComm.SetPortOpen(FALSE);

 if(m_sBaud.GetCurSel()==0)
	{	
		m_ctrlComm.SetSettings("4800,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位 
		UpdateData(FALSE);
	}
 else if(m_sBaud.GetCurSel()==1)
	{	
        m_ctrlComm.SetSettings("9600,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位 
		UpdateData(FALSE);
	}
 else if(m_sBaud.GetCurSel()==2)
	{	
        m_ctrlComm.SetSettings("19200,n,8,1"); //波特率9600,无校验,8个数据位,1个停止位 
		UpdateData(FALSE);
	}
 comint();
}

⌨️ 快捷键说明

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