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

📄 comview.cpp

📁 两台计算机间的串口通讯程序
💻 CPP
字号:
// comView.cpp : implementation of the CComView class
//

#include "stdafx.h"
#include "com.h"

#include "comDoc.h"
#include "comView.h"
#include <stdio.h>
#include <string.h>

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

const MaxStringLen=40;
const X0=10;
const Y0=10;
static char buffer[MaxStringLen+1]="";

/////////////////////////////////////////////////////////////////////////////
// CComView

IMPLEMENT_DYNCREATE(CComView, CEditView)

BEGIN_MESSAGE_MAP(CComView, CEditView)
	//{{AFX_MSG_MAP(CComView)
	ON_WM_CHAR()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CComView construction/destruction

CComView::CComView()
{
	// TODO: add construction code here

}

CComView::~CComView()
{
}

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

	BOOL bPreCreated = CEditView::PreCreateWindow(cs);
	cs.style &= ~(ES_AUTOHSCROLL|WS_HSCROLL);	// Enable word-wrapping

	return bPreCreated;
}

/////////////////////////////////////////////////////////////////////////////
// CComView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CComView diagnostics

#ifdef _DEBUG
void CComView::AssertValid() const
{
	CEditView::AssertValid();
}

void CComView::Dump(CDumpContext& dc) const
{
	CEditView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CComView message handlers

void CComView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	static int i=0;
	CClientDC dc(this);
	if (i<MaxStringLen)
	{
		buffer[i++]=char(nChar);
	}
	else
	{//reached the end.
		i=0;
		buffer[i+1]=char(nChar);
	}
	dc.TextOut(X0,Y0,buffer,strlen(buffer));
//	CEditView::OnChar(nChar, nRepCnt, nFlags);
}

void CComView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	HANDLE CommFile;
	DCB CommDCB;
//	DWORD ErrorFlag;
//	COMSTAT ComState;
	DWORD iLength;
	BOOL rc;
   // CString oBuff;
    
	if((CommFile=CreateFile("COM2",GENERIC_WRITE,0,NULL,
		OPEN_EXISTING,0,NULL))== (HANDLE)-1) 
	{
		MessageBox("无法开放通讯口,请检查是否有此通讯口!","错误!",MB_ICONERROR);
		return;
	}
	SetupComm(CommFile,8192,8192); 
	GetCommState(CommFile, &CommDCB);
/*	CommDCB.BaudRate = 9600;
//	CommDCB.BaudRate =19200;
	CommDCB.ByteSize = 8; 
	CommDCB.Parity = NOPARITY; 
	CommDCB.StopBits = ONESTOPBIT; 
*/
	CommDCB.BaudRate = 19200;
	CommDCB.ByteSize = 8;
	CommDCB.Parity =  NOPARITY;
	CommDCB.StopBits = ONESTOPBIT;
	CommDCB.EvtChar = 0x0d;
//	BuildCommDCB("COM1: 96,n,8,1 ", &dcb);
	CommDCB.fDtrControl = DTR_CONTROL_ENABLE;
	CommDCB.fRtsControl = RTS_CONTROL_ENABLE;
	CommDCB.fInX = CommDCB.fOutX = 1;
    CommDCB.XonLim = CommDCB.XoffLim = 100 ;
	CommDCB.XonChar = 0x11;
	CommDCB.XoffChar = 0x13;
	CommDCB.fBinary = TRUE ;
	CommDCB.fParity = TRUE ;
    SetCommState(CommFile,&CommDCB); 
	iLength=40;
	rc = WriteFile(CommFile, &buffer,iLength,
			&iLength,NULL);
	//ClearCommError(CommFile,&ErrorFlag,&ComState);
	//iLength=ComState.cbOutQue; 
	CloseHandle(CommFile);
	MessageBox("传送完毕","传送成功",MB_OK);
    //CEditView::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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