?? sdicommview.cpp
字號(hào):
// SDICommView.cpp : implementation of the CSDICommView class
//
#include "stdafx.h"
#include "SDIComm.h"
#include "SDICommDoc.h"
#include "SDICommView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSDICommView
IMPLEMENT_DYNCREATE(CSDICommView, CView)
BEGIN_MESSAGE_MAP(CSDICommView, CView)
//{{AFX_MSG_MAP(CSDICommView)
ON_WM_CREATE()
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CSDICommView construction/destruction
CSDICommView::CSDICommView()
{
// TODO: add construction code here
}
CSDICommView::~CSDICommView()
{
}
BOOL CSDICommView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CSDICommView drawing
void CSDICommView::OnDraw(CDC* pDC)
{
CSDICommDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CSDICommView printing
BOOL CSDICommView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CSDICommView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CSDICommView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CSDICommView diagnostics
#ifdef _DEBUG
void CSDICommView::AssertValid() const
{
CView::AssertValid();
}
void CSDICommView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CSDICommDoc* CSDICommView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSDICommDoc)));
return (CSDICommDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CSDICommView message handlers
int CSDICommView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_MSComm.Create(NULL,0,CRect(0,0,0,0),this,IDC_MSCOMM1);
if(m_MSComm.GetPortOpen()) //如果串口是打開的,則行關(guān)閉串口
m_MSComm.SetPortOpen(FALSE);
m_MSComm.SetCommPort(2); //選擇COM2
m_MSComm.SetInBufferSize(1024); //接收緩沖區(qū)
m_MSComm.SetOutBufferSize(1024); //發(fā)送緩沖區(qū)
m_MSComm.SetInputLen(0); //設(shè)置當(dāng)前接收區(qū)數(shù)據(jù)長度為0,表示全部讀取
m_MSComm.SetInputMode(1); //以二進(jìn)制方式讀寫數(shù)據(jù)
m_MSComm.SetRThreshold(5); //接收緩沖區(qū)有5個(gè)及5個(gè)以上字符時(shí),將引發(fā)接收數(shù)據(jù)的OnComm事件
m_MSComm.SetSettings("9600,n,8,1"); //波特率:9600,無檢驗(yàn)位,8個(gè)數(shù)據(jù)位,1個(gè)停止位
if(!m_MSComm.GetPortOpen()) //如果串口沒有打開則打開
m_MSComm.SetPortOpen(TRUE); //打開串口
else
AfxMessageBox("Open Serial Port Failure!");
m_MSComm.GetInput(); //先預(yù)讀緩沖區(qū)以清除殘留數(shù)據(jù)
return 0;
}
/////////////以下為手工添加的代碼///////////////////////////////////////////////
BEGIN_EVENTSINK_MAP(CSDICommView, CView)
//{{AFX_EVENTSINK_MAP(CAboutDlg)
ON_EVENT(CSDICommView, IDC_MSCOMM1, 1, OnComm, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CSDICommView::OnComm()
{
// TODO: Add your control notification handler code here
VARIANT variant_inp;
COleSafeArray safearray_inp;
LONG len,k;
BYTE rxdata[2048]; //設(shè)置BYTE數(shù)組
CString strtemp;
CString strDisp; //用于顯示接收數(shù)據(jù)
if(m_MSComm.GetCommEvent()==2)
{
variant_inp=m_MSComm.GetInput(); //讀緩沖區(qū)
safearray_inp=variant_inp; //VARIANT型變量轉(zhuǎn)換為ColeSafeArray型變量
len=safearray_inp.GetOneDimSize(); //得到有效數(shù)據(jù)長度
for(k=0;k<len;k++)
safearray_inp.GetElement(&k,rxdata+k);//轉(zhuǎn)換為BYTE型數(shù)組
for(k=0;k<len;k++) //將數(shù)組轉(zhuǎn)換為Cstring型變量
{
BYTE bt=*(char*)(rxdata+k); //字符型
strtemp.Format("%c",bt); //將字符送入臨時(shí)變量strtemp存放
strDisp+=strtemp; //加入接收編輯框?qū)?yīng)字符串
}
CDC* pDC=GetDC(); //準(zhǔn)備數(shù)據(jù)顯示
pDC->TextOut(200,100,"COM2接收到:"+strDisp);//顯示接收到的數(shù)據(jù)
ReleaseDC(pDC);
strtemp.Format("OK,'%s' Received ",strDisp);
m_MSComm.SetOutput(COleVariant(strtemp));//發(fā)送數(shù)據(jù)
}
}
/////////////手工添加的代碼結(jié)束////////////////////////////////////////////////////
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -