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

📄 app9054dlg.cpp

📁 一个在ddk环境下编的plx9054的驱动程序
💻 CPP
字号:
// App9054Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "App9054.h"
#include "App9054Dlg.h"

#include <fstream.h>


#include <winioctl.h>

#include "..\D9054\D9054DeviceInterface.h"
#include "..\D9054\D9054ioctl.h"


	HANDLE OpenByInterface( GUID* pClassGuid, 
							DWORD instance,
							PDWORD pError );

	GUID ClassGuid = D9054Device_CLASS_GUID ;


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

/////////////////////////////////////////////////////////////////////////////
// CApp9054Dlg dialog


//////////////

HANDLE	m_hDevice  ;
HANDLE	m_hEvent  ;

BOOL bOpen = FALSE; 
BOOL bRecord = FALSE;

unsigned long nCount = 0 ;

DWORD a =0;

///////// 服务线程 ////////

DWORD WINAPI ServiceThread(PVOID lParm)
{

	TRACE("进入记录线程。\n");
	
	unsigned long nBytes = 0 ;

	CFile fWrite;
	
	if ( !fWrite.Open("PCI.dat",CFile::modeCreate|
						   CFile::modeWrite) )
	{
		TRACE(" FILE can't be opened!! ");

		CloseHandle(m_hDevice);
		CloseHandle(m_hEvent);

		return 0;
	}

	bRecord = TRUE ;

	ULONG ucBuffer[32768]; // 
	
	nCount = 0 ;

	ZeroMemory( ucBuffer,16384 );
	
	a = GetTickCount();

	while (TRUE)
	{	
		// 立即返回,以检查应用程序是否需要将线程关闭.
		
	
		while (WaitForSingleObject(m_hEvent, 0) != WAIT_OBJECT_0) 
		{

			// 线程的终止由应用程序决定,而不是驱动程序.停止记录时,线程自动退出

			if ( !bRecord ) 		
			{
				TRACE("线程被终止1。\n");
				return 0 ;
			}
		}

		if ( !ReadFile( m_hDevice ,
						ucBuffer,
						16384*sizeof(ULONG),	// ?????????
						&nBytes,
						NULL) )
		{
			
			TRACE("	\n读操作失败,退出线程。\n");

			continue ;

			CloseHandle(m_hDevice);
			CloseHandle(m_hEvent);

			return 0;

		}

//		TRACE("\n	nBytes = %ld B。\n",nBytes );

//		nCount += nBytes ;

		// 显示接受数据量

//		AfxGetMainWnd()->SendMessage( WM_DISPLAY , NULL,NULL );

		fWrite.Write( ucBuffer , nBytes  );
		
	}
	
	ASSERT(FALSE);
}

/////////  ////////

CApp9054Dlg::CApp9054Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CApp9054Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CApp9054Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_hDevice = INVALID_HANDLE_VALUE ;
	m_hEvent = NULL ;

	bOpen = FALSE ;
	bRecord = FALSE ;

	nCount = 0 ;

}

CApp9054Dlg::~CApp9054Dlg()
{
	if( m_hDevice != INVALID_HANDLE_VALUE )
	{
		CloseHandle( m_hDevice );
	}
}

void CApp9054Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CApp9054Dlg)
	DDX_Control(pDX, IDC_STATIC_SIZE, m_csSize);
	DDX_Control(pDX, IDC_BUTTON_START, m_btStart);
	DDX_Control(pDX, ID_BUTTON_STOP, m_btStop);
	DDX_Control(pDX, IDC_STATIC_STATE, m_csState);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CApp9054Dlg, CDialog)
	//{{AFX_MSG_MAP(CApp9054Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
	ON_BN_CLICKED(IDCANCEL, OnCancel)
	ON_BN_CLICKED(ID_BUTTON_STOP, OnButtonStop)
	//}}AFX_MSG_MAP

	ON_MESSAGE(WM_DISPLAY,Display)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CApp9054Dlg message handlers

void CApp9054Dlg::Display()
{

	CString msg ;

	msg.Format( "%lu",nCount );

	m_csSize.SetWindowText(msg);

//	TRACE(" 显示数据。\n");

}

BOOL CApp9054Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CApp9054Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CApp9054Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CApp9054Dlg::OnCancel()
{

}

void CApp9054Dlg::OnButtonStart() 
{	

	// 开启设备

	DWORD Error ;	

	m_hDevice = OpenByInterface( &ClassGuid,0,&Error);

	if( m_hDevice == INVALID_HANDLE_VALUE )
	{
		bOpen = FALSE ;

		m_csState.SetWindowText("设备无法打开.");

		return ;
	}

	TRACE("设备已经打开。\n");

	unsigned long nBytesRead = 0 ;
	
	// 生成事件

	m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);

	if( m_hEvent==NULL)
	{
		CloseHandle(m_hDevice);
		return ;
	}

	TRACE("事件已经生成。\n");	
/*
	// 启动事件
*/
	if ( !DeviceIoControl(
				m_hDevice, 
				EVENT_SET, 
				&m_hEvent, 
				sizeof(m_hEvent),
				NULL,
				0,
				&nBytesRead,
				NULL ))
	{
		CloseHandle(m_hDevice);
		CloseHandle(m_hEvent);

		return ;
	}

	TRACE("	事件已经启动. \n");


	////  启动记录线程

	CWnd* m_cWnd=AfxGetMainWnd();

	DWORD Tid;
			  
	Thread_Service = CreateThread(0, 0x1000, ServiceThread, 0, 0, &Tid);

	////

	m_btStart.EnableWindow( FALSE );
	m_btStop.SetWindowText("停止记录");

	////////

}

void CApp9054Dlg::OnButtonStop() 
{

	/////////终止记录/////////

	TRACE("  OVer!!!\n");

	if( !bRecord )
	{

		CDialog::OnCancel();
		return ;
	}

	if ( bRecord )
	{
		bRecord = FALSE;
	}

	CloseHandle(m_hDevice);
	CloseHandle(m_hEvent);

	m_hDevice=m_hEvent=NULL;

	a = GetTickCount() - a;

	CString ss;

	ss.Format("%d",a);

	MessageBox(ss);
	
	m_btStart.EnableWindow( TRUE );

	m_btStop.SetWindowText("退出");
	/////////终止记录/////////
}
 	

BOOL CApp9054Dlg::DestroyWindow() 
{
	CloseHandle(m_hDevice);
	CloseHandle(m_hEvent);
		
	return CDialog::DestroyWindow();
}

⌨️ 快捷键说明

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