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

📄 callerview.cpp.bak

📁 串口的监控程序,提供了固定命令模式和
💻 BAK
字号:
// 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

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("");

		}
	}
}

CCallerView::~CCallerView()
{
}

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

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

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

BOOL CCallerView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

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

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

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
void CCallerView::AssertValid() const
{
	CRichEditView::AssertValid();
}

void CCallerView::Dump(CDumpContext& dc) const
{
	CRichEditView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CCallerView message handlers
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;
}
void CCallerView::OnInitialUpdate() 
{
	CRichEditView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
}

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;
}

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
		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;
		//-----
	}
}


//
// 
//
char CCallerView::Char2Hex(char ch)
{
	if((ch>='0')&&(ch<='9'))
	{
		return ch-0x30;
	}
	else if	((ch>='A')&&(ch<='F'))
	{
		return ch-'A'+10;
	}
	else if((ch>='a')&&(ch<='f'))
	{
		return ch-'a'+10;
	}
	else 
	{
		return (-1);
	}

}


//
// 
//
int CCallerView::String2Hex(CString str, char senddata[24])//CByteArray &senddata)
{

	int hexdata,lowhexdata;
	int hexdatalen=0;
	int len=str.GetLength();
	char data[24];
	memset(data,0, sizeof(data));

	for(int i=0;i<len;)
	{
		char lstr,hstr=str[i];
		if(hstr==' ')
		{
			i++;
			continue;
		}
		i++;
		if(i>=len)
		{
			break;
		}

		lstr=str[i];
		hexdata=Char2Hex(hstr);
		lowhexdata=Char2Hex(lstr);

		if((hexdata==16)||(lowhexdata==16))
		{
			break;
		}
		else 
		{
			hexdata=hexdata*16+lowhexdata;
		}

		i++;
		data[hexdatalen]=(char)hexdata;
		hexdatalen++;
	}

	memcpy( senddata, data ,sizeof(data) );
	memset( data,0,sizeof(data));

	return hexdatalen;
}

//
// 
//
void CCallerView::OnConfigClear() 
{
	// TODO: Add your command handler code here
	CRichEditCtrl &edit = GetRichEditCtrl();

	LONG temp = GetWindowTextLength();
//	GetLineFromChar( temp  );

	int a = edit.GetLineCount();
	edit.SetSel( 0,-1 );
	edit.Clear();
	


}


#define MAXBUFF 512

//
// 
//
void CCallerView::OnFunc1Func1() 
{
	// TODO: Add your command handler code here
	CString str;

	CString strSend;
//	strSend.Format("This is Func1.func2");

    strSend.Format("abcdefg1234567h");

	str.Format( "f11 resquest:");

	SendFunctionCommand( strSend, str);
}

//
// 
//
void CCallerView::OnFunc1Func2() 
{
	CString str;
	// TODO: Add your command handler code here
	char m_strTXData[] = "aBCdefg12345";
	CString strSend;
//	strSend.Format("This is Func1.func2");

    strSend.Format("abcdefg1234567h");
	
	str.Format( "f12 resquest:");
	SendFunctionCommand( strSend, str);
}

//
// 
//
void CCallerView::OnFunc1Func3() 
{
	CString str;
	CString strSend;
	// TODO: Add your command handler code here

	strSend.Format("ghjklmnopq");

	str.Format( "f13 resquest:");
	SendFunctionCommand( strSend, str);
}

//
// 
//
void CCallerView::OnFunc2Func1() 
{
	CString str;
	CString strSend;
	// TODO: Add your command handler code here
	strSend.Format("This is func2.fucn1\n");

	str.Format( "f21 resquest:");
	SendFunctionCommand( strSend, str);	
}

//
//
//
void CCallerView::OnFunc2Func2() 
{
	CString str;
	CString strSend;
	// TODO: Add your command handler code here
	strSend.Format("This is func2.func2\n");

	str.Format( "f22 resquest:");
	SendFunctionCommand( strSend, str);
}

//
//
//
void CCallerView::OnFunc2Func3() 
{
	CString str;
	CString strSend;
	// TODO: Add your command handler code here
	strSend.Format("This is func2. func3\n");


	str.Format( "f23 resquest:");
	SendFunctionCommand( strSend, str);
}

//
//
//
void CCallerView::OnFunc2Func4() 
{
	CString str;
	CString strSend;
	// TODO: Add your command handler code here
	strSend.Format("This is func2.func4\n");
	str.Format( "f24 resquest:");
	SendFunctionCommand( strSend, str);
}

//
//
//
void CCallerView::OnFunc2Func5() 
{
	CString str;
	CString strSend;
	// TODO: Add your command handler code here
	strSend.Format("This is func2.func5\n");

	str.Format( "f25 resquest:");
	SendFunctionCommand( strSend, str);	
}

//
//
//

void CCallerView::SendFunctionCommand(CString strComm,CString strCommId)
{
	if(m_bDataMode)
	{

		BYTE btData[128];
        RichCtrl.StrToHex( strComm, btData );

		m_ComPort.WriteToPort((char*)btData);			    
//		memset(hexdata, 0, sizeof(hexdata));

		strCommId += "( Hex )";
		PutMessage( strCommId );
	}
	else
	{
		char* p = strComm.GetBuffer(128);
		m_ComPort.WriteToPort(p);//m_strTXData); 

		strCommId += "( ASCII )";
		PutMessage( strCommId );
	}	

}


void CCallerView::PutMessage(CString strMessage)
{
    int nTextLength;
	CString str;
	CCallerDoc  *pDoc = GetDocument();
//    CEdit &e = GetEditCtrl(); 
	CRichEditCtrl& e=GetRichEditCtrl();

	// get the length of all text
	nTextLength = e.GetWindowTextLength();

	// set cursor to last position

	e.SetSel( nTextLength , nTextLength);

    e.ReplaceSel(  "\r\n");
	e.ReplaceSel(  "\r\n");

	e.ReplaceSel( strMessage );

	e.ReplaceSel(  "\r\n");
	
}

void CCallerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default

        int a = GetLineIndex();
	int b = GetCursorCol();
	CRichEditView::OnKeyDown(nChar, nRepCnt, nFlags);
	LN_COL_MSG(MAKELPARAM(GetLineIndex(),GetCursorCol()))
}

void CCallerView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    int a = GetLineIndex();
	int b = GetCursorCol();
//	CMainFrame::DrawLnCol( nLineIndex, ne-ns );
	CRichEditView::OnLButtonDown(nFlags, point);
	LN_COL_MSG(MAKELPARAM(GetLineIndex(),GetCursorCol()))
}


long CCallerView::GetLineStart(int nLineIndex)
{
	CRichEditCtrl &pe = GetRichEditCtrl();

	long l = pe.GetLineCount();
	nLineIndex = pe.LineFromChar(pe.LineIndex(-1));

	return pe.LineIndex(nLineIndex);
}

int CCallerView::GetCursorCol()
{
	CRichEditCtrl &pe = GetRichEditCtrl();

	long ns=0,ne=0;
	pe.GetSel(ns,ne);
	if(ns!=ne)
	{
		ns=pe.LineFromChar(ne);
		ns=GetLineStart(ns);
		return ne-ns;
	}
	return ne-pe.LineIndex(-1);
}


int CCallerView::GetLineIndex()
{
	CRichEditCtrl &pe = GetRichEditCtrl();
	long l = pe.GetLineCount();
	int nLineIndex=pe.LineFromChar(pe.LineIndex(-1));
    
	return nLineIndex;
}




⌨️ 快捷键说明

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