DlgDevice.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. #include "stdafx.h"
  2. #include "DlgDevice.h"
  3. #include "StringOperation.h"
  4. #include ".\dlgdevice.h"
  5. #include "MainFrm.h"
  6. #include "resource.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CDlgDevice property page
  14. IMPLEMENT_DYNCREATE(CDlgDevice, CPropertyPage)
  15. CDlgDevice::CDlgDevice() : CPropertyPage(CDlgDevice::IDD)
  16. , m_nCommuniteType(0)
  17. , m_nProtocolType(0)
  18. , m_strProtocolName(_T(""))
  19. , m_nDevEnable(0)
  20. , m_nCollectDev(0)
  21. , m_strIniName(_T(""))
  22. , m_strReserved1(_T(""))
  23. , m_strReserved2(_T(""))
  24. , m_strReserved3(_T(""))
  25. , m_strReserved4(_T(""))
  26. , m_strReserved5(_T(""))
  27. , m_nReserved1(0)
  28. , m_nReserved2(0)
  29. , m_nReserved3(0)
  30. , m_nReserved4(0)
  31. , m_nReserved5(0)
  32. , m_bReserved1(FALSE)
  33. , m_bReserved2(FALSE)
  34. , m_bReserved3(FALSE)
  35. , m_bReserved4(FALSE)
  36. , m_bReserved5(FALSE)
  37. {
  38. //{{AFX_DATA_INIT(CDlgDevice)
  39. m_nDevDriverID = -1;
  40. m_nDevFactoryID = -1;
  41. m_nDevTypeID = -1;
  42. //}}AFX_DATA_INIT
  43. m_bIsStu2000 = false;
  44. }
  45. CDlgDevice::~CDlgDevice()
  46. {
  47. }
  48. void CDlgDevice::DoDataExchange(CDataExchange* pDX)
  49. {
  50. CPropertyPage::DoDataExchange(pDX);
  51. //{{AFX_DATA_MAP(CDlgDevice)
  52. //}}AFX_DATA_MAP
  53. DDX_Control(pDX, IDC_TREE_DEVICE_FOLDER, m_ctrlDevFolder);
  54. //DDX_Control(pDX, IDC_LIST_DEVICE, m_ctrlDevice);
  55. DDX_Radio(pDX, IDC_RADIO_COMM, m_nCommuniteType);
  56. DDX_CBIndex(pDX, IDC_COMBO_PROCOTOL_TYPE, m_nProtocolType);
  57. DDX_Text(pDX, IDC_EDT_DLL_NAME, m_strProtocolName);
  58. DDX_Check(pDX, IDC_CHK_ENABLE, m_nDevEnable);
  59. DDX_Check(pDX, IDC_CHK_COLLECT_DEV, m_nCollectDev);
  60. DDX_Text(pDX, IDC_EDT_INI_NAME, m_strIniName);
  61. }
  62. BEGIN_MESSAGE_MAP(CDlgDevice, CPropertyPage)
  63. //{{AFX_MSG_MAP(CDlgDevice)
  64. //}}AFX_MSG_MAP
  65. ON_NOTIFY(TVN_SELCHANGED, IDC_TREE_DEVICE_FOLDER, OnTvnSelchangedTreeDeviceFolder)
  66. ON_BN_CLICKED(IDC_BTN_SEL_DLL, OnBnClickedBtnSelDll)
  67. ON_BN_CLICKED(IDC_BTN_SEL_INI, OnBnClickedBtnSelIni)
  68. END_MESSAGE_MAP()
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CDlgDevice message handlers
  71. BOOL CDlgDevice::OnInitDialog()
  72. {
  73. CPropertyPage::OnInitDialog();
  74. // TODO: 在此添加额外的初始化
  75. if( !m_imagelistTree )
  76. {
  77. m_imagelistTree.Create(IDB_ADD_DEVICE, 16, 16, RGB(255,255,255));
  78. }
  79. m_ctrlDevFolder.SetImageList(&m_imagelistTree, TVSIL_NORMAL);
  80. // 串口通信设备
  81. if( ((CButton *)GetDlgItem(IDC_RADIO_COMM))->GetCheck() )
  82. {
  83. m_nCommuniteTypeOld = 0;
  84. }
  85. // 网络通信设备
  86. else if( ((CButton *)GetDlgItem(IDC_RADIO_NET))->GetCheck() )
  87. {
  88. m_nCommuniteTypeOld = 1;
  89. }
  90. //
  91. UpdateData(TRUE);
  92. if( m_operation == NEW )
  93. {
  94. m_nCommuniteType = 0;
  95. m_nProtocolType = 0;
  96. m_nDevEnable = 1;
  97. }
  98. UpdateData(FALSE);
  99. ListDeviceTypes();
  100. return TRUE; // return TRUE unless you set the focus to a control
  101. // 异常: OCX 属性页应返回 FALSE
  102. }
  103. void CDlgDevice::ListDeviceTypes(HTREEITEM hItemParent)
  104. {
  105. HTREEITEM hItemDrive, hItemFactory, hItemType;
  106. int nDevDriveCount, nDevFactoryCount, nDevTypeCount;
  107. list<string> listDriveID, listDriveName;
  108. list<string> listFactoryID, listFactoryName;
  109. list<string> listDevTypeID, listDevTypeName;
  110. list<string>::iterator itDriverID, itDriverName;
  111. list<string>::iterator itFactoryID, itFactoryName;
  112. list<string>::iterator itDevTypeID, itDevTypeName;
  113. //获取设备驱动列表
  114. nDevDriveCount = CDBInterface::GetInstancePtr()->GetDevDriveList(listDriveID, listDriveName);
  115. if( nDevDriveCount <= 0 ) return;
  116. for( itDriverID = listDriveID.begin(), itDriverName = listDriveName.begin();
  117. itDriverID != listDriveID.end() && itDriverName != listDriveName.end();
  118. itDriverID++, itDriverName++)
  119. {
  120. hItemDrive = m_ctrlDevFolder.InsertItem((*itDriverName).c_str(), 0, 1, hItemParent);
  121. //获取设备驱动对应的厂家列表
  122. nDevFactoryCount = CDBInterface::GetInstancePtr()->GetDevFactoryList( atoi((*itDriverID).c_str()), listFactoryID, listFactoryName);
  123. if( nDevFactoryCount <= 0 )
  124. {
  125. continue;
  126. }
  127. for( itFactoryID = listFactoryID.begin(), itFactoryName = listFactoryName.begin();
  128. itFactoryID != listFactoryID.end() && itFactoryName != listFactoryName.end();
  129. itFactoryID++, itFactoryName++)
  130. {
  131. hItemFactory = m_ctrlDevFolder.InsertItem((*itFactoryName).c_str(), 0, 1, hItemDrive);
  132. //获取指定厂家设备型号列表
  133. nDevTypeCount = CDBInterface::GetInstancePtr()->GetDevTypeList(
  134. atoi((*itDriverID).c_str()),
  135. atoi((*itFactoryID).c_str()),
  136. listDevTypeID, listDevTypeName );
  137. if( nDevTypeCount <= 0 )
  138. {
  139. continue;
  140. }
  141. for( itDevTypeID = listDevTypeID.begin(), itDevTypeName = listDevTypeName.begin();
  142. itDevTypeID != listDevTypeID.end() && itDevTypeName != listDevTypeName.end();
  143. itDevTypeID++, itDevTypeName++)
  144. {
  145. hItemType = m_ctrlDevFolder.InsertItem((*itDevTypeName).c_str(), 2, 2, hItemFactory);
  146. if( strcmp(m_strDevDriveName, (*itDriverName).c_str()) == 0 &&
  147. strcmp(m_strDevFactoryName, (*itFactoryName).c_str()) == 0 &&
  148. strcmp(m_strDevTypeName, (*itDevTypeName).c_str()) == 0 )
  149. {
  150. m_ctrlDevFolder.SelectItem( hItemType );
  151. }
  152. }
  153. for( itDevTypeID = listDevTypeID.begin(), itDevTypeName = listDevTypeName.begin();
  154. itDevTypeID != listDevTypeID.end() && itDevTypeName != listDevTypeName.end(); )
  155. {
  156. listDevTypeID.erase( itDevTypeID++ );
  157. listDevTypeName.erase( itDevTypeName++ );
  158. }
  159. }
  160. for( itFactoryID = listFactoryID.begin(), itFactoryName = listFactoryName.begin();
  161. itFactoryID != listFactoryID.end() && itFactoryName != listFactoryName.end(); )
  162. {
  163. listFactoryID.erase( itFactoryID++ );
  164. listFactoryName.erase( itFactoryName++ );
  165. }
  166. }
  167. for( itDriverID = listFactoryID.begin(), itDriverName = listFactoryName.begin();
  168. itDriverID != listFactoryID.end() && itDriverName != listFactoryName.end(); )
  169. {
  170. listDriveID.erase( itDriverID++ );
  171. listDriveName.erase( itDriverName++ );
  172. }
  173. }
  174. BOOL CDlgDevice::OnSetActive()
  175. {
  176. // TODO: 在此添加专用代码和/或调用基类
  177. //CPropertySheet* pParent=(CPropertySheet*)GetParent();
  178. //pParent->GetDlgItem(ID_WIZNEXT)->EnableWindow(FALSE);
  179. //pParent->GetDlgItem(ID_WIZBACK)->ShowWindow(FALSE);
  180. CPropertySheet* pParent=(CPropertySheet*)GetParent();
  181. pParent->SetWizardButtons(PSWIZB_NEXT);
  182. return CPropertyPage::OnSetActive();
  183. }
  184. void CDlgDevice::SelChangedTreeDeviceFolder(HTREEITEM hItemSelect)
  185. {
  186. CPropertySheet* pParent=(CPropertySheet*)GetParent();
  187. CString strSelect = m_ctrlDevFolder.GetItemText( hItemSelect );
  188. CString strDevDriveName = "", strDevFactoryName = "", strDevTypeName = "";
  189. HTREEITEM hItemParent = m_ctrlDevFolder.GetParentItem( hItemSelect );
  190. if( hItemParent != NULL )
  191. {
  192. strDevFactoryName = m_ctrlDevFolder.GetItemText( hItemParent );
  193. m_strDevFactoryName = strDevFactoryName;
  194. hItemParent = m_ctrlDevFolder.GetParentItem( hItemParent );
  195. if( hItemParent != NULL )
  196. {
  197. strDevTypeName = strSelect;
  198. m_strDevTypeName = strDevTypeName;
  199. strDevDriveName = m_ctrlDevFolder.GetItemText( hItemParent );
  200. m_strDevDriveName = strDevDriveName;
  201. pParent->GetDlgItem(ID_WIZNEXT)->EnableWindow(TRUE);
  202. }
  203. else
  204. {
  205. pParent->GetDlgItem(ID_WIZNEXT)->EnableWindow(FALSE);
  206. return;
  207. }
  208. }
  209. else
  210. {
  211. pParent->GetDlgItem(ID_WIZNEXT)->EnableWindow(FALSE);
  212. return;
  213. }
  214. CHAR szDevDriveID[MAX_ID + 1] = {0};
  215. CHAR szDevFactoryID[MAX_ID + 1] = {0};
  216. CHAR szDevTypeID[MAX_ID + 1] = {0};
  217. //获取设备驱动ID
  218. CDBInterface::GetInstancePtr()->GetDevDriveID((char *)(LPCTSTR)strDevDriveName, szDevDriveID);
  219. m_nDevDriverID = atoi(szDevDriveID);
  220. //获取设备厂家ID
  221. if( m_nDevDriverID > 0 )
  222. {
  223. CDBInterface::GetInstancePtr()->GetDevFactoryID(m_nDevDriverID, (char *)(LPCTSTR)strDevFactoryName, szDevFactoryID);
  224. m_nDevFactoryID = atoi(szDevFactoryID);
  225. }
  226. //获取设备型号ID
  227. if( m_nDevDriverID > 0 && m_nDevFactoryID > 0 )
  228. {
  229. CDBInterface::GetInstancePtr()->GetDevTypeID(m_nDevDriverID, m_nDevFactoryID, (char *)(LPCTSTR)strDevTypeName, szDevTypeID);
  230. m_nDevTypeID = atoi(szDevTypeID);
  231. }
  232. }
  233. void CDlgDevice::OnTvnSelchangedTreeDeviceFolder(NMHDR *pNMHDR, LRESULT *pResult)
  234. {
  235. LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
  236. // TODO: 在此添加控件通知处理程序代码
  237. HTREEITEM hItemSelect = pNMTreeView->itemNew.hItem;
  238. SelChangedTreeDeviceFolder( hItemSelect );
  239. *pResult = 0;
  240. }
  241. HTREEITEM CDlgDevice::FindTreeNode( CTreeCtrl *pTree, HTREEITEM HItem, HTREEITEM HItemSelect )
  242. {
  243. HTREEITEM HCurrent;
  244. HCurrent = pTree->GetChildItem( HItem );
  245. CString strTemp;
  246. while( HCurrent != NULL )
  247. {
  248. if( HCurrent == HItemSelect )
  249. {
  250. break;
  251. }
  252. HCurrent = pTree->GetNextSiblingItem(HCurrent);
  253. }
  254. return HCurrent;
  255. }
  256. LRESULT CDlgDevice::OnWizardNext()
  257. {
  258. // TODO: 在此添加专用代码和/或调用基类
  259. CMainFrame *pMainFrm=(CMainFrame*)AfxGetApp()->m_pMainWnd;
  260. CString strUID;
  261. CHAR szDevNum[MAX_ID + 1] = {0};
  262. strUID.Format("%d.%d.%d", m_nDevDriverID, m_nDevFactoryID, m_nDevTypeID);
  263. CDBInterface::GetInstancePtr()->FindDevInfoByUID((char *)(LPCTSTR)strUID, szDevNum);
  264. CString strDllName;
  265. GetDlgItem(IDC_EDT_DLL_NAME)->GetWindowText(strDllName);
  266. strDllName.MakeLower();
  267. if( strDllName=="stu2000.dll" )
  268. m_bIsStu2000 = true;
  269. else
  270. m_bIsStu2000 = false;
  271. if( m_operation == CDlgDevice::NEW )
  272. {
  273. pMainFrm->m_wndComport.m_strUID.Format("%d.%d.%d.%d", m_nDevDriverID, m_nDevFactoryID, m_nDevTypeID, atoi(szDevNum) + 1);
  274. pMainFrm->m_wndComport.m_nTimeout = 10000;
  275. pMainFrm->m_wndComport.m_nDevAddr = 1;
  276. pMainFrm->m_wndComport.m_nCommPort = 1;
  277. pMainFrm->m_wndComport.m_nRate = 3;
  278. pMainFrm->m_wndComport.m_nCheckCode = 0;
  279. pMainFrm->m_wndComport.m_nDataBit = 1;
  280. pMainFrm->m_wndComport.m_nStopBit = 0;
  281. pMainFrm->m_wndComport.m_nNetIP = htonl(inet_addr("192.168.1.111"));
  282. pMainFrm->m_wndComport.m_nNetPort = 5555;
  283. // 串口通信设备
  284. if( ((CButton *)GetDlgItem(IDC_RADIO_COMM))->GetCheck() )
  285. {
  286. pMainFrm->m_wndComport.m_nCommunicateMode = 0;
  287. }
  288. // 网络通信设备
  289. else if( ((CButton *)GetDlgItem(IDC_RADIO_NET))->GetCheck() )
  290. {
  291. pMainFrm->m_wndComport.m_nCommunicateMode = 1;
  292. }
  293. if( pMainFrm->m_wndComport )
  294. pMainFrm->m_wndComport.ChangeCtrlState( );
  295. }
  296. else if( m_operation == CDlgDevice::EDIT )
  297. {
  298. CString str1,str2;
  299. int nPos=0;
  300. for( int i=pMainFrm->m_wndComport.m_strUID.GetLength()-1;i>0;i-- )
  301. {
  302. if( pMainFrm->m_wndComport.m_strUID[i]=='.' )
  303. {
  304. str1 = pMainFrm->m_wndComport.m_strUID.Left( i );
  305. break;
  306. }
  307. }
  308. str2.Format("%d.%d.%d", m_nDevDriverID, m_nDevFactoryID, m_nDevTypeID );
  309. CString sTemp;
  310. sTemp.Format("%d.%d.%d.%d", m_nDevDriverID, m_nDevFactoryID, m_nDevTypeID, atoi(szDevNum) + 1);
  311. //if( pMainFrm->m_wndComport )
  312. {
  313. m_sUidOld = pMainFrm->m_wndComport.m_strUID;
  314. pMainFrm->m_wndComport.m_strUID2 = sTemp;
  315. if( str1!=str2 )
  316. pMainFrm->m_wndComport.m_strUID = sTemp;
  317. }
  318. //pMainFrm->m_wndComport.m_nCommunicateMode = m_nCommuniteType;
  319. // 串口通信设备
  320. if( ((CButton *)GetDlgItem(IDC_RADIO_COMM))->GetCheck() )
  321. {
  322. pMainFrm->m_wndComport.m_nCommunicateMode = 0;
  323. }
  324. // 网络通信设备
  325. else if( ((CButton *)GetDlgItem(IDC_RADIO_NET))->GetCheck() )
  326. {
  327. pMainFrm->m_wndComport.m_nCommunicateMode = 1;
  328. }
  329. //
  330. m_nCommuniteType = pMainFrm->m_wndComport.m_nCommunicateMode;
  331. // 网络设备
  332. if( m_nCommuniteType == 1 )
  333. {
  334. if( m_nCommuniteTypeOld==1 )
  335. {
  336. pMainFrm->m_wndComport.m_nTimeout = 10000;
  337. if( !m_bIsStu2000 )
  338. pMainFrm->m_wndComport.m_nDevAddr = 1;
  339. pMainFrm->m_wndComport.m_nCommPort = 1;
  340. pMainFrm->m_wndComport.m_nRate = 3;
  341. pMainFrm->m_wndComport.m_nCheckCode = 0;
  342. pMainFrm->m_wndComport.m_nDataBit = 1;
  343. pMainFrm->m_wndComport.m_nStopBit = 0;
  344. }
  345. if( m_nCommuniteTypeOld==0 )//&& pMainFrm->m_wndComport )
  346. {
  347. pMainFrm->m_wndComport.m_nNetIP = htonl(inet_addr("192.168.1.111"));
  348. pMainFrm->m_wndComport.m_nNetPort = 5555;
  349. }
  350. }
  351. else if( m_nCommuniteType == 0 )// 串口通信设备
  352. {
  353. if( m_nCommuniteTypeOld==1 )
  354. {
  355. pMainFrm->m_wndComport.m_nTimeout = 10000;
  356. pMainFrm->m_wndComport.m_nDevAddr = 1;
  357. pMainFrm->m_wndComport.m_nCommPort = 1;
  358. pMainFrm->m_wndComport.m_nRate = 3;
  359. pMainFrm->m_wndComport.m_nCheckCode = 0;
  360. pMainFrm->m_wndComport.m_nDataBit = 1;
  361. pMainFrm->m_wndComport.m_nStopBit = 0;
  362. }
  363. }
  364. if( pMainFrm->m_wndComport )
  365. pMainFrm->m_wndComport.ChangeCtrlState( );
  366. }
  367. CString strDriverName;
  368. GetDlgItem(IDC_EDT_DLL_NAME)->GetWindowText(strDriverName);
  369. if( !strDriverName.Compare("") || strUID=="-1.-1.-1" )
  370. {
  371. MessageBox(g_strSelDevice,g_strTip,MB_ICONWARNING);
  372. return TRUE;
  373. }
  374. return CPropertyPage::OnWizardNext();
  375. }
  376. void CDlgDevice::OnBnClickedBtnSelDll()
  377. {
  378. // TODO: 在此添加控件通知处理程序代码
  379. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  380. CString strFindFile = "";
  381. LPSTR lpszFile = (char*)(const char*)strFindFile;
  382. LPSTR lpszFilter = "Module File(*.dll)|*.dll||";
  383. CFileDialog dlg(TRUE,".dll",lpszFile,dwFlags,lpszFilter);
  384. if(dlg.DoModal() == IDOK)
  385. {
  386. strFindFile = dlg.GetFileName();
  387. GetDlgItem(IDC_EDT_DLL_NAME)->SetWindowText( strFindFile );
  388. }
  389. }
  390. void CDlgDevice::OnBnClickedBtnSelIni()
  391. {
  392. // TODO: 在此添加控件通知处理程序代码
  393. DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  394. CString strFindFile = "";
  395. LPSTR lpszFile = (char*)(const char*)strFindFile;
  396. LPSTR lpszFilter = "Module File(*.ini)|*.ini||";
  397. CFileDialog dlg(TRUE,".ini",lpszFile,dwFlags,lpszFilter);
  398. if(dlg.DoModal() == IDOK)
  399. {
  400. strFindFile = dlg.GetFileName();
  401. GetDlgItem(IDC_EDT_INI_NAME)->SetWindowText( strFindFile );
  402. }
  403. }