PeugeotView.cpp
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:8k
源码类别:

Internet/IE编程

开发平台:

Visual C++

  1. // PeugeotView.cpp : implementation of the CPeugeotView class
  2. //
  3. #include "stdafx.h"
  4. #include "Peugeot.h"
  5. #include "PeugeotDoc.h"
  6. #include "PeugeotView.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CPeugeotView
  14. IMPLEMENT_DYNCREATE(CPeugeotView, CEditView)
  15. BEGIN_MESSAGE_MAP(CPeugeotView, CEditView)
  16. //{{AFX_MSG_MAP(CPeugeotView)
  17. ON_WM_TIMER()
  18. //}}AFX_MSG_MAP
  19.     ON_MESSAGE(WM_COMMNOTIFY,OnCommNotify)
  20.     
  21. ON_MESSAGE(WM_TCP_SERVER_CONNECTED,OnTCPSrvConnected)
  22.     ON_MESSAGE(WM_TCP_SERVER_CLOSED,OnTCPSrvClosed)
  23.     ON_MESSAGE(WM_TCP_SERVER_RECEIVE,OnTCPSrvRec) 
  24. ON_MESSAGE(WM_TCP_CLIENT_CONNECTED,OnTCPCltConnected)
  25. ON_MESSAGE(WM_TCP_CLIENT_CLOSED,OnTCPCltClosed)
  26.     ON_MESSAGE(WM_TCP_CLIENT_RECEIVE,OnTCPCltRec)
  27.     ON_MESSAGE(WM_UDP_RECEIVE,OnUDPRec)
  28. // Standard printing commands
  29. ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
  30. ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
  31. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
  32. END_MESSAGE_MAP()
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CPeugeotView construction/destruction
  35. CPeugeotView::CPeugeotView()
  36. {
  37. m_pPrtObj    = NULL;
  38.     m_pSocketObj = NULL;
  39. }
  40. CPeugeotView::~CPeugeotView()
  41. {
  42. }
  43. BOOL CPeugeotView::PreCreateWindow(CREATESTRUCT& cs)
  44. {
  45. // TODO: Modify the Window class or styles here by modifying
  46. //  the CREATESTRUCT cs
  47. BOOL bPreCreated = CEditView::PreCreateWindow(cs);
  48. cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
  49. return bPreCreated;
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CPeugeotView drawing
  53. void CPeugeotView::OnDraw(CDC* pDC)
  54. {
  55. CPeugeotDoc* pDoc = GetDocument();
  56. ASSERT_VALID(pDoc);
  57. // TODO: add draw code for native data here
  58. }
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CPeugeotView printing
  61. BOOL CPeugeotView::OnPreparePrinting(CPrintInfo* pInfo)
  62. {
  63. // default preparation
  64. return CEditView::OnPreparePrinting(pInfo);
  65. }
  66. void CPeugeotView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
  67. {
  68. // TODO: add extra initialization before printing
  69. CEditView::OnBeginPrinting(pDC, pInfo);
  70. }
  71. void CPeugeotView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
  72. {
  73. // TODO: add cleanup after printing
  74.    CEditView::OnEndPrinting(pDC, pInfo);
  75. }
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CPeugeotView diagnostics
  78. #ifdef _DEBUG
  79. void CPeugeotView::AssertValid() const
  80. {
  81. CEditView::AssertValid();
  82. }
  83. void CPeugeotView::Dump(CDumpContext& dc) const
  84. {
  85. CEditView::Dump(dc);
  86. }
  87. CPeugeotDoc* CPeugeotView::GetDocument() // non-debug version is inline
  88. {
  89. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPeugeotDoc)));
  90. return (CPeugeotDoc*)m_pDocument;
  91. }
  92. #endif //_DEBUG
  93. /////////////////////////////////////////////////////////////////////////////
  94. // CPeugeotView message handlers
  95. void CPeugeotView::OnTimer(UINT nIDEvent) 
  96. {
  97. m_pPrtObj->MasterSend(nIDEvent);
  98. CView::OnTimer(nIDEvent);
  99. }
  100. BOOL CPeugeotView::Send(BYTE* pBuff, DWORD dwBytesToSend, DWORD& dwBytesSended)
  101. {
  102. CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  103.     ASSERT_VALID(pDoc);
  104.     BOOL IsDataSended = false;  
  105. if (pDoc->m_CmmPortType == pDoc->CommPort)
  106. //串口发送数据
  107. IsDataSended = pDoc->m_ComObj.Send(pBuff, dwBytesToSend, dwBytesSended);
  108. else
  109. { //网口发送数据CFont
  110. if (pDoc->m_NetParam.TCPPrtType == pDoc->m_NetParam.UDP) //UDP方式,现只考虑往指定地址、端口发数据
  111.             dwBytesSended = m_pSocketObj->SendTo(pBuff,dwBytesToSend,pDoc->m_NetParam.RemoteIPPort,pDoc->m_NetParam.RemoteIPAddr);
  112. else
  113. dwBytesSended = m_pSocketObj->Send(pBuff, dwBytesToSend);
  114. if (dwBytesSended >0)
  115.             IsDataSended = true;
  116. }
  117. return IsDataSended;
  118. }
  119. LRESULT CPeugeotView::OnCommNotify(WPARAM wParam,LPARAM lParam)
  120. {
  121. CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  122.     ASSERT_VALID(pDoc);
  123.     BYTE recbyte = 0x00;
  124. if (pDoc->m_ComObj.Receive(&recbyte,1)) 
  125.     {
  126.   if (pDoc->m_MasterFlag)
  127.      m_pPrtObj->MasterRec(recbyte);    //主站接收数据
  128.   else
  129.      m_pPrtObj->SlaveRec(recbyte);     //从站接收数据
  130. }
  131. return 0L;
  132. }
  133. //视图显示通讯报文
  134. void CPeugeotView::DisplayMsg(CString dspstr)
  135. {
  136. CMainFrame* pMain = (CMainFrame*)AfxGetApp()->m_pMainWnd; 
  137. if (!pMain->IsReflashMsg)
  138. return;
  139. CEdit& edit = GetEditCtrl();
  140. int iTextLen;
  141. iTextLen = edit.GetWindowTextLength();
  142. edit.SetSel(iTextLen,iTextLen);
  143. edit.ReplaceSel(dspstr);
  144. if (iTextLen > 10000)
  145. ClearMsg();
  146. }
  147. LRESULT CPeugeotView::OnTCPSrvRec(WPARAM wParam,LPARAM lParam)
  148. {
  149.     NetReceive();
  150. return 0L;
  151. }
  152. LRESULT CPeugeotView::OnTCPSrvConnected(WPARAM wParam,LPARAM lParam)
  153. {
  154.     //目前仅支持一客户端与服务端通讯
  155. //先建立连接的客户端将占用通讯流程。如同时与多个客户端通讯,则需开辟多线程
  156.     CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  157.     ASSERT_VALID(pDoc);
  158. if (m_pSocketObj == NULL)
  159. {
  160. m_pSocketObj = (CAsyncSocket*)lParam;
  161.         if ( (m_pPrtObj != NULL) && (pDoc->m_MasterFlag) ) //主站时启动召唤定时器 
  162. m_pPrtObj->StartTimer();
  163. }
  164. return 0L;
  165. }
  166. LRESULT CPeugeotView::OnTCPSrvClosed(WPARAM wParam,LPARAM lParam)
  167. {
  168. if (m_pSocketObj != NULL)
  169. {
  170.    //如关闭的客户端为当前通讯流程使用的客户端,则m_pSocketObj重新置空,等待其他客户端连接
  171. if (m_pSocketObj->m_hSocket == ((CAsyncSocket*)lParam)->m_hSocket)
  172. {
  173.    m_pPrtObj->CloseTimer();
  174. m_pSocketObj = NULL; 
  175. }
  176. }
  177. return 0L;
  178. }
  179. void CPeugeotView::NetReceive()
  180. {
  181.     CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  182.     ASSERT_VALID(pDoc);
  183.     BYTE recbyte = 0x00;
  184. if (m_pSocketObj->Receive(&recbyte,1)) 
  185.     {
  186. if (pDoc->m_MasterFlag)
  187.         m_pPrtObj->MasterRec(recbyte);    //主站接收数据
  188.     else
  189.         m_pPrtObj->SlaveRec(recbyte);     //从站接收数据
  190. }
  191. }
  192. LRESULT CPeugeotView::OnTCPCltConnected(WPARAM wParam,LPARAM lParam)
  193. {
  194.     CMainFrame* pMain = (CMainFrame*)AfxGetApp()->m_pMainWnd; 
  195. CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  196.     ASSERT_VALID(pDoc);
  197.     m_pSocketObj = (CAsyncSocket*)lParam;
  198.     pMain->IsCmmConnect = true;
  199.     if ( (m_pPrtObj != NULL) && (pDoc->m_MasterFlag) ) //主站时启动召唤定时器 
  200.       m_pPrtObj->StartTimer();
  201. return 0L;
  202. }
  203. LRESULT CPeugeotView::OnTCPCltClosed(WPARAM wParam,LPARAM lParam)
  204. {
  205.     CMainFrame* pMain = (CMainFrame*)AfxGetApp()->m_pMainWnd; 
  206. CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  207.     ASSERT_VALID(pDoc);
  208.     m_pPrtObj->CloseTimer();
  209. m_pSocketObj = NULL;  
  210.     
  211. pMain->IsCmmConnect = false;
  212. AfxMessageBox("远方服务器端口已断开,请重新建立连接!");
  213. return 0L;
  214. }
  215. LRESULT CPeugeotView::OnTCPCltRec(WPARAM wParam,LPARAM lParam)
  216. {
  217.     NetReceive();
  218. return 0L;
  219. }
  220. LRESULT CPeugeotView::OnUDPRec(WPARAM wParam,LPARAM lParam)
  221. {
  222. CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
  223.     ASSERT_VALID(pDoc);
  224. BYTE RecBuf[4096];
  225. int nBufLen = 4096;
  226. int nRecNum = 0;
  227. UINT rSocketPort;
  228. CString rSocketAddr;
  229. nRecNum = m_pSocketObj->ReceiveFrom(RecBuf,nBufLen,rSocketAddr,rSocketPort);
  230.     if (nRecNum > 0)
  231. {
  232. for (int i=0; i<nRecNum; i++)
  233. {
  234. if (pDoc->m_MasterFlag)
  235. m_pPrtObj->MasterRec(RecBuf[i]);    //主站接收数据
  236. else
  237. m_pPrtObj->SlaveRec(RecBuf[i]);     //从站接收数据
  238. }
  239. }
  240. return 0L;
  241. }
  242. //视图清空显示报文
  243. void CPeugeotView::ClearMsg()
  244. {
  245. CEdit& edit = GetEditCtrl();
  246.     edit.SetSel(0,-1);
  247. edit.Clear();
  248. }