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

📄 dspctlview.cpp

📁 动态图形显示程序源码
💻 CPP
字号:
// DspCtlView.cpp : implementation of the CDspCtlView class
//

#include "stdafx.h"
#include "DspCtl.h"

#include "DspCtlDoc.h"
#include "DspCtlView.h"
#include "math.h"
#include "DlgPara.h"
#include "ChildFrm.h"
#include "MainFrm.h"

#include "myCtrlBar.h"

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


IMPLEMENT_DYNCREATE(CDspCtlView, CView)

BEGIN_MESSAGE_MAP(CDspCtlView, CView)
	//{{AFX_MSG_MAP(CDspCtlView)
	ON_WM_TIMER()
	ON_WM_SIZE()
	ON_WM_ERASEBKGND()
	ON_COMMAND(ID_START, OnStart)
	ON_COMMAND(ID_SETPARA, OnSetpara)
	ON_COMMAND(ID_STOP, OnStop)
	ON_COMMAND(ID_DRAWLINE, OnDrawline)
	ON_UPDATE_COMMAND_UI(ID_START, OnUpdateStart)
	ON_UPDATE_COMMAND_UI(ID_STOP, OnUpdateStop)
	ON_MESSAGE(WM_COMMNOTIFY,OnCommNotify)
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CDspCtlView construction/destruction

CDspCtlView::CDspCtlView()
{
mydlgpara=new CDlgPara(this);
canSize=FALSE;

}

CDspCtlView::~CDspCtlView()
{	delete mydlgpara;
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDspCtlView drawing

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

/////////////////////////////////////////////////////////////////////////////
// CDspCtlView printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDspCtlView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CDspCtlView message handlers

void CDspCtlView::OnTimer(UINT nIDEvent) 
{
OnDrawline();
CView::OnTimer(nIDEvent);
}

class CChildFrame;
void CDspCtlView::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	CChildFrame* pwnd=(CChildFrame*)GetParentFrame();
	pwnd->MDIMaximize();

	// TODO: Add your specialized code here and/or call the base class
	CRect Rect;
	GetClientRect(Rect);
	m_Plot.Create(WS_CHILD|WS_VISIBLE,Rect,this,12000);

	m_Plot.SetSerie(0, PS_SOLID, RGB(255,120,0), 0.0, 2000.0, "Pressure");
	m_Plot.SetSerie(1, PS_SOLID, RGB(120,255,0), 0.0, 2000.0, "Pressure");
	m_Plot.SetSerie(2, PS_SOLID, RGB(120,120,255), 0.0, 2000.0, "Pressure");
//	m_Plot.SetSerie(3, PS_SOLID, RGB(255,255,0), 0.0, 2000.0, "Pressure");

	m_Plot.SetLegend(0, PS_SOLID, RGB(255,120,0), "A相电流");
	m_Plot.SetLegend(1, PS_SOLID, RGB(120,255,0), "B相电流");
	m_Plot.SetLegend(2, PS_SOLID, RGB(120,120,255), "转子位置");
//	m_Plot.SetLegend(3, PS_SOLID, RGB(255,255,0), "温度");

	m_Plot.m_bAutoScrollX=TRUE;
	canSize=TRUE;
	bStart=true;
  
}

void CDspCtlView::OnSize(UINT nType, int cx, int cy) 
{	CView::OnSize(nType, cx, cy);
	if(canSize){
		CRect Rect;
		GetClientRect(Rect);
		m_Plot.MoveWindow(Rect);
	}
}

BOOL CDspCtlView::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	
	return FALSE;
}

void CDspCtlView::OnStart() 
{/////////////start
    CDspCtlDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pDoc->m_bConnected=true;
	pDoc->CreateComm();
	btn=(CAnimateBtn* )(((CMainFrame*)(AfxGetApp()->m_pMainWnd))->m_wndWorkspace.btn);
	btn->SetWindowText("启动");
	btn->Play();
   //////////////dizhi
   /*Sleep(1000);
	unsigned char x4[9];
	int temp1;
    x4[0]=0x9;
	temp1=0x0207;
	x4[1]=temp1&255;
    x4[2]=(temp1&65280)/256;
     temp1=0x0208;
	x4[3]=temp1&255;
    x4[4]=(temp1&65280)/256;
    temp1=0x0211;
	x4[5]=temp1&255;
    x4[6]=(temp1&65280)/256;
	temp1=0x0212;
	x4[7]=temp1&255;
    x4[8]=(temp1&65280)/256;
	CreateComm2();
   WriteData(x4,9);
   CloseHandle(hCom);
  */
}


void CDspCtlView::OnSetpara() 
{
	if(mydlgpara->GetSafeHwnd()==0)
		mydlgpara->CreateModelessDlg();
}

void CDspCtlView::OnStop() 
{   CDspCtlDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if(!pDoc->m_bConnected) return;
	pDoc->m_bConnected=false;
   unsigned char x1[2];
	x1[0]=0x3;
	x1[1]=0x0;
	pDoc->WriteData(x1,2);
	btn=(CAnimateBtn* )(((CMainFrame*)(AfxGetApp()->m_pMainWnd))->m_wndWorkspace.btn);
	btn->Stop();
	SetCommMask(pDoc->m_hCom,0);
//终止串口线程
	TerminateThread(pDoc->m_pThread->m_hThread,0);
	WaitForSingleObject(pDoc->m_pThread->m_hThread,INFINITE);
		pDoc->m_pThread=NULL;

	CloseHandle(pDoc->m_hCom);
}

void CDspCtlView::OnDrawline() 
{
	CDspCtlDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
//	TRACE("buf=%d,%d,%d,%d,%d,%d,%d,%d,%d\n",pDoc->buf[0],pDoc->buf[1],pDoc->buf[2],pDoc->buf[3],pDoc->buf[4],pDoc->buf[5],pDoc->buf[6],pDoc->buf[7],pDoc->buf[8]);
//从接受缓冲区读取9个字节
	char str[100];
	double y1;
	double y2;
	double y3;
	for(int i=0;i<200;i++)
	{  
		if(pDoc->buf[1+6*i]>128) 
		{
			y1=-(0xffff-(pDoc->buf[0+6*i]+pDoc->buf[1+6*i]*pow(2,8))+1);
			y1=y1/pow(2,12)*100+800;
		}
		else
			y1=(pDoc->buf[0+6*i]+pDoc->buf[1+6*i]*pow(2,8))/pow(2,12)*100+800;

			if(pDoc->buf[3+6*i]>128) 
		{
			y2=-(0xffff-(pDoc->buf[2+6*i]+pDoc->buf[3+6*i]*pow(2,8))+1);
			y2=y2/pow(2,12)*100+800;
		}
		else
			y2=(pDoc->buf[2+6*i]+pDoc->buf[3+6*i]*pow(2,8))/pow(2,12)*100+800;
      
		if(pDoc->buf[5+6*i]>128) 
		{
			y3=-(0xffff-(pDoc->buf[4+6*i]+pDoc->buf[5+6*i]*pow(2,8))+1);
			y3=y3/2;
			//y3=y3+200;
		}
		else
			//y3=(pDoc->buf[4+6*i]+pDoc->buf[5+6*i]*pow(2,8))/pow(2,12)*100+200;
			y3=(pDoc->buf[4+6*i]+pDoc->buf[5+6*i]*pow(2,8))/2;  
		
		pDoc->dspvalue[0]=y1;
		        //m_Plot.AddPoint(0,  CTime::GetCurrentTime(), y);
    		    //      m_Plot.AddPoint(0,  i, y);
				 
			   pDoc->dspvalue[1]=y2;
		 //m_Plot.AddPoint(1,  CTime::GetCurrentTime(), y);
			  // 	 m_Plot.AddPoint(1,  i, y);
 			   //y3=(pDoc->buf[4+6*i]+pDoc->buf[5+6*i]*pow(2,8));
			    pDoc->dspvalue[2]=y3;
			//m_Plot.AddPoint(2,  CTime::GetCurrentTime(), y);
			  	// 	m_Plot.AddPoint(2,  i, y);
			  if(i%5==0)
			   {	TRACE("i=%d,y1=%f,y2=%f,y3=%f\n",i,y1,y2,y3);
				}
			   m_Plot.AddPointA(y1,y2,y3);
			
			for(int j=0;j<3;j++)
			{
				sprintf(str, "%f", pDoc->dspvalue[j]);
				((CMainFrame*)(AfxGetApp()->m_pMainWnd))->m_wndWorkspace.lstctrl->SetItemText(j, 1, str);
			}
	}
	Invalidate(true);

}

void CDspCtlView::OnUpdateStart(CCmdUI* pCmdUI) 
{	
	CDspCtlDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pCmdUI->Enable(	!pDoc->m_bConnected);
	
}

void CDspCtlView::OnUpdateStop(CCmdUI* pCmdUI) 
{
	CDspCtlDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	pCmdUI->Enable(	pDoc->m_bConnected);	
}


LRESULT CDspCtlView::OnCommNotify(WPARAM wparam,LPARAM lparam)
{
	OnDrawline();
	return 0L;
}





⌨️ 快捷键说明

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