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

📄 11view.cpp

📁 研华板卡通过visual c++多线程进行控制
💻 CPP
字号:
// 11View.cpp : implementation of the CMy11View class
//

#include "stdafx.h"
#include "11.h"

#include "11Doc.h"
#include "11View.h"

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

IMPLEMENT_DYNCREATE(CMy11View, CView)

BEGIN_MESSAGE_MAP(CMy11View, CView)
	//{{AFX_MSG_MAP(CMy11View)
	ON_WM_TIMER()
	ON_WM_PAINT()
	ON_MESSAGE(WM_ITEM_CHANGE,OnItemChange)
	ON_WM_ERASEBKGND()
	//}}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()

/////////////////////////////////////////////////////////////////////////////
// CMy11View construction/destruction

CMy11View::CMy11View()
{
	// TODO: add construction code here
	m_pThread = NULL;
	nVal = 0;
}

CMy11View::~CMy11View()
{
	::TerminateThread(m_pThread,0);
}

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

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CMy11View drawing

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

/////////////////////////////////////////////////////////////////////////////
// CMy11View printing

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy11View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CMy11View message handlers


UINT WINAPI CMy11View::Main_Thread_DI(LPVOID pParam)//控制DI线程
{  
	CMy11View * pView=(CMy11View *)pParam;
	while(1)
	{
		pView->ptDeviceReadDIByte(); 
		pView->nVal++;
		pView->ShowMsg();
		Sleep(10);
	}
	return 0;
}


void CMy11View::OnInitialUpdate() 
{
	CView::OnInitialUpdate();
	
	m_Info = new tagSYSTEMSTATE[64]; 
	for(int i=0;i<64;i++)
	{  
		m_Info[i].nState = 0;
		m_Info[i].bState = FALSE;
	}
	
	unsigned int nDummy;
	m_pThread=(HANDLE) _beginthreadex(NULL,0,Main_Thread_DI,this,CREATE_SUSPENDED,&nDummy);//开辟DI线程 
	
	if (!m_pThread) 
		TRACE(_T(" Couldn't start a  thread\n")); 
	else
		ResumeThread(m_pThread);  
}

void CMy11View::ptDeviceReadDIByte()
{
	SHORT       gnNumOfDevices;
	LONG        DriverHandle = (LONG)NULL;
	USHORT      gwChannel; 
	static      PT_DioReadPortByte ptDioReadPortByte;
	USHORT      gwValue;  
	gnNumOfDevices = 1; 
	DRV_DeviceOpen(gnNumOfDevices,//板号0;
		(LONG far *)&DriverHandle);
	for(gwChannel = 0; gwChannel < 8;gwChannel ++)
	{
		ptDioReadPortByte.port = gwChannel;//////////0-7通道号
		ptDioReadPortByte.value = (USHORT far *)&gwValue;
		DRV_DioReadPortByte(DriverHandle,
			(LPT_DioReadPortByte)&ptDioReadPortByte);
		gwValue=gwValue&0x00ff;/////////////////
		for(int j=0;j<8;j++)
		{
			if( (gwValue & (1<<j)) >0)  
				m_Info[gwChannel*8+j].nState = 1;
			else
				m_Info[gwChannel*8+j].nState = 0;
		}
	}  
	DRV_DeviceClose((LONG far *)&DriverHandle);  
}
 

void CMy11View::OnItemChange(WPARAM wParam, LPARAM lParam)
{
	::AfxMessageBox("sadfasdf");

}

void CMy11View::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
//	Invalidate();
	CView::OnTimer(nIDEvent);
}

void CMy11View::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
/*	
	// TODO: Add your message handler code here 
	CRect rect;
	GetClientRect(&rect);
	CString strText;
	CBrush pNewBrush,*pOldBrush;
	pNewBrush.CreateSolidBrush(RGB(128,128,255));
	pOldBrush=dc.SelectObject(&pNewBrush);
	dc.FillRect(&rect,&pNewBrush);	
	COLORREF *oldCol;
	oldCol=(COLORREF *)dc.SetTextColor(RGB(255,0,0));
	dc.SetBkColor(RGB(128,128,255));
	dc.TextOut(10,10,"采集模拟点显示信息:");
	strText.Format("%d",nVal);
	dc.TextOut(10,25,strText);
	for(int i=0;i<64;i++)
	{
		strText.Format("第%.2d个结点的信息是:%d",i+1,m_Info[i].nState);
		int nHeight= (i/4)*25;
		int nWidth = (i%4)*180; 
		dc.TextOut(10+nWidth,40+nHeight,strText);
	}
	dc.SelectObject(&oldCol);
	dc.SelectObject(&pOldBrush);
	pNewBrush.DeleteObject(); */
	// Do not call CView::OnPaint() for painting messages
}

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

void CMy11View::ShowMsg()
{
	CDC *pDC;
	pDC = GetDC();
	CRect rect;
	GetClientRect(&rect);
	CString strText;
	CBrush pNewBrush,*pOldBrush;
	pNewBrush.CreateSolidBrush(RGB(128,128,255));
	pOldBrush=pDC->SelectObject(&pNewBrush);
	pDC->FillRect(&rect,&pNewBrush);	
	COLORREF *oldCol;
	oldCol=(COLORREF *)pDC->SetTextColor(RGB(255,0,0));
	pDC->SetBkColor(RGB(128,128,255));
	pDC->TextOut(10,10,"采集模拟点显示信息:");
	strText.Format("线程采集程序运行了%d次",nVal);
	pDC->TextOut(10,35,strText);
	for(int i=0;i<64;i++)
	{
		strText.Format("第%.2d个结点的信息是:%d",i+1,m_Info[i].nState);
		int nHeight= (i/4)*25;
		int nWidth = (i%4)*180; 
		pDC->TextOut(10+nWidth,70+nHeight,strText);
	}
	pDC->SelectObject(&oldCol);
	pDC->SelectObject(&pOldBrush);
	pNewBrush.DeleteObject(); 
	pDC->DeleteDC(); 
}

⌨️ 快捷键说明

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