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

📄 serialporttest2view.cpp

📁 利用SerialPortT类实现,串口通信的编程方法,区别与comm类的编程
💻 CPP
字号:
// SerialPortTest2View.cpp : implementation of the CSerialPortTest2View class
//

#include "stdafx.h"
#include "SerialPortTest2.h"

#include "SerialPortTest2Doc.h"
#include "SerialPortTest2View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTest2View

IMPLEMENT_DYNCREATE(CSerialPortTest2View, CView)

BEGIN_MESSAGE_MAP(CSerialPortTest2View, CView)
	//{{AFX_MSG_MAP(CSerialPortTest2View)
	ON_WM_TIMER()
	ON_MESSAGE(WM_COMM_RXCHAR,OnComm)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTest2View construction/destruction

CSerialPortTest2View::CSerialPortTest2View()
{
	// TODO: add construction code here
	m_bCOM1Opened=FALSE; //com1初始状态没有打开
	m_bCOM2Opened=FALSE; //com2初始状态没有打开

}

CSerialPortTest2View::~CSerialPortTest2View()
{
}

BOOL CSerialPortTest2View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTest2View drawing

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

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTest2View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTest2View diagnostics

#ifdef _DEBUG
void CSerialPortTest2View::AssertValid() const
{
	CView::AssertValid();
}

void CSerialPortTest2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CSerialPortTest2View message handlers
LONG CSerialPortTest2View::OnComm(WPARAM ch, LPARAM port)
{
	if(port==2)   //COM2接收到数据    
	{
		switch(ch)
		{
		case '$':
			m_strRXDataCom2=(char)ch;
			
			break;
		case '*':
			{
			m_strRXDataCom2+=(char)ch;
			CDC* pDC=GetDC();   //准备数据显示
  			pDC->TextOut(10,150,"COM2接收到"+m_strRXDataCom2);//显示接收到的数据 
			ReleaseDC(pDC);
			m_strRXDataCom2.Replace('$','Y');
			//COM2发送应答信息
			m_SerialPort[1].WriteToPort((LPCTSTR)m_strRXDataCom2); 
			}
			break;
		default:
			m_strRXDataCom2+=(char)ch;
			break;
		}
	}

	if(port==1)	    //COM1接收到数据
	{
		switch(ch)
		{
		case 'Y':
			m_strRXDataCom1=(char)ch;
			break;
		case '*':
			{
			m_strRXDataCom1+=(char)ch;
			CDC* pDC=GetDC();   //准备数据显示
  			pDC->TextOut(200,100,"COM1接收到:"+m_strRXDataCom1);//显示接收到的数据 
			ReleaseDC(pDC);
			}
			break;
		default:
			m_strRXDataCom1+=(char)ch;
			break;
		}
	}

	return 0;
}

void CSerialPortTest2View::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	// TODO: Add your specialized code here and/or call the base class
	for(int i=0;i<2;i++)
	{
		if(m_SerialPort[i].InitPort(this,i+1,9600,'N',8,1,EV_RXFLAG|EV_RXCHAR,512))
		{
			m_SerialPort[i].StartMonitoring();
			if(i==0)
			{
				SetTimer(1,1000,NULL);
				m_bCOM1Opened=TRUE;//com1口打开
			}
			else
				m_bCOM2Opened=TRUE;

		}
		else
		{
			CString strTemp;
			strTemp.Format("com%d没有发现,或者被其他设备占用",i+1);
			AfxMessageBox(strTemp);
		}
		   
	}
}

void CSerialPortTest2View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	static UINT unCount=0;//定义静态变量
	if(m_bCOM1Opened)
	{
		unCount++;
		CString strTemp;
		strTemp.Format("$%06d",unCount);
		m_SerialPort[0].WriteToPort((LPCTSTR)strTemp);
		CDC* pDC=GetDC();
		pDC->TextOut(10,100,"COM1发送:"+strTemp);
		ReleaseDC(pDC);
	}
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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