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

📄 mycomdlg.cpp

📁 串口通信程序设置
💻 CPP
📖 第 1 页 / 共 2 页
字号:


LONG CMycomDlg::OnShowData(WPARAM w,LPARAM l)
{

    CEdit *pEdit;
	pEdit = (CEdit*)GetDlgItem(IDC_EDIT_DISPLAY);

	int len,i;
    CString str,str1;
    if(m_isHex)//是否16进制显示
	{
	   str = ToHex(szReadBuffer);
	}
	else
	{
		i=0;
		while(i < INBUF_SIZE)
		{
		
			str1.Format("%c",szReadBuffer[i]);
			str += str1;
			str += "  ";
			i++;
			if(i%10 == 0)
			{
				str += "\r\n";
			}
					
		}
	
	}
	len = str.GetLength();
	pEdit->SetWindowText(str);
	return 1;
}

void CMycomDlg::OnButtonRecv() 
{
	// TODO: Add your control notification handler code here
	DWORD dwThreadID;
    hCommWatchThread = CreateThread((LPSECURITY_ATTRIBUTES)NULL,//安全属性
		          0,//初始化线程栈的大小,缺省为与主线程大小相同
				  (LPTHREAD_START_ROUTINE)ReciveProc, //线程的全局函数 
				  GetSafeHwnd(),//此处传入了主框架的句柄 
				  0,//创建好后,立即运行
				  &dwThreadID);
	AfxMessageBox("启动接收");
	ASSERT(hCommWatchThread != NULL);
	
}

void CMycomDlg::OnButtonSend() 
{
	// TODO: Add your control notification handler code here
	DWORD dwBytesToWrite,dwBytesWritten;
	int len = 0;
	CString str;
	char *pstr;
	CEdit *pEdit;
	pEdit = (CEdit*)GetDlgItem(IDC_EDIT_DISPLAY);
    pEdit->GetWindowText(str);

    len = str.GetLength();
	
	if(len <= 0)
	{
	   AfxMessageBox("请输入发送字符!");
	   return;
	}
    
	if(len > OUTBUF_SIZE)
		len = OUTBUF_SIZE;

    pstr = (char*)str.GetBuffer(len);
    
	for(int i = 0; i < len; i++)
	{
	   szWriteBuffer[i] = pstr[i];
	}

	Send(hCom,szWriteBuffer,dwBytesToWrite,dwBytesWritten);
}

void CMycomDlg::SetBautRate(HANDLE hCom,DWORD bautRate)
{
	
 
	DCB dcb;
	BOOL bSuccess;
	memset(&dcb,0,sizeof(DCB));
    dcb.DCBlength = sizeof(DCB);
    bSuccess = GetCommState(hCom, &dcb);
	
	if(!bSuccess)
	{
		AfxMessageBox("can't get Com state!");
		return;
	}
    dcb.Parity = NOPARITY; // no parity bit
    dcb.BaudRate = bautRate;
		
	bSuccess = SetCommState(hCom, &dcb);
   
	if(bSuccess)
	{
	   AfxMessageBox("设置波特率成功!");
	}
}

void CMycomDlg::SetStopBit(HANDLE hCom,UINT stopBit)
{

    DCB dcb;
	BOOL bSuccess;
	memset(&dcb,0,sizeof(DCB));
    dcb.DCBlength = sizeof(DCB);
    bSuccess = GetCommState(hCom, &dcb);
	
	if(!bSuccess)
	{
		AfxMessageBox("can't get Com state!");
		return;
	}

	dcb.Parity = NOPARITY;     // no parity bit

	if(stopBit == 1)
	{
		dcb.StopBits = ONESTOPBIT; // one stop bit
	}
	else if(stopBit == 2)
	{
        dcb.StopBits = ONE5STOPBITS;//1.5 stop bit
	}
	else 
	{
        dcb.StopBits = TWOSTOPBITS;//two stop bit
	}
	
	bSuccess = SetCommState(hCom, &dcb);

	if(bSuccess)
	{
	   AfxMessageBox("设置停止位成功!");
	}
}

void CMycomDlg::SetByteSize(HANDLE hCom,UINT byteSize)
{

    DCB dcb;
	BOOL bSuccess;
	memset(&dcb,0,sizeof(DCB));
    dcb.DCBlength = sizeof(DCB);
    bSuccess = GetCommState(hCom, &dcb);
	
	if(!bSuccess)
	{
		AfxMessageBox("can't get Com state!");
		return;
	}

   
	dcb.ByteSize = byteSize;   // data size, xmit, and rcv
	dcb.Parity = NOPARITY;     // no parity bit

	bSuccess = SetCommState(hCom, &dcb);

	if(bSuccess)
	{
	   AfxMessageBox("设置字节大小成功!");
	}
}


void CMycomDlg::OnCheckHexdecimal() 
{
	// TODO: Add your control notification handler code here
	CButton *pChBt;
	pChBt = (CButton*)GetDlgItem(IDC_CHECK_HEXDECIMAL);
    if(pChBt->GetCheck())
	{
		m_isHex = TRUE;
	}
	else
	{
        m_isHex = FALSE;
	}
}

void CMycomDlg::OnSelchangeComboBautrate() 
{
	// TODO: Add your control notification handler code here
	int index;
	DWORD bautRate;
	CString str;
	CComboBox *pComBox = (CComboBox*)GetDlgItem(IDC_COMBO_BAUTRATE);
	index = pComBox->GetCurSel();
	pComBox->GetLBText(index,str);
	if(str == "110")
	{
		bautRate = CBR_110;
	}
	else if(str == "300")
	{
		bautRate = CBR_300;
	}
	else if(str == "600")
	{
		bautRate = CBR_600;
	}
	else if(str == "1200")
	{ 
		bautRate = CBR_1200;
	}
	else if(str == "2400")
	{ 
		bautRate = CBR_2400;
	}
	else if(str == "4800")
	{
		bautRate = CBR_4800;
	}
	else if(str == "9600")
	{
		bautRate = CBR_9600;
	}
	else if(str == "14400")
	{
		bautRate = CBR_14400;
	}
	else if(str == "19200")
	{
		bautRate = CBR_19200;
	}
	else if(str == "38400")
	{
		bautRate = CBR_38400;
	}
	else if(str == "57600")
	{
		bautRate = CBR_57600;
	}
	else if(str == "115200")
	{
		bautRate = CBR_115200;
	}
	else if(str == "128000")
	{
		bautRate = CBR_128000;
	}
	else if(str == "256000")
	{
		bautRate = CBR_256000;
	}
    SetBautRate(hCom,bautRate);	
}

void CMycomDlg::OnSelchangeComboBytesize() 
{
	// TODO: Add your control notification handler code here
	int index;
	DWORD byteSize;
	CString str;
	CComboBox *pComBox = (CComboBox*)GetDlgItem(IDC_COMBO_BAUTRATE);
	index = pComBox->GetCurSel();
	pComBox->GetLBText(index,str);
	if(str == "8")
	{
		byteSize = 8;
	}
	SetByteSize(hCom,byteSize);
}

void CMycomDlg::OnSelchangeComboStopbit() 
{
	// TODO: Add your control notification handler code here
    int index;
	DWORD stopBit;
	CString str;
	CComboBox *pComBox = (CComboBox*)GetDlgItem(IDC_COMBO_BAUTRATE);
	index = pComBox->GetCurSel();
	pComBox->GetLBText(index,str);
	if(str == "1")
	{
		stopBit = 1;
	}
	else if(str == "1.5")
	{
	    stopBit = 2;
	}
	else if(str == "2")
	{
		stopBit = 3;
	}
	SetStopBit(hCom,stopBit);
}

void CMycomDlg::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	CloseHandle(hCom);//关串口句柄
	CDialog::OnClose();
}


void CMycomDlg::Recv(HANDLE hCom,char *szReadBuffer,DWORD dwBytesToRead,DWORD dwBytesRead)
{

	BOOL bReadStat;	
	OVERLAPPED overlapped;
	memset(&overlapped, 0, sizeof(OVERLAPPED));
	bReadStat = ReadFile(hCom, //串口句柄
				szReadBuffer,//
				dwBytesToRead, //读最大数目
				&dwBytesRead,//实际读得的数目
				&overlapped);//
		
	if(!bReadStat)		
	{	
		if(GetLastError() == ERROR_IO_PENDING)			
		{	
			while(!GetOverlappedResult(hCom,
						&overlapped,
						&dwBytesRead, TRUE));//
		}
	}
}



char * CMycomDlg::itohs(int n,char * ps)
{
    int i,k;
    int size=4;
	
	ps[size]='\0';
    ps[0] = '0';
	ps[1] = 'x';
	for (i=size-1;i>=2;i--)
	{
		k=n%16;
		n/=16;
		
		if (k<10)
			ps[i]='0'+k;
		else
			ps[i]='A'+(k-10);
	}
    return ps;
}

CString CMycomDlg::ToHex(char *str)
{
   char *tmpStr;
   char tmpCh,tmpCh1[4];
   int i,tmp;
   CString str1 = "";
   if(str == '\0')
	   return "";

   i = 0; 
   while(str[i] != '\0')
   { 
	   tmpCh = str[i]; 
	   tmp = atoi(&tmpCh);  
	   itohs(tmp,tmpCh1);  
	   str1 += tmpCh1;
	   str1 += "  ";
	   i++;
	   if(i%10 == 0)
	   {
	      str1 += "\r\n";
	   }
   }
   return str1;
}



    


⌨️ 快捷键说明

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