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

📄 serialtestview.cpp

📁 用于下位机和上位机的串口通讯 测试串口 程序 V C++ 编程
💻 CPP
字号:
// SerialTestView.cpp : implementation of the CSerialTestView class
//

#include "stdafx.h"
#include "SerialTest.h"

#include "SerialTestDoc.h"
#include "SerialTestView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSerialTestView

IMPLEMENT_DYNCREATE(CSerialTestView, CFormView)

BEGIN_MESSAGE_MAP(CSerialTestView, CFormView)
	//{{AFX_MSG_MAP(CSerialTestView)
	ON_BN_CLICKED(IDC_btnSend, OnbtnSend)
	ON_WM_ERASEBKGND()
	ON_WM_DESTROY()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CSerialTestView construction/destruction

CSerialTestView::CSerialTestView()
	: CFormView(CSerialTestView::IDD)
{
	//{{AFX_DATA_INIT(CSerialTestView)
	m_strRXData = _T("");
	m_strTXData = _T("");
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	
	
}

CSerialTestView::~CSerialTestView()
{		


}

void CSerialTestView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSerialTestView)
	DDX_Control(pDX, IDC_EDIT_RXDATA, m_ctlRXData);
	DDX_Control(pDX, IDC_CHECK_HEXDISPLAY, m_ctrlHexDisplay);
	DDX_Text(pDX, IDC_EDIT_RXDATA, m_strRXData);
	DDX_Control(pDX, IDC_MSCOMM1, m_comm);
	DDX_Text(pDX, IDC_EDIT_TXDATA, m_strTXData);
	//}}AFX_DATA_MAP
}

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

	return CFormView::PreCreateWindow(cs);
}

void CSerialTestView::OnInitialUpdate()
{
	CFormView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();


			//初始化
    if(m_comm.GetPortOpen())
	   m_comm.SetPortOpen(FALSE);

	m_comm.SetCommPort(1);    // Com口设置。//m_nport=1
	m_comm.SetInBufferSize(1024);
	m_comm.SetInBufferCount(0);
	if( !m_comm.GetPortOpen())
	m_comm.SetPortOpen(TRUE);//打开串口
	else
	AfxMessageBox("不能打开串口1");

    m_comm.SetSettings("115200,N,8,1");//m_nbaud=m-nBaud波特率

    m_comm.SetInputLen(0);       //从接收缓冲区中读取的字符数。 
    m_comm.SetInputMode(1);      //1:表示以二进制方式检取数据
    m_comm.SetRTSEnable(TRUE);
    m_comm.SetRThreshold(1);//说明在产生 OnComm 事件之前要接收的字符数。
					    	//设置 Rthreshold 为 1,接收缓冲区收到每一个字
							//符都会使 MSComm 控件产生 OnComm 事件。	
    m_comm.GetInput();//先预读缓冲区以清除残留数据

}

/////////////////////////////////////////////////////////////////////////////
// CSerialTestView printing

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

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSerialTestView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CSerialTestView message handlers

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

void CSerialTestView::OnOnCommMscomm1() 
{
	// TODO: Add your control notification handler code here
	//	VARIANT variant_inp;
	COleVariant myVar;
	
    COleSafeArray safearray_inp;
    LONG len,k;
    BYTE rxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.
    CString strtemp;

	FILE *pFile=fopen("ecg.dat","ab+");//追加打开二进制(b)文件
//   int test_index=0;
//	if(!test_index)
	{
	if(m_comm.GetCommEvent()==2) //事件值为2表示接收缓冲区内有字符
		{             ////////以下你可以根据自己的通信协议加入处理代码
		  // variant_inp=m_comm.GetInput(); //读缓冲区
		  myVar.Attach(m_comm.GetInput());
//			safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量
		  safearray_inp=myVar;
		  len=safearray_inp.GetOneDimSize(); //得到有效数据长度
		  for(k=0;k<len;k++)
		      safearray_inp.GetElement(&k,rxdata+k);//转换为BYTE型数组
		  for(k=0;k<len;k++) //将数组转换为Cstring型变量
			{
				  BYTE bt=*(char*)(rxdata+k); //字符型
				  fwrite(rxdata+k,1,sizeof(rxdata[0]),pFile);
			   if(m_ctrlHexDisplay.GetCheck())
			
					strtemp.Format("%02X ",bt); //将字符以十六进制方式送入临时变量strtemp存放,注意这里加入一个空隔
				else 
					strtemp.Format("%c",bt); //将字符送入临时变量strtemp存放
		
			   m_strRXData+=strtemp; //加入接收编辑框对应字符串 
			}	
			
//				fwrite(rxdata,1,len,pFile);
				fflush(pFile);
//			test_index=1;
			if(m_ctlRXData.GetLineCount()>100)//大于100行,清空
			{
				m_strRXData.Empty();
				UpdateData(FALSE);
			}

			Sleep(10);
		}
	}
    UpdateData(FALSE); //更新编辑框内容
	fclose(pFile);


}

void CSerialTestView::OnbtnSend() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	m_comm.SetOutput(COleVariant(m_strTXData));//strOutput));
}

BOOL CSerialTestView::OnEraseBkgnd(CDC* pDC) //窗口背景需要擦除时发送消息,
{
	// TODO: Add your message handler code here and/or call default
	CBitmap bitmap;
	bitmap.LoadBitmap(IDB_BITMAP1);

//	BITMAP bmp;
//	bitmap.GetBitmap(&bmp);

	CDC dcCompatible;//兼容的dc
	dcCompatible.CreateCompatibleDC(pDC);

	dcCompatible.SelectObject(&bitmap);//位图选到兼容dc中
	CRect rect;
	GetClientRect(&rect);
//	pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,
//		0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);
	
	pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,
		0,0,SRCCOPY);									//拷贝(1:1)
	return TRUE;
	//return CFormView::OnEraseBkgnd(pDC);
}

void CSerialTestView::OnDestroy() 
{
	CFormView::OnDestroy();
	
	// TODO: Add your message handler code here

	  
	m_strRXData.Empty();
}

⌨️ 快捷键说明

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