MFC 串口检测

枚举串口

void CN4010A_CPP_Example_ProgramDlg::getcom()
{
	/
	//以下功能为:查找电脑中所有串口并显示到combox;
	m_Comlist.ResetContent();
	HKEY hKey; 
	LONG ret; 
	OSVERSIONINFO  osvi; 
	BOOL bOsVersionInfoEx; 
	char keyinfo[100],comm_name[40],ValueName[40]; 
	int i; 
	DWORD sType,Reserved,cbData,cbValueName; 
	
	//hIcon =AfxGetApp()->LoadIcon(IDI_HARDWARE); 
	//SetIcon(hIcon,false); 
	
	ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); 
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); 
	
	memset(keyinfo,0,100); 
	strcpy(keyinfo,"HARDWARE\\DEVICEMAP\\SERIALCOMM"); 
    i=0; sType=REG_SZ;Reserved=0; 
	bOsVersionInfoEx =GetVersionEx(&osvi); 
    ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,keyinfo,0,KEY_ALL_ACCESS,&hKey); 
	if (ret==ERROR_SUCCESS){ 
		// 10-25 
		if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)  
		{ 
			for(i=1;i<=128;i++) 
			{     
				sprintf(comm_name,"COM%d",i); 
				m_Comlist.AddString(comm_name); 
			} 
		} 
		else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) 
		{ 
			do  
			{ 
				cbData=40;cbValueName=40; 
				memset(comm_name,0,40);  
				memset(ValueName,0,40); 
				ret=RegEnumValue(hKey,i,ValueName,&cbValueName,NULL,&sType,(LPBYTE)comm_name,&cbData); 
				if (ret==ERROR_SUCCESS) 
				{ 
					m_Comlist.AddString(comm_name); 
					i++; 
				} 
			}while (ret==ERROR_SUCCESS); 
		} 
	} 
	RegCloseKey(hKey); 
	/

 
	//m_Comlist.SetCurSel(0);
}

获取选中串口号

void CN4010A_CPP_Example_ProgramDlg::getport()
{
	//
	//功能为:得到串口编号;	
	m_Comlist.GetCurSel();

	m_Comlist.GetWindowText(CurPort);
	int i;
	CString ks;
	CString states;
	for(i=0; i < strlen(CurPort);i++)
	{
		if(CurPort[i]>='0'&&CurPort[i]<='9')
		{
			ks+=CurPort[i];
		}
	}
 
		num=atoi(ks);//cstring字符型转换为short型

		TRACE("*****num:%d" , num);

}

打开串口

void CN4010A_CPP_Example_ProgramDlg::OnButtonPort() 
{
	// TODO: Add your control notification handler code here


	if(!isPortOpen)
	{
	//luke add st
		getport();
		
		if(m_Comm1.GetPortOpen())
			m_Comm1.SetPortOpen(FALSE);
		m_Comm1.SetCommPort(num);
		if(!m_Comm1.GetPortOpen())
		{	
			m_Comm1.SetPortOpen(TRUE);
		}
		else  		
			AfxMessageBox("fail to open!");
		
		m_Comm1.SetSettings("115200,n,8,1");
		m_Comm1.SetInputMode(1);
		m_Comm1.SetRThreshold(1);
		m_Comm1.SetInputLen(0);
		m_Comm1.GetInput();

		GetDlgItem(IDC_BUTTON_PORT)->SetWindowText("Close");
		isPortOpen = true;
	//add ed
	}
	else
	{
		if (m_Comm1.GetPortOpen())
		{
			m_Comm1.SetPortOpen(FALSE);
		}

		GetDlgItem(IDC_BUTTON_PORT)->SetWindowText("Open");
		isPortOpen = false;
	}
	
}







评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值