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

📄 lihaoview.cpp

📁 一个时时单片机发送数据测试代码
💻 CPP
字号:
// LiHaoView.cpp : implementation of the CLiHaoView class
//

#include "stdafx.h"
#include "LiHao.h"
#include "DrawTest.h"
#include "time.h"

#include "LiHaoSet.h"
#include "LiHaoDoc.h"
#include "LiHaoView.h"

/*#include <windows.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<winioctl.h>
#include "CH365DLL.H"
#define DIN_PORT 0xf1*/


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

/////////////////////////////////////////////////////////////////////////////
// CLiHaoView

mPCH365_IO_REG mIoBase;
mPCH365_CFG_REG mcfgbase;


IMPLEMENT_DYNCREATE(CLiHaoView, CRecordView)

BEGIN_MESSAGE_MAP(CLiHaoView, CRecordView)
	//{{AFX_MSG_MAP(CLiHaoView)
	ON_BN_CLICKED(IDC_CAIJI_STOP, OnCaijiStop)
	ON_BN_CLICKED(IDC_CAIJI_BEGIN, OnCaijiBegin)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_TEST, OnTest)
	ON_BN_CLICKED(IDC_TEST_STOP, OnTestStop)
	ON_EN_CHANGE(IDC_EDIT2, OnChangeEdit2)
	ON_EN_CHANGE(IDC_ROAD_SELECT, OnChangeRoadSelect)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLiHaoView construction/destruction

CLiHaoView::CLiHaoView()
	: CRecordView(CLiHaoView::IDD)
{

	caiji=0;
	for(i=0;i<=400;i++)
	{
		apt[i].x=i;
		apt[i].y=200;
	}


	//{{AFX_DATA_INIT(CLiHaoView)
	m_pSet = NULL;
	m_data1 = 0;
	m_data2 = 0;
	m_iSelect = 0;
	//}}AFX_DATA_INIT
	// TODO: add construction code here

}

CLiHaoView::~CLiHaoView()
{
}

void CLiHaoView::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLiHaoView)
	DDX_Text(pDX, IDC_EDIT1, m_data1);
	DDX_Text(pDX, IDC_EDIT2, m_data2);
	DDX_Text(pDX, IDC_ROAD_SELECT, m_iSelect);
	//}}AFX_DATA_MAP
}

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

	return CRecordView::PreCreateWindow(cs);
}

void CLiHaoView::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_liHaoSet;
	CRecordView::OnInitialUpdate();
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit();

}

/////////////////////////////////////////////////////////////////////////////
// CLiHaoView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CLiHaoView diagnostics

#ifdef _DEBUG
void CLiHaoView::AssertValid() const
{
	CRecordView::AssertValid();
}

void CLiHaoView::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CLiHaoView database support
CRecordset* CLiHaoView::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CLiHaoView message handlers

void CLiHaoView::OnDraw(CDC* pDC) 
{
	// TODO: Add your specialized code here and/or call the base class
	CString str;
	LONG i;
	CPaintDC dc(this);
//	CWnd *pWnd=GetDlgItem(IDC_COORD);
//	pWnd->Invalidate();
//	pWnd->UpdateWindow();

	pDC->Rectangle(0,0,Width,Width);
	CPen *pPenRed=new CPen;
//	CGdiObject *pOldPen;
    pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));//红

	CPen *pPenBlue=new CPen;
    pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0,255));//蓝

	CPen *pPenGreen=new CPen;
	pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));//绿

	CPen *pPenBack=new CPen;
    pPenBack->CreatePen(PS_DOT,1,RGB(128,128,128));//背景色

	pDC->SelectObject(pPenBack);


	pDC->MoveTo(0,0);
	for(i=20;i<=Width;i+=Grid)
	{
		pDC->MoveTo(i,0);
		pDC->LineTo(i,Width);
	}
	for(i=20;i<=Width;i+=Grid)
	{
		pDC->MoveTo(0,i);
		pDC->LineTo(Width,i);
	}

	pDC->SelectObject(pPenBlue);
	pDC->MoveTo(0,300);
	pDC->LineTo(Width,300);


//恢复以前的画笔;
   //pDC->SelectObject(pOldPen);
//删除已经建立的新的画笔
	delete pPenRed;
	delete pPenGreen;
	delete pPenBlue;	
}

void CLiHaoView::OnCaijiStop() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);	
}

UINT CLiHaoView::PointRand(UINT max)
{
	int nRand=rand();
	float fRand=((float)max)*nRand/RAND_MAX;
	return (UINT)fRand;

}

void CLiHaoView::OnCaijiBegin() 
{
	// TODO: Add your control notification handler code here
    UpdateData(TRUE);//从文本框向变量写数
	int a;
	a=m_iSelect;
	if( (a>=1) && (a<=12) )
	{
	   Write_data(a);
	   SetTimer(1,100,NULL);
	}
	else
	 {
	   AfxMessageBox("输入错误,请在1~12之间选择");
  	   KillTimer(1);	
	 }
	UpdateData(FALSE);  //从变量显示出来
}

void CLiHaoView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	CClientDC dc(this);
	DrawBegin(&dc);
	CRecordView::OnTimer(nIDEvent);
}

void CLiHaoView::DrawBegin(CClientDC *pDC)
{
    CPen *pPenRed=new CPen;
	pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
	CPen *pOldpen=pDC->SelectObject(pPenRed);	

	for(i=0;i<=600;i++)
	{
		pDC->MoveTo(apt[i].x,apt[i].y);
		pDC->LineTo(apt[i+1].x,apt[i+1].y);
		for(i=0;i<=600;i++)
		{
		apt[i].x=apt[i+1].x;
		apt[i].y=apt[i+1].y;
		}
		apt[1].y=PointRand(599);
	}
	pDC->SelectObject(pOldpen);
}

void CLiHaoView::OnTest() 
{ 
//	  int i;    
	  Input_data=rand()%0x0100;
	  /*m_data1=Input_data;
      UpdateData(FALSE);*/
	  Write_data(Input_data);
	  data=Read_data();
	  /*m_data2=data;
	  UpdateData(FALSE);*/
	  if(Input_data!=data)
      AfxMessageBox("错误");	
	  else AfxMessageBox("正确");
}

void CLiHaoView::OnTestStop() 
{
	// TODO: Add your control notification handler code here
}

UCHAR CLiHaoView::Read_data()
{
  int i,j;
  UCHAR mByte;      //low
  /*CH365ReadIoByte(&mIoBase->mCh365IoPort[DIN_PORT],&mByte);
  while((mByte&0x40)==1);
  printf("mByte=%7x\n",mByte);*/
  //CH365SetA15_A8(0x80);
  CH365SetA15_A8(0x00);      //A15=0触发单片机中断,a14=0表示要上传
  for(i=0;i<50;i++)for(j=0;j<500;j++);
  printf( "*** CH365ReadIoByte: addr=CH361_IO_BUF, data " );
  if(CH365ReadIoByte(&mIoBase->mCh365IoBuf,&mByte)==FALSE)printf("error");//从CH3656数据缓寸寄存器读数
  m_data2=mByte;
  //Invalidate();
  UpdateData(FALSE);
  CH365SetA15_A8(0xc0);
  return m_data2;
  //return mByte;
}

void CLiHaoView::Write_data(UCHAR data)
{
  int a;
  //long i,j;
  UCHAR mByte;
  
  CH365SetA15_A8(((data<<2)&0x3c)|0x40); //A15为低电平,指示灯亮,此时触发中断,同时a14为高电平,下传。
  
  //for(i=0;i<5000;i++)for(j=0;j<5000;j++);
  a=CH365ReadIoByte(&mIoBase->mCh365IoBuf,&mByte);


  /* a=CH365ReadIoByte(&mIoBase->mCh365IoBuf,&mByte);
  printf("a=%7d\n",a); 
  printf( "mByte= %7x\n", mByte);*/
  
  m_data1=data;
  UpdateData(false);
  //AfxMessageBox("得到mbyte的值!");
  
  CH365SetA15_A8(((data>>2)&0x3c)|0x80);       //A15为高电平,指示灯灭,a14为低电平
  return;
}

void CLiHaoView::OnMenuTest() 
{
	// TODO: Add your command handler code here
}

void CLiHaoView::OnChangeEdit2() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CRecordView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
}

void CLiHaoView::OnChangeRoadSelect() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CRecordView::OnInitDialog()
	// function and call CRichEditCtrl().SetEventMask()
	// with the ENM_CHANGE flag ORed into the mask.
	
	// TODO: Add your control notification handler code here
	
}

void CLiHaoView::OnButton3() 
{
	// TODO: Add your control notification handler code here
	KillTimer(1);
	CDrawTest dlg;
	dlg.DoModal();             //弹出动态绘制曲线的菜单	
	
}

⌨️ 快捷键说明

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