// CDialogCommSet1.cpp: 实现文件 // #include "pch.h" #include "KLink2.h" #include "afxdialogex.h" #include #include #include #include "CDialogCommSet1.h" #include #include #pragma comment(lib,"SetupAPI.lib") // CDialogCommSet1 对话框 IMPLEMENT_DYNAMIC(CDialogCommSet1, CDialogEx) CDialogCommSet1::CDialogCommSet1(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_DIALOG_COMMSET1, pParent) { } CDialogCommSet1::~CDialogCommSet1() { } void CDialogCommSet1::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_COMBO_PORT, m_combo_comport); } BEGIN_MESSAGE_MAP(CDialogCommSet1, CDialogEx) ON_WM_DEVICECHANGE() ON_BN_CLICKED(IDOK, &CDialogCommSet1::OnBnClickedOk) ON_BN_CLICKED(IDC_BUTTON_INIT, &CDialogCommSet1::OnBnClickedButtonInit) END_MESSAGE_MAP() // CDialogCommSet1 消息处理程序 BOOL CDialogCommSet1::OnInitDialog() { CDialogEx::OnInitDialog(); // TODO: 在此添加额外的初始化 ShowParams(); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } //得到COMx的名字 //namebuf:用于存放名字的缓冲区 //bufsize:缓冲区大小 //comx:要查找的COM编号.例如:COM1,COM2,COM3.... //返回值:0,成功找到了. // 1,失败. int get_com_name(CString comx, CString &namebuf) { HDEVINFO hdinfo; int res = 0; SP_DEVINFO_DATA hddevinfo = { sizeof(SP_DEVINFO_DATA) }; hdinfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);//获取PORTS类别的已安装设备信息 if (hdinfo != INVALID_HANDLE_VALUE)//获取成功 { for (int i = 0; SetupDiEnumDeviceInfo(hdinfo, i, &hddevinfo); i++)//轮询所有已安装设备 { SetupDiGetDeviceRegistryProperty(hdinfo, &hddevinfo, SPDRP_FRIENDLYNAME, 0, (PBYTE)(namebuf.GetBuffer(2048)), 2048, 0);//获得单个装置的详细资料 namebuf.ReleaseBuffer(); if (namebuf.Find(comx) != -1) { res = 1; break; } //char *t; //t = strstr(namebuf, comx); //if (t) //{ // t--; // *t = '\0';//添加结束符,作用就是把"(COMX)"这段字符去掉 // res = 0; // break;//成功找到了COMx的名字 //} } } return res; } int CDialogCommSet1::UpdateComPortList() { // TODO: 在此处添加实现代码. CString s1, s2; m_combo_comport.ResetContent(); for (int i = 1; i <= 16; i++) { s1.Format(_T("COM%d"), i); if (get_com_name(s1, s2)) s1 = s2; m_combo_comport.AddString(s1); } sComSelStr = _T("COM3"); m_combo_comport.SelectString(0, sComSelStr); m_combo_comport.SetCurSel(2); return 0; HKEY hKey; if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS) { TCHAR szPortName[256], szComName[256]; DWORD dwLong, dwSize; int nCount = 0; m_combo_comport.ResetContent(); while (true) { dwLong = dwSize = 256; if (RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize) == ERROR_NO_MORE_ITEMS) break; m_combo_comport.InsertString(nCount, szComName); nCount++; } RegCloseKey(hKey); m_combo_comport.SetCurSel(0); } return 0; } BOOL CDialogCommSet1::OnDeviceChange(UINT nEventType, DWORD_PTR dwData) { if (nEventType == DBT_DEVNODES_CHANGED) UpdateComPortList(); return TRUE; } int CDialogCommSet1::DisplayParams() { // TODO: 在此处添加实现代码. return 0; } int CDialogCommSet1::ShowParams() { // TODO: 在此处添加实现代码. ((CComboBox *)GetDlgItem(IDC_COMBO_NETWORK_TYPE))->SetCurSel(0); ((CComboBox *)GetDlgItem(IDC_COMBO_PORT))->SetCurSel(0); ((CComboBox *)GetDlgItem(IDC_COMBO_BAUD))->SetCurSel(8); ((CButton *)GetDlgItem(IDC_RADIO_DATA_LENGTH_8B))->SetCheck(1); ((CButton *)GetDlgItem(IDC_RADIO_STOP_1B))->SetCheck(1); ((CButton *)GetDlgItem(IDC_RADIO_PARITY_NONE))->SetCheck(1); ((CComboBox *)GetDlgItem(IDC_COMBO_COMM_TIMEOUT))->SetCurSel(5); ((CButton *)GetDlgItem(IDC_CHECK_AUTO_BAUD))->SetCheck(1); ((CButton *)GetDlgItem(IDC_CHECK_AUTO_DATA_LENGTH))->SetCheck(1); ((CButton *)GetDlgItem(IDC_CHECK_AUTO_PARITY))->SetCheck(1); UpdateComPortList(); return 0; } int CDialogCommSet1::GetParams() { // TODO: 在此处添加实现代码. ((CComboBox *)GetDlgItem(IDC_COMBO_NETWORK_TYPE))->GetCurSel(); m_nComNum = ((CComboBox *)GetDlgItem(IDC_COMBO_PORT))->GetCurSel() + 1; //((CComboBox *)GetDlgItem(IDC_COMBO_BAUD))->GetCurSel(); CString baudstr; GetDlgItemText(IDC_COMBO_BAUD, baudstr); m_nBaudRate = _tstoi(baudstr); m_Settings = "8,N,1"; ((CButton *)GetDlgItem(IDC_RADIO_DATA_LENGTH_8B))->GetCheck(); ((CButton *)GetDlgItem(IDC_RADIO_STOP_1B))->GetCheck(); ((CButton *)GetDlgItem(IDC_RADIO_PARITY_NONE))->GetCheck(); ((CComboBox *)GetDlgItem(IDC_COMBO_COMM_TIMEOUT))->GetCurSel(); ((CButton *)GetDlgItem(IDC_CHECK_AUTO_BAUD))->GetCheck(); ((CButton *)GetDlgItem(IDC_CHECK_AUTO_DATA_LENGTH))->GetCheck(); ((CButton *)GetDlgItem(IDC_CHECK_AUTO_PARITY))->GetCheck(); return 0; } void CDialogCommSet1::OnOK() { // TODO: 在此添加专用代码和/或调用基类 GetParams(); CDialogEx::OnOK(); } void CDialogCommSet1::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 GetParams(); CDialogEx::OnOK(); } void CDialogCommSet1::OnBnClickedButtonInit() { // TODO: 在此添加控件通知处理程序代码 }