欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

commwizarddlg.cpp

实现网络+串口 红外接收、发送
CPP
第 1 页 / 共 5 页
字号:
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CCommWizardDlg::OnPaint() 
{

	CPaintDC dcb(this); // device context for painting
	CRect rcWnd;
	this->GetClientRect(&rcWnd);
	CBrush brush;
	brush.CreateSolidBrush(RGB(210,213,132));
	//dc.SetMapMode(TRANSPARENT);
	dcb.FillRect(&rcWnd,&brush);
	brush.DeleteObject();



	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CCommWizardDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CCommWizardDlg::OnReceive() 
{
	////m_Com.GetInput();//先预读缓冲区以清除残留数据
	bReceive = !bReceive;
	if(bReceive)
		m_ctrlReceive.SetWindowText(_T("停止接收"));
	else
		m_ctrlReceive.SetWindowText(_T("开始接收"));
}

void CCommWizardDlg::OnSend() 
{

	bSend = !bSend;
	if(bSend)
	{
		m_ctrlSend.SetWindowText(_T("停止发送"));
		SetTimer(1,m_nTime,NULL);//时间为1000毫秒
	}
	else
	{
		m_ctrlSend.SetWindowText(_T("自动发送"));
		KillTimer(1);  //取消定时
	}	
}

void CCommWizardDlg::OnSettings() 
{

	CSettingDlg setDlg;
	
	setDlg.nPort = m_nPort;
	setDlg.nTime = m_nTime;
	setDlg.strSettings = m_strSettings;
	setDlg.strSendString = m_strSendString;

	if(setDlg.DoModal() == IDOK)
	{
		m_nPort = setDlg.nPort;
		m_nTime = setDlg.nTime;
		m_strSettings = setDlg.strSettings;
		m_strSendString = setDlg.strSendString;
	}


	//m_bSerialSet  = true;//串口参数设置标置
	m_bSerialSet = !m_bSerialSet;
	if(m_bSerialSet)
	{
		m_Com.SetCommPort(m_nPort);       //选择com1
		if(m_Com.GetPortOpen())
		   m_Com.SetPortOpen(false);//关闭串口

		m_btSerSet.SetWindowText(_T("串口设置"));
	}
	else
	{
	    OpenPort();  //2008.06.24 ZQL Modified
		m_btSerSet.SetWindowText(_T("关闭串口"));
	}
}

void CCommWizardDlg::OnSelchangeType() 
{
	CString str;
	m_nInputMode = m_ctrlDataType.GetCurSel();
	str.Format("%d",m_nInputMode);
	::WritePrivateProfileString("COM","PORT_MODE", str,	".\\irda.ini");

}

void CCommWizardDlg::OnClear() 
{
	// TODO: Add your control notification handler code here
	m_strReceive.Empty();// = _T("");
//	m_sRev.SetLimitText(0);
//	m_sRev.SetSel(0,-1);
//	m_sRev.Clear();

	m_sRecStr.Empty();
	UpdateData(FALSE); 
}

BEGIN_EVENTSINK_MAP(CCommWizardDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CCommWizardDlg)
	ON_EVENT(CCommWizardDlg, IDC_COMMCTRL, 1 /* OnComm */, OnOnCommCommctrl, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CCommWizardDlg::OnOnCommCommctrl() 
{
	VARIANT variant_inp;
	COleSafeArray safearray_inp;
	LONG len,k;
	BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
	CString strtemp;
	CString sIrda;
	if(bReceive)
	{
		bReceived = true;//false;//未接收完
		if(m_Com.GetCommEvent()==2)     //事件值为2表示接收缓冲区内有字符
		{//m_sRev.SetSel(0,-1);
			variant_inp = m_Com.GetInput();   //读缓冲区
			safearray_inp = variant_inp;           //VARIANT型变量转换为ColeSafeArray型变量
			len=safearray_inp.GetOneDimSize(); //得到有效数据长度
			for(k=0;k<len;k++)
				safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
			for(k=0;k<len;k++)             //将数组转换为Cstring型变量
			{
				BYTE bt=*(char*)(rxdata+k);      //字符型

				if(m_nInputMode == 2)
				{
					byTemp[nRec] = bt;//ZQL 2008.03.12
					nRec++;

					sIrda.Format("%02x ",bt);
					strtemp.Format("%02X ",bt); //将字符以十六进制方式送入临时变量strtemp存放,注意这里加入一个空隔
				}
				else 
					strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
				///m_strReceive = m_strReceive + strtemp;    //加入接收编辑框对应字符串   
				m_sRecStr = m_sRecStr + strtemp;
				sRecIrda = sRecIrda + sIrda;
			}
			if(nRec >= 6)
			{
				CString sts = _T("");

					if( nRec >= (unsigned char)byTemp[5] + 10 )
					{	
						bReceived=true;//接收完
						//nRec = 0;
					}
			}
			//m_strReceive += "\r\n接收:";
			m_sRecStr += "\r\n接收:";

			m_sRev.SetSel(0,-1);
			m_sRev.Clear();
			m_sRev.SetLimitText(0);
			m_sRev.ReplaceSel(m_sRecStr);
		}
		else
			bReceived=true;//接收完
	}


	//UpdateData(FALSE);           //更新编辑框内容	
}

void CCommWizardDlg::OpenPort()
{
	if(m_Com.GetPortOpen())
		m_Com.SetPortOpen(FALSE);

	m_Com.SetCommPort(m_nPort);       //选择com1
	if( !m_Com.GetPortOpen())
		m_Com.SetPortOpen(TRUE);//打开串口
	else
	{
		AfxMessageBox("cannot open serial port");
		return;
	}
	m_Com.SetSettings(m_strSettings); //波特率9600,无校验,8个数据位,1个停止位
	m_Com.SetRThreshold(1); 
	//参数1表示每当串口接收缓冲区中有多于或等于1个字符时将引发一个接收数据的OnComm事件
	m_Com.SetInputMode(1);
	m_Com.SetInputLen(0);  //设置当前接收区数据长度为0
	m_Com.GetInput();//先预读缓冲区以清除残留数据	
	
}

void CCommWizardDlg::OnManualsend() 
{
	// TODO: Add your control notification handler code here
	CString strSend;

	if(m_nInputMode > 1)
	{
		CByteArray hexdata;
		int len=String2Hex(m_strSendString ,hexdata);
		m_Com.SetOutput(COleVariant(hexdata));
	}
	else
		m_Com.SetOutput(COleVariant(m_strSendString));//发送数据
}

void CCommWizardDlg::OnTimer(UINT nIDEvent) 
{

	if(nIDEvent == 1)
	{
		OnManualsend();
	}
	if(nIDEvent == 2)
	{
		OnDataup();
	}
	if(nIDEvent == 3)
	{
		OnDatadown();
	}
	if(nIDEvent == 4)//查徇
	{
		StartLook();
	}
	if(nIDEvent == 6)
	{
		//timers ++;
		
		//if(m_bStudy == 1)
		//{
		//	m_bStudy =0;
			nRec = 0;//接收缓冲记数清除
			if(m_comSty.GetCurSel() > 0 && m_comSty.GetCurSel() <=16)
			{
				//AfxMessageBox("");
				if(byTemp[7] == 0x55)
				{
					for(int i=0;i<512;i++)
					{
						byTemp[i] = 0xAA;//
					}

					//if(m_bSerialSet)//网络传输时
					//{
						//if(m_bTreadRWok)//等待线程读写完成
						//	AfxBeginThread(Serial_ReadThread,&SocketPr);//读线程
					//}					
				}
				

				if(byTemp[7] == 0xff)
				{
					KillTimer(6);  //取消定时
					AfxMessageBox("学习失败!");
					
				}

				if(byTemp[7] == 0x00)
				{
					KillTimer(6);  //取消定时
					AfxMessageBox("学习成功!");   
					
				}
				

			}
			else if( m_comSty.GetCurSel() > 16)
			{
				if(byTemp[7] == 0xff)
				{
					KillTimer(6);  //取消定时
					AfxMessageBox("控制操作失败!");
				}
				if(byTemp[7] == 0x00)
				{
					KillTimer(6);  //取消定时
					AfxMessageBox("控制操作成功!");
				}
			}
		//}//读取返回:成功或失败

	}


	//if(timers >= 3)//nIDEvent == 50)
	if(nIDEvent == 5)
	{
		//KillTimer(5);
		//timers = 0;

	    CString sts;// = _T("");

		rem++;
		if(rem>=200)
		{
			rem=0;
			OnClear();
			
		}
		sRecIrda.Empty();


		m_edAlarmAStUp.Clear();
		m_edAlarmAStUp.SetSel(0,-1);
		m_edAlarmAStUp.SetLimitText(0);
		m_edAlarmAStUp.ReplaceSel("");

		m_edAlarmBStUp.Clear();
		m_edAlarmBStUp.SetSel(0,-1);
		m_edAlarmBStUp.SetLimitText(0);
		m_edAlarmBStUp.ReplaceSel("");

		m_edAlarmAStDown.Clear();
		m_edAlarmAStDown.SetSel(0,-1);
		m_edAlarmAStDown.SetLimitText(0);
		m_edAlarmAStDown.ReplaceSel("");

		m_edAlarmBStDown.Clear();
		m_edAlarmBStDown.SetSel(0,-1);
		m_edAlarmBStDown.SetLimitText(0);
		m_edAlarmBStDown.ReplaceSel("");


		if(m_ctrDataType.GetCurSel() == 1)//显示模拟数据
		{
			

			LTemp = 0;
			sts.Empty();// = _T("");
			
			LTemp =  byTemp[8];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[9];
			sts.Format("%d",LTemp);

			m_edRecA.Clear();
			m_edRecA.SetSel(0,-1);
			m_edRecA.SetLimitText(0);
			m_edRecA.ReplaceSel(sts);
			
			//AfxMessageBox(sts);
			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[10];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[11];
			sts.Format("%d",LTemp);

			m_edRecB.Clear();
			m_edRecB.SetSel(0,-1);
			m_edRecB.SetLimitText(0);
			m_edRecB.ReplaceSel(sts);


			if( (byTemp[12] & 0x01) == 0x01)
			{
				m_edAlarmAStUp.Clear();
				m_edAlarmAStUp.SetSel(0,-1);
				m_edAlarmAStUp.SetLimitText(0);
				m_edAlarmAStUp.ReplaceSel("上限告警!");
			}
			else
			{
				m_edAlarmAStUp.Clear();
				m_edAlarmAStUp.SetSel(0,-1);
				m_edAlarmAStUp.SetLimitText(0);
				m_edAlarmAStUp.ReplaceSel("  正常");
			}
			if( (byTemp[12] & 0x02) == 0x02)
			{
				m_edAlarmBStUp.Clear();
				m_edAlarmBStUp.SetSel(0,-1);
				m_edAlarmBStUp.SetLimitText(0);
				m_edAlarmBStUp.ReplaceSel("上限告警!");
			}
			else
			{
				m_edAlarmBStUp.Clear();
				m_edAlarmBStUp.SetSel(0,-1);
				m_edAlarmBStUp.SetLimitText(0);
				m_edAlarmBStUp.ReplaceSel("  正常");
			}
			if( (byTemp[12] & 0x04) == 0x04)
			{
				m_edAlarmAStDown.Clear();
				m_edAlarmAStDown.SetSel(0,-1);
				m_edAlarmAStDown.SetLimitText(0);
				m_edAlarmAStDown.ReplaceSel("下限告警!");
			}
			else
			{
				m_edAlarmAStDown.Clear();
				m_edAlarmAStDown.SetSel(0,-1);
				m_edAlarmAStDown.SetLimitText(0);
				m_edAlarmAStDown.ReplaceSel("  正常");
			}
			if( (byTemp[12] & 0x08) == 0x08)
			{
				m_edAlarmBStDown.Clear();
				m_edAlarmBStDown.SetSel(0,-1);
				m_edAlarmBStDown.SetLimitText(0);
				m_edAlarmBStDown.ReplaceSel("下限告警!");
			}
			else
			{
				m_edAlarmBStDown.Clear();
				m_edAlarmBStDown.SetSel(0,-1);
				m_edAlarmBStDown.SetLimitText(0);
				m_edAlarmBStDown.ReplaceSel("  正常");
			}
		
		}
		
		if(m_ctrDataType.GetCurSel() == 4)//显示告警参数
		{
			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[8];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[9];
			sts.Format("%d",LTemp);
			m_edMAUp.SetSel(0,-1);
			m_edMAUp.Clear();
			m_edMAUp.ReplaceSel(sts);		
					
			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[10];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[11];
			sts.Format("%d",LTemp);
			m_edMADown.SetSel(0,-1);
			m_edMADown.Clear();
			m_edMADown.ReplaceSel(sts);

			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[12];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[13];
			sts.Format("%d",LTemp);
			m_edMBUp.SetSel(0,-1);
			m_edMBUp.Clear();
			m_edMBUp.ReplaceSel(sts);
			
			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[14];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[15];
			sts.Format("%d",LTemp);
			m_edMBDown.SetSel(0,-1);
			m_edMBDown.Clear();
			m_edMBDown.ReplaceSel(sts);

			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[16];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[17];
			sts.Format("%d",LTemp);
			m_edAlarmAUp.SetSel(0,-1);
			m_edAlarmAUp.Clear();
			m_edAlarmAUp.ReplaceSel(sts);
			
			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[18];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[19];
			sts.Format("%d",LTemp);
			m_edAlarmBUp.SetSel(0,-1);
			m_edAlarmBUp.Clear();
			m_edAlarmBUp.ReplaceSel(sts);

			LTemp = 0;
			sts.Empty();//sts = _T("");

			LTemp =  byTemp[20];
			LTemp = LTemp<<8;
			LTemp = LTemp + byTemp[21];
			sts.Format("%d",LTemp);
			m_edAlarmADown.SetSel(0,-1);
			m_edAlarmADown.Clear();
			m_edAlarmADown.ReplaceSel(sts);
			
			LTemp = 0;
			sts.Empty();//sts = _T("");

⌨️ 快捷键说明

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