提交 | 用户 | age
|
0a20f7
|
1 |
// CDialogCommSet1.cpp: 实现文件
|
Q |
2 |
//
|
|
3 |
|
|
4 |
#include "pch.h"
|
|
5 |
#include "KLink2.h"
|
|
6 |
#include "afxdialogex.h"
|
|
7 |
#include <devguid.h>
|
|
8 |
#include <SetupAPI.h>
|
|
9 |
#include <Dbt.h>
|
|
10 |
#include "CDialogCommSet1.h"
|
|
11 |
|
|
12 |
#include <devguid.h>
|
|
13 |
#include <SetupAPI.h>
|
|
14 |
|
|
15 |
#pragma comment(lib,"SetupAPI.lib")
|
|
16 |
|
|
17 |
// CDialogCommSet1 对话框
|
|
18 |
|
|
19 |
IMPLEMENT_DYNAMIC(CDialogCommSet1, CDialogEx)
|
|
20 |
|
|
21 |
CDialogCommSet1::CDialogCommSet1(CWnd* pParent /*=nullptr*/)
|
|
22 |
: CDialogEx(IDD_DIALOG_COMMSET1, pParent)
|
|
23 |
{
|
|
24 |
|
|
25 |
}
|
|
26 |
|
|
27 |
CDialogCommSet1::~CDialogCommSet1()
|
|
28 |
{
|
|
29 |
}
|
|
30 |
|
|
31 |
void CDialogCommSet1::DoDataExchange(CDataExchange* pDX)
|
|
32 |
{
|
|
33 |
CDialogEx::DoDataExchange(pDX);
|
|
34 |
DDX_Control(pDX, IDC_COMBO_PORT, m_combo_comport);
|
|
35 |
}
|
|
36 |
|
|
37 |
|
|
38 |
BEGIN_MESSAGE_MAP(CDialogCommSet1, CDialogEx)
|
|
39 |
|
|
40 |
ON_WM_DEVICECHANGE()
|
|
41 |
|
|
42 |
ON_BN_CLICKED(IDOK, &CDialogCommSet1::OnBnClickedOk)
|
|
43 |
ON_BN_CLICKED(IDC_BUTTON_INIT, &CDialogCommSet1::OnBnClickedButtonInit)
|
|
44 |
END_MESSAGE_MAP()
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
// CDialogCommSet1 消息处理程序
|
|
49 |
|
|
50 |
BOOL CDialogCommSet1::OnInitDialog()
|
|
51 |
{
|
|
52 |
CDialogEx::OnInitDialog();
|
|
53 |
|
|
54 |
// TODO: 在此添加额外的初始化
|
|
55 |
ShowParams();
|
|
56 |
return TRUE; // return TRUE unless you set the focus to a control
|
|
57 |
// 异常: OCX 属性页应返回 FALSE
|
|
58 |
}
|
|
59 |
|
|
60 |
//得到COMx的名字
|
|
61 |
//namebuf:用于存放名字的缓冲区
|
|
62 |
//bufsize:缓冲区大小
|
|
63 |
//comx:要查找的COM编号.例如:COM1,COM2,COM3....
|
|
64 |
//返回值:0,成功找到了.
|
|
65 |
// 1,失败.
|
|
66 |
int get_com_name(CString comx, CString &namebuf)
|
|
67 |
{
|
|
68 |
HDEVINFO hdinfo;
|
|
69 |
int res = 0;
|
|
70 |
SP_DEVINFO_DATA hddevinfo = { sizeof(SP_DEVINFO_DATA) };
|
|
71 |
hdinfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, 0, 0, DIGCF_PRESENT);//获取PORTS类别的已安装设备信息
|
|
72 |
if (hdinfo != INVALID_HANDLE_VALUE)//获取成功
|
|
73 |
{
|
|
74 |
for (int i = 0; SetupDiEnumDeviceInfo(hdinfo, i, &hddevinfo); i++)//轮询所有已安装设备
|
|
75 |
{
|
|
76 |
SetupDiGetDeviceRegistryProperty(hdinfo, &hddevinfo, SPDRP_FRIENDLYNAME, 0, (PBYTE)(namebuf.GetBuffer(2048)), 2048, 0);//获得单个装置的详细资料
|
|
77 |
namebuf.ReleaseBuffer();
|
|
78 |
if (namebuf.Find(comx) != -1) {
|
|
79 |
res = 1; break;
|
|
80 |
}
|
|
81 |
//char *t;
|
|
82 |
//t = strstr(namebuf, comx);
|
|
83 |
//if (t)
|
|
84 |
//{
|
|
85 |
// t--;
|
|
86 |
// *t = '\0';//添加结束符,作用就是把"(COMX)"这段字符去掉
|
|
87 |
// res = 0;
|
|
88 |
// break;//成功找到了COMx的名字
|
|
89 |
//}
|
|
90 |
}
|
|
91 |
}
|
|
92 |
return res;
|
|
93 |
}
|
|
94 |
|
|
95 |
int CDialogCommSet1::UpdateComPortList()
|
|
96 |
{
|
|
97 |
// TODO: 在此处添加实现代码.
|
|
98 |
CString s1, s2;
|
|
99 |
|
|
100 |
m_combo_comport.ResetContent();
|
|
101 |
for (int i = 1; i <= 16; i++)
|
|
102 |
{
|
|
103 |
s1.Format(_T("COM%d"), i);
|
|
104 |
if (get_com_name(s1, s2)) s1 = s2;
|
|
105 |
m_combo_comport.AddString(s1);
|
|
106 |
}
|
|
107 |
sComSelStr = _T("COM3");
|
|
108 |
m_combo_comport.SelectString(0, sComSelStr);
|
|
109 |
m_combo_comport.SetCurSel(2);
|
|
110 |
return 0;
|
|
111 |
HKEY hKey;
|
|
112 |
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("Hardware\\DeviceMap\\SerialComm"), NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
|
|
113 |
{
|
|
114 |
TCHAR szPortName[256], szComName[256];
|
|
115 |
DWORD dwLong, dwSize;
|
|
116 |
int nCount = 0;
|
|
117 |
|
|
118 |
m_combo_comport.ResetContent();
|
|
119 |
while (true)
|
|
120 |
{
|
|
121 |
dwLong = dwSize = 256;
|
|
122 |
if (RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize) == ERROR_NO_MORE_ITEMS)
|
|
123 |
break;
|
|
124 |
|
|
125 |
m_combo_comport.InsertString(nCount, szComName);
|
|
126 |
nCount++;
|
|
127 |
}
|
|
128 |
RegCloseKey(hKey);
|
|
129 |
m_combo_comport.SetCurSel(0);
|
|
130 |
}
|
|
131 |
|
|
132 |
return 0;
|
|
133 |
}
|
|
134 |
|
|
135 |
BOOL CDialogCommSet1::OnDeviceChange(UINT nEventType, DWORD_PTR dwData)
|
|
136 |
{
|
|
137 |
if (nEventType == DBT_DEVNODES_CHANGED)
|
|
138 |
UpdateComPortList();
|
|
139 |
|
|
140 |
return TRUE;
|
|
141 |
}
|
|
142 |
|
|
143 |
int CDialogCommSet1::DisplayParams()
|
|
144 |
{
|
|
145 |
// TODO: 在此处添加实现代码.
|
|
146 |
return 0;
|
|
147 |
}
|
|
148 |
|
|
149 |
int CDialogCommSet1::ShowParams()
|
|
150 |
{
|
|
151 |
// TODO: 在此处添加实现代码.
|
|
152 |
((CComboBox *)GetDlgItem(IDC_COMBO_NETWORK_TYPE))->SetCurSel(0);
|
|
153 |
((CComboBox *)GetDlgItem(IDC_COMBO_PORT))->SetCurSel(0);
|
|
154 |
((CComboBox *)GetDlgItem(IDC_COMBO_BAUD))->SetCurSel(8);
|
|
155 |
|
|
156 |
((CButton *)GetDlgItem(IDC_RADIO_DATA_LENGTH_8B))->SetCheck(1);
|
|
157 |
|
|
158 |
((CButton *)GetDlgItem(IDC_RADIO_STOP_1B))->SetCheck(1);
|
|
159 |
|
|
160 |
((CButton *)GetDlgItem(IDC_RADIO_PARITY_NONE))->SetCheck(1);
|
|
161 |
|
|
162 |
((CComboBox *)GetDlgItem(IDC_COMBO_COMM_TIMEOUT))->SetCurSel(5);
|
|
163 |
((CButton *)GetDlgItem(IDC_CHECK_AUTO_BAUD))->SetCheck(1);
|
|
164 |
((CButton *)GetDlgItem(IDC_CHECK_AUTO_DATA_LENGTH))->SetCheck(1);
|
|
165 |
((CButton *)GetDlgItem(IDC_CHECK_AUTO_PARITY))->SetCheck(1);
|
|
166 |
|
|
167 |
UpdateComPortList();
|
|
168 |
return 0;
|
|
169 |
}
|
|
170 |
|
|
171 |
int CDialogCommSet1::GetParams()
|
|
172 |
{
|
|
173 |
// TODO: 在此处添加实现代码.
|
|
174 |
((CComboBox *)GetDlgItem(IDC_COMBO_NETWORK_TYPE))->GetCurSel();
|
|
175 |
m_nComNum = ((CComboBox *)GetDlgItem(IDC_COMBO_PORT))->GetCurSel() + 1;
|
|
176 |
//((CComboBox *)GetDlgItem(IDC_COMBO_BAUD))->GetCurSel();
|
|
177 |
CString baudstr;
|
|
178 |
GetDlgItemText(IDC_COMBO_BAUD, baudstr);
|
|
179 |
m_nBaudRate = _tstoi(baudstr);
|
|
180 |
m_Settings = "8,N,1";
|
|
181 |
|
|
182 |
((CButton *)GetDlgItem(IDC_RADIO_DATA_LENGTH_8B))->GetCheck();
|
|
183 |
|
|
184 |
((CButton *)GetDlgItem(IDC_RADIO_STOP_1B))->GetCheck();
|
|
185 |
|
|
186 |
((CButton *)GetDlgItem(IDC_RADIO_PARITY_NONE))->GetCheck();
|
|
187 |
|
|
188 |
((CComboBox *)GetDlgItem(IDC_COMBO_COMM_TIMEOUT))->GetCurSel();
|
|
189 |
((CButton *)GetDlgItem(IDC_CHECK_AUTO_BAUD))->GetCheck();
|
|
190 |
((CButton *)GetDlgItem(IDC_CHECK_AUTO_DATA_LENGTH))->GetCheck();
|
|
191 |
((CButton *)GetDlgItem(IDC_CHECK_AUTO_PARITY))->GetCheck();
|
|
192 |
|
|
193 |
return 0;
|
|
194 |
}
|
|
195 |
|
|
196 |
void CDialogCommSet1::OnOK()
|
|
197 |
{
|
|
198 |
// TODO: 在此添加专用代码和/或调用基类
|
|
199 |
GetParams();
|
|
200 |
CDialogEx::OnOK();
|
|
201 |
}
|
|
202 |
|
|
203 |
void CDialogCommSet1::OnBnClickedOk()
|
|
204 |
{
|
|
205 |
// TODO: 在此添加控件通知处理程序代码
|
|
206 |
GetParams();
|
|
207 |
CDialogEx::OnOK();
|
|
208 |
}
|
|
209 |
|
|
210 |
void CDialogCommSet1::OnBnClickedButtonInit()
|
|
211 |
{
|
|
212 |
// TODO: 在此添加控件通知处理程序代码
|
|
213 |
}
|
|
214 |
|