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

📄 comtestview.cpp

📁 visual c6.0实现的窜口通讯
💻 CPP
字号:
// ComTestView.cpp : implementation of the CComTestView class
//

#include "stdafx.h"
#include "ComTest.h"

#include "ComTestDoc.h"
#include "ComTestView.h"
#include "omSeting.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CComTestView

IMPLEMENT_DYNCREATE(CComTestView, CView)

BEGIN_MESSAGE_MAP(CComTestView, CView)
	//{{AFX_MSG_MAP(CComTestView)
	ON_COMMAND(IDC_SETING, OnSeting)
	ON_WM_CREATE()
	ON_COMMAND(IDC_OPENCOM, OnOpencom)
	ON_WM_TIMER()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CComTestView construction/destruction

CComTestView::CComTestView()
{
	// TODO: add construction code here
	m_comnum=1;
	str_Receive="";
	str_Send="";
}

CComTestView::~CComTestView()
{
}

BOOL CComTestView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CComTestView drawing

void CComTestView::OnDraw(CDC* pDC)
{
	CComTestDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	CFont m_font;
	m_font.CreatePointFont(200,"华文行楷");
	CFont *m_oldfont=pDC->SelectObject(&m_font);

	pDC->TextOut(10,10,"输出数据是: "+str_Send);
	TEXTMETRIC mt;
	pDC->GetTextMetrics(&mt);
	pDC->TextOut(10,10+mt.tmHeight,"输入数据是: "+str_Receive);
	pDC->SelectObject(m_oldfont);
	// TODO: add draw code for native data here
}

/////////////////////////////////////////////////////////////////////////////
// CComTestView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CComTestView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CComTestView message handlers

void CComTestView::OnSeting() 
{
	// TODO: Add your command handler code here
	ComSeting dlg;
	dlg.m_ComNum=m_comnum-1;
	if (dlg.DoModal()==IDOK)
	{
// 		CString str;
// 		str.Format("%d",dlg.m_ComNum);
// 		MessageBox(str);
		m_comnum=dlg.m_ComNum+1;
	}
	
}

void CComTestView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	m_Com.SetInBufferSize(1024);//接收缓冲器大小
	m_Com.SetInBufferCount(0);
	m_Com.SetOutBufferSize(1024);//发送缓冲器大小
	m_Com.SetOutBufferCount(0);
	m_Com.SetInputLen(0);//读取缓冲器中的全部数据
	m_Com.SetInputMode(1);//以2进制方式读数据
	m_Com.SetRThreshold(1);//接受1个数据位产生oncomm事件
	m_Com.SetSettings("9600,n,8,1");//波特率9600,无校验位,8个数据位,1个停止位
	m_Com.SetCommPort(m_comnum);
	// TODO: Add your specialized code here and/or call the base class
	
}
BEGIN_EVENTSINK_MAP(CComTestView, CView)
    //{{AFX_EVENTSINK_MAP(ComSeting)
	ON_EVENT(ComSeting, IDC_MyCom, 1 /* OnComm */, OnOnCommMscomm, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CComTestView::OnOnCommMscomm() 
{
	// TODO: Add your control notification handler code here
	VARIANT vt;
	BYTE bt[1024];
	COleSafeArray safearray;
	int len;
	long k;
	switch(m_Com.GetCommEvent())
	{
	case 2:
		vt=m_Com.GetInput();
		safearray=vt;
		len=safearray.GetOneDimSize();
		for (k=0;k<len;k++)
		{
			safearray.GetElement(&k,bt+k);
		}
		CString str;
		str.Format("%d ",bt[0]);
		str_Receive+=str;
		Invalidate();
		break;
	}
}

//DEL void CComTestView::DoDataExchange(CDataExchange* pDX) 
//DEL {
//DEL 	// TODO: Add your specialized code here and/or call the base class
//DEL 	
//DEL 	CView::DoDataExchange(pDX);
//DEL }

int CComTestView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	DWORD styles=WS_VISIBLE|WS_CHILD;
	if (!m_Com.Create(NULL,styles,CRect(0,0,0,0),this,IDC_MyCom))
	{
		AfxMessageBox("创建串口失败!!");
		return -1;
	}

	return 0;
}

void CComTestView::OnOpencom() 
{
	// TODO: Add your command handler code here
	if (m_Com.GetPortOpen())
	{
		//MessageBox("nihao");
		m_Com.SetPortOpen(0);//关闭串口
	}
	m_Com.SetCommPort(m_comnum);
	m_Com.SetPortOpen(1);
	CByteArray array1;
	array1.RemoveAll();
	array1.SetSize(1);
	array1.SetAt(0,02);
	m_Com.SetOutput(COleVariant(array1));
	CString str;
	str.Format("%d ",2);
	str_Send+=str;
	Invalidate();
//	SetTimer(1,1000,NULL);
}

void CComTestView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	MessageBox("超时");
	CView::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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