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

📄 comm2410dlg.cpp

📁 wince下用evc写的一个串口通讯的程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		 UpdateData(FALSE);
	 }
	 GetDlgItem(IDC_BUTTON_STATUS)->SetWindowText(_T("关闭串口"));
}

void CComm2410Dlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	if  (hPort == NULL) 
	{
		AfxMessageBox(_T("还没有串口被打开!"));
		return;
	}

    if (!StopSend)
	{
		KillTimer(3);
		KillTimer(1);
		GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(TRUE);
		GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("发送"));
		StopSend=TRUE;
		return;
    }
	sendbyte=0;
	UpdateData(TRUE);
	int iStart,iEnd;
	BYTE c;
	iStart=iEnd=0;
	if (m_iCharhex)   ///16进制
	{  
		Tempchar.RemoveAll();
		LPCWSTR lpstr = (LPCWSTR)m_iStarthex;
		swscanf(lpstr,_T("%x"),&iStart);
		lpstr = (LPCWSTR)m_iEndhex;
		swscanf(lpstr,_T("%x"),&iEnd);
		if (iStart<=iEnd)  //升序
		{
			for (int t=iStart;t<=iEnd;t++)
			{
				c =(BYTE)t;
				Tempchar.Add(c);
			}
		}
		else  //降序
		{
			for (int t=iStart;t>=iEnd;t--)
			{
				c =(BYTE)t;
				Tempchar.Add(c);
			}
		}
	}

	else ////字符
	{

		BYTE* pbyte = NULL;
		//Tempchar.SetSize(m_senddata.GetLength());
		// memcpy(Tempchar.GetData(),m_senddata.GetBuffer(m_senddata.GetLength()),m_senddata.GetLength());
		int iSize = SvrConvertUni2Mbcs(m_senddata,pbyte);
		Tempchar.SetSize(iSize);
		memcpy(Tempchar.GetData(),pbyte,iSize);
		delete pbyte;
	}

	if (m_iForEver)     ///永久发送
	{
		GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(FALSE);
		//SetTimer(3,m_iInterval,0);
		StopSend=FALSE;
		GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("停止发送"));
	}
	else 
	{
		
		if (m_count >1)
		{
			//   GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(FALSE);  ///DISABLE 发送按钮
			usedtime=m_count;
			SetTimer(1,m_iInterval,0); 
			StopSend=FALSE;
			GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("停止发送"));  ///DISABLE 发送按钮
		}

		else     //只发送一次
		{
			GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("停止发送"));
			SendData();
			CString str;
			str.Format(_T("%d"),Tempchar.GetSize());
			//		 m_iSendCount=str;
			m_iSendCount.SetWindowText(str);
			StopSend=TRUE;
			GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("发送"));
			UpdateData(FALSE); 
		}
	}
	return ;	
}

void CComm2410Dlg::OnButtonClear() 
{
	// TODO: Add your control notification handler code here
	   UpdateData(TRUE);

       m_iReceiveData.SetWindowText(_T(""));
	   
	   receivebyte = 0;
	   m_iTemp.SetWindowText(_T("0"));
	   //	 m_iRecCount = _T("0  Byte");
	   UpdateData(FALSE);
}

void CComm2410Dlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CByteArray TempData;
	DWORD dwlen;
	CString strs,recstr,rectempstr;
	if (hPort ==NULL)
		return ;

	{
		//		UpdateData(TRUE); 
		TempData.SetSize(0);
		DWORD Err;
        COMSTAT  Comm;

		if (!ClearCommError(hPort,&Err,&Comm))
            return;

		if (Err != 0)
		{
			CString str;
			str.Format(_T("Err%d %d\n"),iErrCount ++,Err);
			GetDlgItem(IDC_STATIC123)->SetWindowText(str);
		}

		DWORD ReceQue;
		CByteArray bbb;
		//while (Comm.cbInQue > 0)
		if (Comm.cbInQue > 0)
		{

			ReceQue=0;
            TempData.SetSize(Comm.cbInQue);
            bbb.SetSize(0);
			if (!ReadFile(hPort,TempData.GetData(),
				Comm.cbInQue,
				&ReceQue,NULL))
			{
				GetLastError();
                //break;
			}
			else 
			{
				int size;
				size=bbb.GetSize();
				bbb.SetSize(size+ReceQue);
				memcpy(bbb.GetData()+size,TempData.GetData(),ReceQue);

				/*
				Sleep(10);
				if (!ClearCommError(hPort,&Err,&Comm))
				break;		
				*/
			}

			rectempstr=_T("");
			if (m_iCharhex)  ///16进制
			{	
				for (int i=0;i<bbb.GetSize();i++)
				{
					strs.Format(_T("%02x "),bbb[i]);
					rectempstr+=strs;			
				}
				m_iReceiveData.SetWindowText(rectempstr);    //显示16进制数
				receivebyte+=bbb.GetSize();
				recstr.Format(_T("%ld"),receivebyte);
				m_iTemp.SetWindowText(recstr);//显示接收到的16进制数个数
				strs.ReleaseBuffer(bbb.GetSize());

			}
			else  ///字符
			{
				
				CString tempstr;
				long nlength=MultiByteToWideChar(CP_ACP,0,(char *)bbb.GetData(),bbb.GetSize(),NULL,0);
				long nlengthwidth=MultiByteToWideChar(CP_ACP,0,(char *)bbb.GetData(),bbb.GetSize(),tempstr.GetBuffer(nlength),nlength);
				//  receivebyte+=bbb.GetSize();
				//			m_iReceiveData.Insert(m_iReceiveData.GetAt(0)+m_iReceiveData.GetLength(),(LPCTSTR)tempstr);
				//m_iReceiveData.SetWindowText(tempstr); ///显示接收到的字符
				//			m_iReceiveData=tempstr;

				receivebyte+=nlengthwidth;     ///显示接收到的字符数

				// m_iTemp = receivebyte;
				recstr.Format(_T("%ld"),receivebyte);

				m_iTemp.SetWindowText(recstr); //显示接收到的字符数个数
				tempstr.ReleaseBuffer(nlengthwidth);
			}

			//  UpdateData(FALSE);
		}///连续发时,每次要在这里回到WHILE语句
		// goon=FALSE;
	}
	if (nIDEvent == 1)
	{
		if (usedtime > 0 && StopSend == FALSE)
		{
			usedtime--;
			SendData();
			strs.Format(_T("%d"),sendbyte);
			m_iSendCount.SetWindowText(strs);   ///发送的字节数
			//		m_iSendCount=strs;
			//	UpdateData(FALSE); 
		}
		else
		{
			KillTimer(1);
			GetDlgItem(IDC_BUTTON_SEND)->EnableWindow(TRUE);
			GetDlgItem(IDC_BUTTON_SEND)->SetWindowText(_T("发送"));
			GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(TRUE);
			StopSend=TRUE;
			//AfxMessageBox(_T("数据已发送完毕"));
		}
	}

	CDialog::OnTimer(nIDEvent);
}

void CComm2410Dlg::OnRadioFashion() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_EDIT_STARTHEX)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_ENDHEX)->EnableWindow(FALSE);
	GetDlgItem(IDC_EDIT_SEND)->EnableWindow(TRUE);
	UpdateData(TRUE);
}

void CComm2410Dlg::OnRadio3() 
{
	// TODO: Add your control notification handler code here
	GetDlgItem(IDC_EDIT_STARTHEX)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_ENDHEX)->EnableWindow(TRUE);
	GetDlgItem(IDC_EDIT_SEND)->EnableWindow(FALSE);
	UpdateData(TRUE);
}



void CComm2410Dlg::OnKillfocusEditSendnumber() 
{
	// TODO: Add your control notification handler code here
	int iRestore;
	iRestore=m_count;
	UpdateData(TRUE);		

	if (m_count >= 0xffffffff)
	{
		AfxMessageBox(_T("已超过最大值!"));
		m_count=iRestore;
	}
	UpdateData(FALSE);
}


void CComm2410Dlg::OnKillfocusEditStarthex() 
{
	// TODO: Add your control notification handler code here
	int iRestore,iStart;
	LPCWSTR lpstr = (LPCWSTR)m_iStarthex;
	swscanf(lpstr,_T("%x"),&iStart);
    iRestore=iStart;
	UpdateData(TRUE);

    lpstr = (LPCWSTR)m_iStarthex;
	swscanf(lpstr,_T("%x"),&iStart);
	if ((iStart < 0)||(iStart > 0x00ff))
	{
		iStart=iRestore;
		m_iStarthex.Format(_T("%x"),iStart);
	}
	UpdateData(FALSE);
}

void CComm2410Dlg::OnKillfocusEditEndhex() 
{
	// TODO: Add your control notification handler code here
	int iRestore,iEnd;
	LPCWSTR lpstr = (LPCWSTR)m_iEndhex;
	swscanf(lpstr,_T("%x"),&iEnd);
    iRestore=iEnd;
	UpdateData(TRUE);

	
    lpstr = (LPCWSTR)m_iEndhex;
	swscanf(lpstr,_T("%x"),&iEnd);
	if ((iEnd < 0)||(iEnd > 0x00ff))
	{
		iEnd=iRestore;
		//	swprintf(tempbuffer,_T("%d"),iEnd);

		m_iEndhex.Format(_T("%x"),iEnd);
	}
	UpdateData(FALSE);
}

void CComm2410Dlg::OnRadioForever() 
{
	// TODO: Add your control notification handler code here
   	
}


void CComm2410Dlg::OnCheckForever() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	if (m_iForEver)
        GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(FALSE);
	else 
		GetDlgItem(IDC_EDIT_SENDNUMBER)->EnableWindow(TRUE);

}

⌨️ 快捷键说明

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