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

📄 serialview.cpp

📁 It a serial port programming
💻 CPP
字号:
// SerialView.cpp : implementation of the CSerialView class
//

#include "stdafx.h"
#include "Serial.h"

#include "SerialDoc.h"
#include "SerialView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSerialView

IMPLEMENT_DYNCREATE(CSerialView, CFormView)

BEGIN_MESSAGE_MAP(CSerialView, CFormView)
	//{{AFX_MSG_MAP(CSerialView)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSerialView construction/destruction

CSerialView::CSerialView()
	: CFormView(CSerialView::IDD)
{
	//{{AFX_DATA_INIT(CSerialView)
	m_output = _T("");
	m_input = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CSerialView::~CSerialView()
{
}

void CSerialView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialView)
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Text(pDX, IDC_EDIT1, m_output);
	DDX_Text(pDX, IDC_EDIT2, m_input);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CSerialView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();
	// COM Port Initialisation 

	m_comm.SetCommPort(1);
	m_comm.SetSettings("9600,N,8,1");
	m_comm.SetInputLen(1);
	m_comm.SetInputMode(0);
	m_comm.SetRTSEnable(TRUE);
	m_comm.SetRThreshold(1);
	//m_comm.SetSThreshold (1);
	m_comm.SetPortOpen(true);
}

/////////////////////////////////////////////////////////////////////////////
// CSerialView printing

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

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

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

void CSerialView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
{
	// TODO: add customized printing code here
}

/////////////////////////////////////////////////////////////////////////////
// CSerialView diagnostics

#ifdef _DEBUG
void CSerialView::AssertValid() const
{
	CFormView::AssertValid();
}

void CSerialView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CSerialView message handlers

BEGIN_EVENTSINK_MAP(CSerialView, CFormView)
    //{{AFX_EVENTSINK_MAP(CSerialView)
	ON_EVENT(CSerialView, IDC_MSCOMM1, 1 /* OnComm */, OnOnCommMscomm1, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()


void CSerialView::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
	if (m_comm.GetCommEvent()==2 ) 
		{	
			VARIANT in_dat;
			in_dat = m_comm.GetInput();
			CString strInput(in_dat.bstrVal);
			m_input = m_input + strInput;
			UpdateData(FALSE);	
		}

}
void CSerialView::OnButton1() 
{
	// TODO: Add your control notification handler code here

	UpdateData(TRUE);
	m_comm.SetOutput(COleVariant(m_output));//strOutput));	
}

⌨️ 快捷键说明

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