GpsSCDoc.cpp
上传用户:yffx2008
上传日期:2014-10-12
资源大小:12414k
文件大小:10k
源码类别:

交通/航空行业

开发平台:

Visual C++

  1. // GpsSCDoc.cpp : implementation of the CGpsSCDoc class
  2. //
  3. #include "stdafx.h"
  4. #include "GpsSC.h"
  5. #include "GpsSCDoc.h"
  6. #include "ProcSM.h"
  7. #include "SetCommDlg.h"
  8. #include "ShowAllClientDlg.h"
  9. #include "AddClientinfoDlg.h"
  10. #include "ClientinfoSet.h"
  11. #include "VehicleMessageSet.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. extern CGpsSCApp theApp;       // 在此引用应用类中的theApp来获取库连接指针
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CGpsSCDoc
  20. IMPLEMENT_DYNCREATE(CGpsSCDoc, CDocument)
  21. BEGIN_MESSAGE_MAP(CGpsSCDoc, CDocument)
  22. //{{AFX_MSG_MAP(CGpsSCDoc)
  23. ON_UPDATE_COMMAND_UI(ID_CCONNET_COMM, OnUpdateCconnetComm)
  24. ON_COMMAND(ID_CDISCONNET_COMM, OnCdisconnetComm)
  25. ON_UPDATE_COMMAND_UI(ID_CDISCONNET_COMM, OnUpdateCdisconnetComm)
  26. ON_COMMAND(ID_CSET_COMM, OnCsetComm)
  27. ON_UPDATE_COMMAND_UI(ID_CSET_COMM, OnUpdateCsetComm)
  28. ON_COMMAND(IDD_READONESM, OnReadonesm)
  29. ON_COMMAND(IDD_ADDCLIENTD, OnAddclientd)
  30. ON_COMMAND(IDD_CLIENTLIST, OnClientlist)
  31. ON_UPDATE_COMMAND_UI(IDD_CLIENTLIST, OnUpdateClientlist)
  32. ON_UPDATE_COMMAND_UI(IDD_ADDCLIENTD, OnUpdateAddclientd)
  33. //}}AFX_MSG_MAP
  34. END_MESSAGE_MAP()
  35. /////////////////////////////////////////////////////////////////////////////
  36. // CGpsSCDoc construction/destruction
  37. CGpsSCDoc::CGpsSCDoc()
  38. {
  39. // TODO: add one-time construction code here
  40. m_bConnected = FALSE;
  41. m_pThread  = NULL;
  42. m_sPort  = theApp.m_strPort;
  43. m_nBaud  = atol(theApp.m_strBaud);
  44. m_nDataBits  = atol(theApp.m_strDataBits);
  45. m_nParity  = theApp.m_nParity;
  46. m_nStopBits  = theApp.m_nStopBit;
  47. }
  48. CGpsSCDoc::~CGpsSCDoc()
  49. {
  50. if(m_bConnected)
  51. {
  52. CloseConnection();
  53. }
  54. if(m_hPostMsgEvent)
  55. {
  56. CloseHandle(m_hPostMsgEvent);
  57. }
  58. if(m_osRead.hEvent)
  59. {
  60. CloseHandle(m_osRead.hEvent);
  61. }
  62. if(m_osWrite.hEvent)
  63. {
  64. CloseHandle(m_osWrite.hEvent);
  65. }
  66. }
  67. BOOL CGpsSCDoc::OnNewDocument()
  68. {
  69. if (!CDocument::OnNewDocument())
  70. return FALSE;
  71. // TODO: add reinitialization code here
  72. // (SDI documents will reuse this document)
  73. if (!CDocument::OnNewDocument())
  74. {
  75. return FALSE;
  76. }
  77. SetTitle("广州海特天高信息有限公司");
  78. // TODO: add reinitialization code here
  79. // (SDI documents will reuse this document)
  80. // 为WM_COMMNOTIFY消息创建事件对象,手工重置,初始化为有信号的
  81. if((m_hPostMsgEvent=CreateEvent(NULL, TRUE, TRUE, NULL))==NULL)
  82. return FALSE;
  83. memset(&m_osRead, 0, sizeof(OVERLAPPED));
  84. memset(&m_osWrite, 0, sizeof(OVERLAPPED));
  85. // 为重叠读创建事件对象,手工重置,初始化为无信号的
  86. if((m_osRead.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
  87. return FALSE;
  88. // 为重叠写创建事件对象,手工重置,初始化为无信号的
  89. if((m_osWrite.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL))==NULL)
  90. return FALSE;
  91. return TRUE;
  92. }
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CGpsSCDoc serialization
  95. void CGpsSCDoc::Serialize(CArchive& ar)
  96. {
  97. if (ar.IsStoring())
  98. {
  99. // TODO: add storing code here
  100. }
  101. else
  102. {
  103. // TODO: add loading code here
  104. }
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CGpsSCDoc diagnostics
  108. #ifdef _DEBUG
  109. void CGpsSCDoc::AssertValid() const
  110. {
  111. CDocument::AssertValid();
  112. }
  113. void CGpsSCDoc::Dump(CDumpContext& dc) const
  114. {
  115. CDocument::Dump(dc);
  116. }
  117. #endif //_DEBUG
  118. /////////////////////////////////////////////////////////////////////////////
  119. // CGpsSCDoc commands
  120. // 打开并配置串行口,建立工作者线程
  121. BOOL CGpsSCDoc::OpenConnection()
  122. {
  123. COMMTIMEOUTS TimeOuts;
  124. POSITION firstViewPos;
  125. CView *pView;
  126. firstViewPos = GetFirstViewPosition();
  127. pView = GetNextView(firstViewPos);
  128. m_hTermWnd = pView->GetSafeHwnd();
  129. if(m_bConnected)
  130. {
  131. return FALSE;
  132. }
  133. m_hCom=CreateFile(m_sPort, GENERIC_READ | GENERIC_WRITE, 0, NULL,
  134. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
  135. NULL); // 重叠方式
  136. if(m_hCom==INVALID_HANDLE_VALUE)
  137. {
  138. return FALSE;
  139. }
  140. SetupComm(m_hCom,MAXBLOCK,MAXBLOCK);
  141. SetCommMask(m_hCom, EV_RXCHAR);
  142. // 把间隔超时设为最大,把总超时设为0将导致ReadFile立即返回并完成操作
  143. TimeOuts.ReadIntervalTimeout=MAXDWORD; 
  144. TimeOuts.ReadTotalTimeoutMultiplier=0; 
  145. TimeOuts.ReadTotalTimeoutConstant=0; 
  146. /* 设置写超时以指定WriteComm成员函数中的GetOverlappedResult函数的等待时间*/
  147. TimeOuts.WriteTotalTimeoutMultiplier=50;
  148. TimeOuts.WriteTotalTimeoutConstant=2000;
  149. SetCommTimeouts(m_hCom, &TimeOuts);
  150. if(ConfigConnection())
  151. {
  152. m_pThread=AfxBeginThread(CommProc, this, THREAD_PRIORITY_NORMAL, 
  153. 0, CREATE_SUSPENDED, NULL); // 创建并挂起线程
  154. if(m_pThread==NULL)
  155. {
  156. CloseHandle(m_hCom);
  157. return FALSE;
  158. }
  159. else
  160. {
  161. m_bConnected=TRUE;
  162. m_pThread->ResumeThread(); // 恢复线程运行
  163. }
  164. }
  165. else
  166. {
  167. CloseHandle(m_hCom);
  168. return FALSE;
  169. }
  170. return TRUE;
  171. }
  172. //关闭连接,关闭工作线程
  173. void CGpsSCDoc::CloseConnection()
  174. {
  175. if(!m_bConnected)
  176. {
  177. return;
  178. }
  179. m_bConnected=FALSE;
  180. //结束CommProc线程中WaitSingleObject函数的等待
  181. SetEvent(m_hPostMsgEvent); 
  182. //结束CommProc线程中WaitCommEvent的等待
  183. SetCommMask(m_hCom, 0); 
  184. //等待辅助线程终止
  185. WaitForSingleObject(m_pThread->m_hThread, INFINITE);
  186. m_pThread=NULL;
  187. CloseHandle(m_hCom);
  188. }
  189. BOOL CGpsSCDoc::ConfigConnection()
  190. {
  191. DCB dcb;
  192. if(!GetCommState(m_hCom, &dcb))
  193. {
  194. return FALSE;
  195. }
  196. dcb.fBinary=TRUE;
  197. dcb.BaudRate = m_nBaud; // 数据传输速率
  198. dcb.ByteSize = m_nDataBits; // 每字节位数
  199. dcb.fParity = TRUE;
  200. switch(m_nParity) // 校验设置
  201. {
  202. case 0: 
  203. dcb.Parity=NOPARITY;
  204. break;
  205. case 1: 
  206. dcb.Parity=EVENPARITY;
  207. break;
  208. case 2: 
  209. dcb.Parity=ODDPARITY;
  210. break;
  211. default:;
  212. }
  213. switch(m_nStopBits) // 停止位
  214. {
  215. case 0: 
  216. dcb.StopBits=ONESTOPBIT;
  217. break;
  218. case 1: 
  219. dcb.StopBits=ONE5STOPBITS;
  220. break;
  221. case 2:
  222. dcb.StopBits=TWOSTOPBITS;
  223. break;
  224. default:;
  225. }
  226. /*
  227. // 硬件流控制设置
  228. dcb.fOutxCtsFlow = 1;
  229. dcb.fRtsControl = 1?RTS_CONTROL_HANDSHAKE:RTS_CONTROL_ENABLE;
  230. // XON/XOFF流控制设置
  231. dcb.fInX = dcb.fOutX = 1;
  232. dcb.XonChar = XON;
  233. dcb.XoffChar = XOFF;
  234. dcb.XonLim = 50;
  235. dcb.XoffLim = 50;
  236. */
  237. return SetCommState(m_hCom, &dcb);
  238. }
  239. DWORD CGpsSCDoc::ReadComm(char *buff, DWORD dwLength)
  240. {
  241. DWORD length=0;
  242. COMSTAT ComStat;
  243. DWORD dwErrorFlags;
  244. ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
  245. length=min(dwLength, ComStat.cbInQue);
  246. ReadFile(m_hCom,buff,length,&length,&m_osRead);
  247. return length;
  248. }
  249. DWORD CGpsSCDoc::WriteComm(char *buff, DWORD dwLength)
  250. {
  251. BOOL fState;
  252. DWORD length=dwLength;
  253. COMSTAT ComStat;
  254. DWORD dwErrorFlags;
  255. ClearCommError(m_hCom,&dwErrorFlags,&ComStat);
  256. fState=WriteFile(m_hCom,buff,length,&length,&m_osWrite);
  257. if(!fState)
  258. {
  259. if(GetLastError()==ERROR_IO_PENDING)
  260. {
  261. GetOverlappedResult(m_hCom,&m_osWrite,&length,TRUE); // 等待
  262. }
  263. else
  264. {
  265. length=0;
  266. }
  267. }
  268. return length;
  269. }
  270. // 工作者线程,负责监视串行口
  271. UINT CommProc(LPVOID pParam)
  272. {
  273. OVERLAPPED os;
  274. DWORD dwMask, dwTrans;
  275. COMSTAT ComStat;
  276. DWORD dwErrorFlags;
  277. CGpsSCDoc *pDoc=(CGpsSCDoc*)pParam;
  278. memset(&os, 0, sizeof(OVERLAPPED));
  279. os.hEvent=CreateEvent(NULL, TRUE, FALSE, NULL);
  280. if(os.hEvent==NULL)
  281. {
  282. AfxMessageBox("Can't create event object!");
  283. return (UINT)-1;
  284. }
  285. while(pDoc->m_bConnected)
  286. {
  287. ClearCommError(pDoc->m_hCom,&dwErrorFlags,&ComStat);
  288. if(ComStat.cbInQue)
  289. {
  290. // 无限等待WM_COMMNOTIFY消息被处理完
  291. WaitForSingleObject(pDoc->m_hPostMsgEvent, INFINITE);
  292. ResetEvent(pDoc->m_hPostMsgEvent);
  293. // 通知视图
  294. PostMessage(pDoc->m_hTermWnd, WM_COMMNOTIFY, EV_RXCHAR, 0);
  295. continue;
  296. }
  297. dwMask=0;
  298. if(!WaitCommEvent(pDoc->m_hCom, &dwMask, &os))
  299. { // 重叠操作
  300. if(GetLastError()==ERROR_IO_PENDING)
  301. {
  302. // 无限等待重叠操作结果
  303. GetOverlappedResult(pDoc->m_hCom, &os, &dwTrans, TRUE);
  304. }
  305. else
  306. {
  307. CloseHandle(os.hEvent);
  308. return (UINT)-1;
  309. }
  310. }
  311. }
  312. CloseHandle(os.hEvent);
  313. return 0;
  314. }
  315. //inite the Wavecom
  316. BOOL CGpsSCDoc::OnConnetComm() 
  317. {
  318. // TODO: Add your command handler code here
  319. if(!OpenConnection())
  320. {
  321. AfxMessageBox("不能打开串行口!");
  322. return FALSE;
  323. }
  324. return TRUE;
  325. }
  326. void CGpsSCDoc::OnUpdateCconnetComm(CCmdUI* pCmdUI) 
  327. {
  328. // TODO: Add your command update UI handler code here
  329. pCmdUI->Enable(!m_bConnected);
  330. }
  331. void CGpsSCDoc::OnCdisconnetComm() 
  332. {
  333. // TODO: Add your command handler code here
  334. CloseConnection();
  335. }
  336. void CGpsSCDoc::OnUpdateCdisconnetComm(CCmdUI* pCmdUI) 
  337. {
  338. // TODO: Add your command update UI handler code here
  339. pCmdUI->Enable(m_bConnected);
  340. }
  341. void CGpsSCDoc::OnCsetComm() 
  342. {
  343. // TODO: Add your command handler code here
  344. CSetCommDlg dlg;
  345. CString str;
  346. dlg.m_bConnected = m_bConnected;
  347. dlg.m_sPort  = theApp.m_strPort;
  348. dlg.m_sBaud  = theApp.m_strBaud;
  349. dlg.m_sDataBits  = theApp.m_strDataBits;
  350. dlg.m_nParity  = theApp.m_nParity;
  351. dlg.m_nStopBits  = theApp.m_nStopBit;
  352. if(dlg.DoModal() == IDOK)
  353. {
  354. m_sPort = dlg.m_sPort;
  355. m_nBaud = atoi(dlg.m_sBaud);
  356. m_nDataBits = atoi(dlg.m_sDataBits);
  357. m_nParity = dlg.m_nParity;
  358. m_nStopBits = dlg.m_nStopBits;
  359. theApp.m_strPort = dlg.m_sPort;
  360. theApp.m_strBaud = dlg.m_sBaud;
  361. theApp.m_strDataBits = dlg.m_sDataBits;
  362. theApp.m_nParity = dlg.m_nParity;
  363. theApp.m_nStopBit = dlg.m_nStopBits;
  364. if(m_bConnected)
  365. {
  366. if(!ConfigConnection())
  367. {
  368. AfxMessageBox( "不能设置串行口!");
  369. }
  370. }
  371. }
  372. }
  373. void CGpsSCDoc::OnUpdateCsetComm(CCmdUI* pCmdUI) 
  374. {
  375. // TODO: Add your command update UI handler code here
  376. }
  377. void CGpsSCDoc::OnReadonesm() 
  378. {
  379. WriteComm("AT+CMGR=1r",10);
  380. }
  381. void CGpsSCDoc::OnAddclientd() 
  382. {
  383. CAddClientinfoDlg dlg;
  384. dlg.DoModal();
  385. }
  386. void CGpsSCDoc::OnClientlist() 
  387. {
  388. CShowAllClientDlg dlg;
  389. dlg.DoModal();
  390. }
  391. BOOL CGpsSCDoc::CanCloseFrame(CFrameWnd* pFrame) 
  392. {
  393. // TODO: Add your specialized code here and/or call the base class
  394. return CDocument::CanCloseFrame(pFrame);
  395. CVehicleMessageSet messageSet;
  396. CClientinfoSet clientinfoSet;
  397. try
  398. {
  399. messageSet.Close();
  400. clientinfoSet.Close();
  401. }
  402. catch(CDBException *Exp)
  403. {
  404. AfxMessageBox(Exp->m_strError);
  405. messageSet.Close();
  406. clientinfoSet.Close();
  407. }
  408. }
  409. void CGpsSCDoc::OnUpdateClientlist(CCmdUI* pCmdUI) 
  410. {
  411. // TODO: Add your command update UI handler code here
  412. pCmdUI->Enable(theApp.m_bUserType);
  413. }
  414. void CGpsSCDoc::OnUpdateAddclientd(CCmdUI* pCmdUI) 
  415. {
  416. // TODO: Add your command update UI handler code here
  417. pCmdUI->Enable(theApp.m_bUserType);
  418. }