PeugeotView.cpp
上传用户:asikq0571
上传日期:2014-07-12
资源大小:528k
文件大小:8k
- // PeugeotView.cpp : implementation of the CPeugeotView class
- //
- #include "stdafx.h"
- #include "Peugeot.h"
- #include "PeugeotDoc.h"
- #include "PeugeotView.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPeugeotView
- IMPLEMENT_DYNCREATE(CPeugeotView, CEditView)
- BEGIN_MESSAGE_MAP(CPeugeotView, CEditView)
- //{{AFX_MSG_MAP(CPeugeotView)
- ON_WM_TIMER()
- //}}AFX_MSG_MAP
- ON_MESSAGE(WM_COMMNOTIFY,OnCommNotify)
-
- ON_MESSAGE(WM_TCP_SERVER_CONNECTED,OnTCPSrvConnected)
- ON_MESSAGE(WM_TCP_SERVER_CLOSED,OnTCPSrvClosed)
- ON_MESSAGE(WM_TCP_SERVER_RECEIVE,OnTCPSrvRec)
- ON_MESSAGE(WM_TCP_CLIENT_CONNECTED,OnTCPCltConnected)
- ON_MESSAGE(WM_TCP_CLIENT_CLOSED,OnTCPCltClosed)
- ON_MESSAGE(WM_TCP_CLIENT_RECEIVE,OnTCPCltRec)
- ON_MESSAGE(WM_UDP_RECEIVE,OnUDPRec)
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CEditView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CEditView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CEditView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPeugeotView construction/destruction
- CPeugeotView::CPeugeotView()
- {
- m_pPrtObj = NULL;
- m_pSocketObj = NULL;
- }
- CPeugeotView::~CPeugeotView()
- {
- }
- BOOL CPeugeotView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- BOOL bPreCreated = CEditView::PreCreateWindow(cs);
- cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL); // Enable word-wrapping
- return bPreCreated;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPeugeotView drawing
- void CPeugeotView::OnDraw(CDC* pDC)
- {
- CPeugeotDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPeugeotView printing
- BOOL CPeugeotView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return CEditView::OnPreparePrinting(pInfo);
- }
- void CPeugeotView::OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo)
- {
- // TODO: add extra initialization before printing
- CEditView::OnBeginPrinting(pDC, pInfo);
- }
- void CPeugeotView::OnEndPrinting(CDC* pDC, CPrintInfo* pInfo)
- {
- // TODO: add cleanup after printing
- CEditView::OnEndPrinting(pDC, pInfo);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CPeugeotView diagnostics
- #ifdef _DEBUG
- void CPeugeotView::AssertValid() const
- {
- CEditView::AssertValid();
- }
- void CPeugeotView::Dump(CDumpContext& dc) const
- {
- CEditView::Dump(dc);
- }
- CPeugeotDoc* CPeugeotView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPeugeotDoc)));
- return (CPeugeotDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CPeugeotView message handlers
- void CPeugeotView::OnTimer(UINT nIDEvent)
- {
- m_pPrtObj->MasterSend(nIDEvent);
- CView::OnTimer(nIDEvent);
- }
- BOOL CPeugeotView::Send(BYTE* pBuff, DWORD dwBytesToSend, DWORD& dwBytesSended)
- {
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
-
- BOOL IsDataSended = false;
-
- if (pDoc->m_CmmPortType == pDoc->CommPort)
- //串口发送数据
- IsDataSended = pDoc->m_ComObj.Send(pBuff, dwBytesToSend, dwBytesSended);
- else
- { //网口发送数据CFont
- if (pDoc->m_NetParam.TCPPrtType == pDoc->m_NetParam.UDP) //UDP方式,现只考虑往指定地址、端口发数据
- dwBytesSended = m_pSocketObj->SendTo(pBuff,dwBytesToSend,pDoc->m_NetParam.RemoteIPPort,pDoc->m_NetParam.RemoteIPAddr);
- else
- dwBytesSended = m_pSocketObj->Send(pBuff, dwBytesToSend);
-
- if (dwBytesSended >0)
- IsDataSended = true;
- }
-
- return IsDataSended;
- }
- LRESULT CPeugeotView::OnCommNotify(WPARAM wParam,LPARAM lParam)
- {
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
- BYTE recbyte = 0x00;
- if (pDoc->m_ComObj.Receive(&recbyte,1))
- {
- if (pDoc->m_MasterFlag)
- m_pPrtObj->MasterRec(recbyte); //主站接收数据
- else
- m_pPrtObj->SlaveRec(recbyte); //从站接收数据
- }
- return 0L;
- }
- //视图显示通讯报文
- void CPeugeotView::DisplayMsg(CString dspstr)
- {
- CMainFrame* pMain = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- if (!pMain->IsReflashMsg)
- return;
-
- CEdit& edit = GetEditCtrl();
- int iTextLen;
- iTextLen = edit.GetWindowTextLength();
- edit.SetSel(iTextLen,iTextLen);
- edit.ReplaceSel(dspstr);
-
- if (iTextLen > 10000)
- ClearMsg();
- }
- LRESULT CPeugeotView::OnTCPSrvRec(WPARAM wParam,LPARAM lParam)
- {
- NetReceive();
- return 0L;
- }
- LRESULT CPeugeotView::OnTCPSrvConnected(WPARAM wParam,LPARAM lParam)
- {
- //目前仅支持一客户端与服务端通讯
- //先建立连接的客户端将占用通讯流程。如同时与多个客户端通讯,则需开辟多线程
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
-
- if (m_pSocketObj == NULL)
- {
- m_pSocketObj = (CAsyncSocket*)lParam;
-
- if ( (m_pPrtObj != NULL) && (pDoc->m_MasterFlag) ) //主站时启动召唤定时器
- m_pPrtObj->StartTimer();
- }
- return 0L;
- }
- LRESULT CPeugeotView::OnTCPSrvClosed(WPARAM wParam,LPARAM lParam)
- {
- if (m_pSocketObj != NULL)
- {
- //如关闭的客户端为当前通讯流程使用的客户端,则m_pSocketObj重新置空,等待其他客户端连接
- if (m_pSocketObj->m_hSocket == ((CAsyncSocket*)lParam)->m_hSocket)
- {
- m_pPrtObj->CloseTimer();
- m_pSocketObj = NULL;
- }
- }
- return 0L;
- }
- void CPeugeotView::NetReceive()
- {
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
- BYTE recbyte = 0x00;
- if (m_pSocketObj->Receive(&recbyte,1))
- {
- if (pDoc->m_MasterFlag)
- m_pPrtObj->MasterRec(recbyte); //主站接收数据
- else
- m_pPrtObj->SlaveRec(recbyte); //从站接收数据
- }
- }
- LRESULT CPeugeotView::OnTCPCltConnected(WPARAM wParam,LPARAM lParam)
- {
- CMainFrame* pMain = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
- m_pSocketObj = (CAsyncSocket*)lParam;
- pMain->IsCmmConnect = true;
- if ( (m_pPrtObj != NULL) && (pDoc->m_MasterFlag) ) //主站时启动召唤定时器
- m_pPrtObj->StartTimer();
- return 0L;
- }
- LRESULT CPeugeotView::OnTCPCltClosed(WPARAM wParam,LPARAM lParam)
- {
- CMainFrame* pMain = (CMainFrame*)AfxGetApp()->m_pMainWnd;
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
- m_pPrtObj->CloseTimer();
- m_pSocketObj = NULL;
-
- pMain->IsCmmConnect = false;
- AfxMessageBox("远方服务器端口已断开,请重新建立连接!");
- return 0L;
- }
- LRESULT CPeugeotView::OnTCPCltRec(WPARAM wParam,LPARAM lParam)
- {
- NetReceive();
- return 0L;
- }
- LRESULT CPeugeotView::OnUDPRec(WPARAM wParam,LPARAM lParam)
- {
- CPeugeotDoc* pDoc = (CPeugeotDoc*)GetDocument();
- ASSERT_VALID(pDoc);
- BYTE RecBuf[4096];
- int nBufLen = 4096;
- int nRecNum = 0;
- UINT rSocketPort;
- CString rSocketAddr;
-
- nRecNum = m_pSocketObj->ReceiveFrom(RecBuf,nBufLen,rSocketAddr,rSocketPort);
-
- if (nRecNum > 0)
- {
- for (int i=0; i<nRecNum; i++)
- {
- if (pDoc->m_MasterFlag)
- m_pPrtObj->MasterRec(RecBuf[i]); //主站接收数据
- else
- m_pPrtObj->SlaveRec(RecBuf[i]); //从站接收数据
- }
- }
- return 0L;
- }
- //视图清空显示报文
- void CPeugeotView::ClearMsg()
- {
- CEdit& edit = GetEditCtrl();
- edit.SetSel(0,-1);
- edit.Clear();
- }