如题!
UINT BaudTable[15][2]=
{
{0,110},
{1,300},
{2,600},
{3,1200},
{4,2400},
{5,4800},
{6,9600},
{7,14400},
{8,19200},
{9,38400},
{10,56000},
{11,57600},
{12,115200},
{13,128000},
{14,256000}
};
BOOL ComSetDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
CString str;
int Index;
HKEY hKey;
LONG ret;
OSVERSIONINFO osvi;
BOOL bOsVersionInfoEx;
char keyinfo[100],comm_name[200],ValueName[200];
int i;
DWORD sType,Reserved,cbData,cbValueName;
for(i=0;i<15;i++)
{
str.Format("%d",BaudTable[i][1]);
m_ComboBaud.AddString(str);
}
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){
if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
for(i=1;i<=128;i++)
{
sprintf(comm_name,"COM%d",i);
m_CtrlPortNum.AddString(comm_name);
}
}
else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
do
{
cbData=200;cbValueName=200;
memset(comm_name,0,200);
memset(ValueName,0,200);
ret=RegEnumValue(hKey,i,ValueName,&cbValueName,NULL,&sType,(LPBYTE)comm_name,&cbData);
if (strlen(comm_name) > 0)
{
// 检测出来的串口
//sprintf(comm_name,"COM%d%s",i,comm_name);
m_CtrlPortNum.AddString(comm_name);
}
i++;
}while (ret==ERROR_SUCCESS);
}
}
RegCloseKey(hKey);
MainDlg = (CUpFlashDlg*)AfxGetApp()->m_pMainWnd;
PortNum = MainDlg->nSerialPortNum;
BaudRate = MainDlg->nBaudRate;
str.Format("COM%d",PortNum);
Index=m_CtrlPortNum.FindString(-1,str);
if(Index>=0)
m_CtrlPortNum.SetCurSel(Index);
else
m_CtrlPortNum.SetCurSel(0);
str.Format("%d",BaudRate);
Index=m_ComboBaud.FindString(-1,str);
if(Index>=0)
m_ComboBaud.SetCurSel(Index);
else
m_ComboBaud.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
然,得SerialPort欲用之,且听我胡诌乱侃。
另附笑话一则,说:苍蝇妈妈带着小苍蝇吃饭,它们飞到一堆牛粪上,小苍蝇郁闷地问:“妈妈,为什么咱们总是吃牛粪呢?”苍蝇妈妈给了小苍蝇一记耳光,骂道:“这倒霉孩子,吃饭的时候不许说这么恶心的事儿!”