⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 callerview.cpp

📁 串口的监控程序,提供了固定命令模式和
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// CallerView.cpp : implementation of the CCallerView class
//

#include "stdafx.h"
#include "Caller.h"

#include "CallerDoc.h"
#include "CallerView.h"
#include "ConfigSerial.h"
#include "XRichEditCtrl.h"
//#include "CallerEdit.h"
#include "ConfigCmd.h"

#include "CntrItem.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

CXRichEditCtrl RichCtrl;
CSerialPort m_ComPort;


#define LN_COL_MSG(lparam)	::PostMessage(GetParent()->m_hWnd,WM_XRICHEDIT_MSG,\
(WPARAM)1,lparam);

/////////////////////////////////////////////////////////////////////////////
// CCallerView

IMPLEMENT_DYNCREATE(CCallerView, CRichEditView)

BEGIN_MESSAGE_MAP(CCallerView, CRichEditView)
	//{{AFX_MSG_MAP(CCallerView)
	ON_COMMAND(ID_CONFIG_SERIAL, OnConfigSerial)
	ON_COMMAND(ID_FUNC1_FUNC1, OnFunc1Func1)
	ON_COMMAND(IDC_CONFIG_CLEAR, OnConfigClear)
	ON_COMMAND(ID_FUNC1_FUNC2, OnFunc1Func2)
	ON_COMMAND(ID_FUNC1_FUNC3, OnFunc1Func3)
	ON_COMMAND(ID_FUNC2_FUNC1, OnFunc2Func1)
	ON_COMMAND(ID_FUNC2_FUNC2, OnFunc2Func2)
	ON_COMMAND(ID_FUNC2_FUNC3, OnFunc2Func3)
	ON_COMMAND(ID_FUNC2_FUNC4, OnFunc2Func4)
	ON_COMMAND(ID_FUNC2_FUNC5, OnFunc2Func5)
	ON_WM_DESTROY()
	ON_WM_KEYDOWN()
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(IDD_CONFIG_SERIAL, OnConfigSerial)
	ON_COMMAND(ID_CONFIG_COMMAND, OnConfigCommand)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRichEditView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRichEditView::OnFilePrintPreview)
	ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)
END_MESSAGE_MAP()
// ON_MESSAGE(WM_COMM_RXCHAR, OnCommunication)


/////////////////////////////////////////////////////////////////////////////
// CCallerView construction/destruction

//##ModelId=3FFC8F3A0159
CCallerView::CCallerView()
{
    CString str;
	// TODO: add construction code here
	// Initialize serial port
   	if (m_ComPort.InitPort(this,1,19200,'N',8,1,EV_RXFLAG | EV_RXCHAR,512))
	{
		//start monitor process
		m_ComPort.StartMonitoring();
	}
	else
	{
		CString str;
		str.Format("无法初始化COM%d ",1);
		AfxMessageBox(str);
	}

	// char mode 
	m_bDataMode = 0;	
	m_bDataDisp = 0;
	
	// clear checksum 
	m_chChecksum=0;		
	
	m_bRXDataTimeout  = FALSE;

	m_unReceiveTimeOut = 20;

	m_hWnd = (CWnd*)AfxGetApp()->m_pMainWnd;

	// disable The user Config command line
	int i = 0;
	int j = 0;
	for ( i = 0; i < 2; i ++ )
	{
		for ( j = 0; j < 5; j ++ )
		{
//			CmdLineInfo.m_bCmdLineEn[i][j] = FALSE;
//			CmdLineInfo.m_strCmdLine
			 m_CmdLine[i][j].m_bCmdLineEn = FALSE;
			 m_CmdLine[i][j].m_strCmdLine.Format("");

		}
	}
}

//##ModelId=3FFC8F3A029F
CCallerView::~CCallerView()
{
}

//##ModelId=3FFC8F3A01F4
BOOL CCallerView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	BOOL bResult = CRichEditView::PreCreateWindow (cs);
    
	//
    cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);

	return bResult ;
}

/////////////////////////////////////////////////////////////////////////////
// CCallerView drawing

//##ModelId=3FFC8F3A01E9
void CCallerView::OnDraw(CDC* pDC)
{
	CCallerDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CCallerView printing

//##ModelId=3FFC8F3A0205
BOOL CCallerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

//##ModelId=3FFC8F3A0215
void CCallerView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

//##ModelId=3FFC8F3A0232
void CCallerView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

//##ModelId=3FFC8F3A030D
void CCallerView::OnDestroy() 
{
	CRichEditView::OnDestroy();
	
	// TODO: Add your message handler code here
COleClientItem* pActiveItem = GetDocument()->GetInPlaceActiveItem(this);
   if (pActiveItem != NULL && pActiveItem->GetActiveView() == this)
   {
      pActiveItem->Deactivate();
      ASSERT(GetDocument()->GetInPlaceActiveItem(this) == NULL);
   }
}

/////////////////////////////////////////////////////////////////////////////
// CCallerView diagnostics

#ifdef _DEBUG
//##ModelId=3FFC8F3A02A1
void CCallerView::AssertValid() const
{
	CRichEditView::AssertValid();
}

//##ModelId=3FFC8F3A02AF
void CCallerView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

//##ModelId=3FFC8F3A01C6
CCallerDoc* CCallerView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CCallerDoc)));
	return (CCallerDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CCallerView message handlers
//##ModelId=3FFC8F3A032F
LONG CCallerView::OnCommunication(WPARAM ch, LPARAM port)
{
    static int BuffIndex = 0;
	static char char_old;

	CString str;

    CRichEditCtrl& edit=GetRichEditCtrl();

	CCallerDoc  *pDoc = GetDocument();
    
	int nTextLength;
	CString strtemp;
	m_nRXCounterCOM1++;

	nTextLength = edit.GetWindowTextLength();

    LARGE_INTEGER  litmp; 
	LONGLONG       QPart;
	double         dfMinus = 0;
	double         dfFreq  = 0;
	double         dfTim   = 0;

	QueryPerformanceFrequency(&litmp); 

	dfFreq = (double)litmp.QuadPart; 
	QueryPerformanceCounter(&litmp); 

	QPart = litmp.QuadPart;
       dfMinus = ( double )( QPart-QPart_old ); 
	     dfTim = dfMinus / dfFreq; 

	if ( dfTim > ( 0.001 * m_unReceiveTimeOut ) )      // micro seconds
	{
		m_bRXDataTimeout = TRUE;
	}
    /////
	 QPart_old = QPart;
	dfFreq_old = dfFreq;

	if( m_bRXDataTimeout)
	{
		strtemp += "\r\n";	
		m_bRXDataTimeout = FALSE;

		str = "     Answer:";

		if(m_bDataMode)
		{
			str += "( Hex )";
		}
		else
		{
			str += "( ASCII )";
		}
		PutMessage( str );
	}

	if(m_bDataDisp)
	{
		//send char to strtemp with HEX format 
		strtemp.Format("%02X ",ch);    
	}
	else 
	{
		switch (ch)
		{
			// 
		case '\n':                        
			strtemp += "\r\n";
			break;
		case '\r':                       
			break;
		default:
			strtemp += ch;
		}

	}

	// save current char from serial port
	char_old = ch;

    //  All the text in the edit control is selected
    edit.SetSel( -1, 0);

	// Updata the edit control
	edit.ReplaceSel(strtemp);
//	strtemp.ReleaseBuffer();
    
	return (LONG)0;
}
//##ModelId=3FFC8F3A0203
void CCallerView::OnInitialUpdate() 
{
	CRichEditView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
}

//##ModelId=3FFC8F3A02BF
void CCallerView::OnConfigSerial() 
{
	// TODO: Add your command handler code here
	CConfigSerial *  dlg = new CConfigSerial(this, m_ComPort.GetDCB());

	// get data transf mode 
    dlg->m_bHexSend = m_bDataMode;
	dlg->m_ctrlHexDisp = m_bDataDisp;

	dlg->m_unReceiveTimeout = m_unReceiveTimeOut;
	
	if (dlg->DoModal() == IDOK)
	{
		m_bDataMode = dlg->m_ctrlHexSend;
		m_bDataDisp = dlg->m_ctrlHexDisp;

		CString strtemp = dlg->m_strBandRate;
		m_nBandRate = _ttoi( strtemp);

		m_unReceiveTimeOut = dlg->m_unReceiveTimeout;
        
		if( dlg->m_bBandChange)
		{
			m_ComPort.InitPort(this, 1, m_nBandRate, 'N', 8, 1, EV_RXFLAG | EV_RXCHAR, 512);
    
	    	m_ComPort.StartMonitoring();
		}

		delete dlg;

		// check  command format if hexmode is selected
		if ( m_bDataMode )
		{
			CheckCmdFormat();		
		}
	}
}

//##ModelId=3FFC8F3A034B
void CCallerView::OnConfigCommand() 
{
	// TODO: Add your command handler code here
    //CConfigCmd *pDlg = (CConfigCmd *)GetDlgItem(IDD_CONFIG_COMMAND);
    CConfigCmd  dlg;
  
	//-------
	// Command line enable flag
	dlg.m_bCmd11En = m_CmdLine[0][0].m_bCmdLineEn;
	dlg.m_bCmd12En = m_CmdLine[0][1].m_bCmdLineEn;
	dlg.m_bCmd13En = m_CmdLine[0][2].m_bCmdLineEn;

	dlg.m_bCmd21En = m_CmdLine[1][0].m_bCmdLineEn;
	dlg.m_bCmd22En = m_CmdLine[1][1].m_bCmdLineEn;
	dlg.m_bCmd23En = m_CmdLine[1][2].m_bCmdLineEn;
	dlg.m_bCmd24En = m_CmdLine[1][3].m_bCmdLineEn;
	dlg.m_bCmd25En = m_CmdLine[1][4].m_bCmdLineEn;
	//-------
    // set dlg Command text
    dlg.m_strCmdLine11Text = m_CmdLine[0][0].m_strCmdLine;
	dlg.m_strCmdLine12Text = m_CmdLine[0][1].m_strCmdLine;
	dlg.m_strCmdLine13Text = m_CmdLine[0][2].m_strCmdLine;

	dlg.m_strCmdLine21Text = m_CmdLine[1][0].m_strCmdLine;
	dlg.m_strCmdLine22Text = m_CmdLine[1][1].m_strCmdLine;
	dlg.m_strCmdLine23Text = m_CmdLine[1][2].m_strCmdLine;
	dlg.m_strCmdLine24Text = m_CmdLine[1][3].m_strCmdLine;
	dlg.m_strCmdLine25Text = m_CmdLine[1][4].m_strCmdLine;
	//-----
	
	if( IDOK == dlg.DoModal() )
	{
		// Get Command line Active flag 1:Active; 0 no Active
		m_CmdLine[0][0].m_bCmdLineEn = dlg.m_bCmd11En;
		m_CmdLine[0][1].m_bCmdLineEn = dlg.m_bCmd12En;   
		m_CmdLine[0][2].m_bCmdLineEn = dlg.m_bCmd13En; 
                                                 
		m_CmdLine[1][0].m_bCmdLineEn = dlg.m_bCmd21En; 
		m_CmdLine[1][1].m_bCmdLineEn = dlg.m_bCmd22En; 
		m_CmdLine[1][2].m_bCmdLineEn = dlg.m_bCmd23En; 
		m_CmdLine[1][3].m_bCmdLineEn = dlg.m_bCmd24En; 
		m_CmdLine[1][4].m_bCmdLineEn = dlg.m_bCmd25En; 
	    //-------

	    // Get dlg Command text
		m_CmdLine[0][0].m_strCmdLine = dlg.m_strCmdLine11Text;
		m_CmdLine[0][1].m_strCmdLine = dlg.m_strCmdLine12Text;
		m_CmdLine[0][2].m_strCmdLine = dlg.m_strCmdLine13Text;
                                   
		m_CmdLine[1][0].m_strCmdLine = dlg.m_strCmdLine21Text;
		m_CmdLine[1][1].m_strCmdLine = dlg.m_strCmdLine22Text;
		m_CmdLine[1][2].m_strCmdLine = dlg.m_strCmdLine23Text;
		m_CmdLine[1][3].m_strCmdLine = dlg.m_strCmdLine24Text;
		m_CmdLine[1][4].m_strCmdLine = dlg.m_strCmdLine25Text;
		//-----
		if ( m_bDataMode )
		{
			CheckCmdFormat();		
		}
	}
}


//
// 
//
//##ModelId=3FFC8F3A0283
char CCallerView::Char2Hex(char ch)
{
	if((ch>='0')&&(ch<='9'))
	{
		return ch-0x30;
	}
	else if	((ch>='A')&&(ch<='F'))
	{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -