枚举串口
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;
}
}