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

📄 app01dlg.cpp

📁 PCI9054应用程序(VC)是用VC开发的对应的应用程序
💻 CPP
字号:
// App01Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "App01.h"
#include "App01Dlg.h"

#include "stdlib.h"      //修改
#include <winioctl.h>
#include "windows.h"
#include "PCI9054ioctl.h"
#include "D:\Program Files\NuMega\DriverStudio\DriverWorks\include\devintf.h"

#include <sys/timeb.h>    //和系统时间相关的头文件
#include <time.h>

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

char *sLinkName = "\\\\.\\PCI9054Device0";
#define	IOCTL_INBUF_SIZE	16384         //写入16384个数据
#define	IOCTL_OUTBUF_SIZE	16384         //读出16384个数据

CString m_strWriteStartTime;
CString m_strWriteEndTime;
CString m_strReadStartTime;
CString m_strReadEndTime;

// 传给驱动程序要写入的参数和数据
// 数组的第一个元素为写入的偏移地址,第二个元素为数据的个数,其它元素为真正要写入的数据
USHORT	bufInput[IOCTL_INBUF_SIZE+2];

USHORT	bufOutput[IOCTL_OUTBUF_SIZE];	// 缓冲区,用于传出读取的数据,
	

/////////////////////////////////////////////////////////////////////////////
// CApp01Dlg dialog

CApp01Dlg::CApp01Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CApp01Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CApp01Dlg)
	//}}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;    //添加行
}

CApp01Dlg::~CApp01Dlg()     //添加段,析构函数
{
	if(m_hDevice!=INVALID_HANDLE_VALUE) CloseHandle(m_hDevice);
}

void CApp01Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CApp01Dlg)
	DDX_Control(pDX, IDC_ListRead, m_listRead);
	DDX_Control(pDX, IDC_ListWrite, m_listWrite);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CApp01Dlg, CDialog)
	//{{AFX_MSG_MAP(CApp01Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_Write, OnWrite)
	ON_BN_CLICKED(IDC_Read, OnRead)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CApp01Dlg message handlers

BOOL CApp01Dlg::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
	
	// TODO: Add extra initialization here

	m_listWrite.InsertColumn(0,"序号",LVCFMT_RIGHT,80);
	m_listWrite.InsertColumn(1,"数据(Hex)",LVCFMT_RIGHT,70);

	m_listRead.InsertColumn(0,"序号",LVCFMT_RIGHT,80);
	m_listRead.InsertColumn(1,"数据(Hex)",LVCFMT_RIGHT,70);


	//添加段
	m_hDevice = CreateFile(sLinkName,
					  GENERIC_READ | GENERIC_WRITE,
					  FILE_SHARE_READ,
					  NULL,
					  OPEN_EXISTING,
					  0,
					  NULL);
	if (m_hDevice == INVALID_HANDLE_VALUE)
	{
		MessageBox("设备打不开","错误",MB_OK|MB_ICONHAND);
	}
	else
	{
		MessageBox("设备成功打开",MB_OK);
	}
	
	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 CApp01Dlg::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 CApp01Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CApp01Dlg::OnWrite() 
{
	ULONG	nOutput;						// Count written to bufOutput
	

	USHORT  offset=0;     // 写入的偏移地址
	USHORT  num=0;          // 写入的初始数据,以此来产生一个数组
	
	
	bufInput[0]=offset;   //将用户指定的写入的偏移地址赋值给bufInput数组的第一个元素

	bufInput[1]=IOCTL_INBUF_SIZE;  //把要写入的数据个数赋值给bufInput数组的第二个元素

	for(ULONG j=0;j<IOCTL_INBUF_SIZE;j++)
	{
		bufInput[2+j]=num;
		num+=1;
	}

	//显示生成的数组
	CString showinfo;
	showinfo.Format("要求写入%d个数据\n写入的偏移地址为:0x %x\n写入的起始数据是:0x %x",
		                bufInput[1],bufInput[0],bufInput[2]);	
	AfxMessageBox(showinfo);

	//获取当前系统时间--------------------------------------
	struct _timeb timebuffer;
	char *timeline;
	//获得毫秒级的时间
	_ftime( &timebuffer );
	timeline = ctime(&(timebuffer.time));
	//格式化时间
	m_strWriteStartTime.Format("写数据起始时间是:%.19s.%hu %s\n", timeline, timebuffer.millitm, &timeline[20]);
	//------------------------------------------------------

	// 调用DeviceIoControl()函数
	if (!DeviceIoControl(m_hDevice,
						 PCI9054_IOCTL_803_WriteBase2,
						 bufInput,
						 (IOCTL_INBUF_SIZE+2)*sizeof(USHORT),  //字节数
						 NULL,
						 0,
						 &nOutput,   //字节数
						 NULL)
	   )
	{
		MessageBox("无法进入驱动程序进行写操作","错误",MB_OK|MB_ICONHAND);
		
	}
	else
	{
		//获取当前系统时间-----------------------------------------
		struct _timeb timebuffer;
		char *timeline;
		//获得毫秒级的时间
		_ftime( &timebuffer );
		timeline = ctime(&(timebuffer.time));
		//格式化时间
		m_strWriteEndTime.Format("写数据终止时间是:%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
		CString showtime;
		showtime.Format(m_strWriteStartTime+m_strWriteEndTime);
		AfxMessageBox(showtime);
		//-------------------------------------------------------------
		
		m_listWrite.DeleteAllItems();

		CListCtrl *pmyListCtrl=(CListCtrl*)GetDlgItem(IDC_ListWrite);

		CString strText;
		
		//显示写入的数据
		for(ULONG i=0;i<IOCTL_INBUF_SIZE;i++)
		{

			strText.Format(TEXT("No.%d"), i);

			// Insert the item, select every other item.
			pmyListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE, i, strText, 
					(i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,0, 0);
		
			// Initialize the text of the subitems.
			for (int j=1;j<3;j++)
			{
				strText.Format(TEXT("%d"), bufInput[i+2]);
				pmyListCtrl->SetItemText(i, j, strText);
			}
		}


	}  // 调用DeviceIoControl()函数 结束

	
}

void CApp01Dlg::OnRead() 
{

	ULONG	nOutput;		   // 实际读取的数据字节数

    USHORT   bufInput[2];	 	    // 传入读取的参数,其中第一个元素为指定的读取偏移地址,
	                                // 第二个元素为指定的读取的数据个数

	USHORT   offset=0;   // 定义的变量,用于存放要读取的偏移地址


	//把获取的读取偏移地址赋值给bufInput第一个元素
	bufInput[0]=offset;    

    //把要读取的数据个数赋值给bufInput第二个元素
	bufInput[1]=IOCTL_OUTBUF_SIZE;  

	//显示生成的数组
	CString showinfo;
	showinfo.Format("要求读出%d个数据\n读取的偏移地址为:0x %x",
		                bufInput[1],bufInput[0]);	
	AfxMessageBox(showinfo);

	//获取当前系统时间--------------------------------------
	struct _timeb timebuffer;
	char *timeline;
	//获得毫秒级的时间
	_ftime( &timebuffer );
	timeline = ctime(&(timebuffer.time));
	//格式化时间
	m_strReadStartTime.Format("读数据起始时间是:%.19s.%hu %s\n", timeline, timebuffer.millitm, &timeline[20]);
	//------------------------------------------------------

	// 调用DeviceIoControl()函数
	if (!DeviceIoControl(m_hDevice,
						 PCI9054_IOCTL_802_ReadBase2,
						 bufInput,
                         2*sizeof(USHORT),     // 字节数,为数组bufInput的大小
						 bufOutput,
						 IOCTL_OUTBUF_SIZE*sizeof(USHORT),    // 字节数
						 &nOutput,
						 NULL)
	   )
	{
		MessageBox("无法进入驱动程序进行读操作","错误",MB_OK|MB_ICONHAND);
	}
	else
	{

		//获取当前系统时间--------------------------------------
		struct _timeb timebuffer;
		char *timeline;
		//获得毫秒级的时间
		_ftime( &timebuffer );
		timeline = ctime(&(timebuffer.time));
		//格式化时间
		m_strReadEndTime.Format("读数据终止时间是:%.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20]);
		CString showtime;
		showtime.Format(m_strReadStartTime+m_strReadEndTime);
		AfxMessageBox(showtime);
		//------------------------------------------------------


		m_listRead.DeleteAllItems();

		CListCtrl *pmyListCtrl=(CListCtrl*)GetDlgItem(IDC_ListRead);

		CString strText;
		
		//显示读出的数据
		for(ULONG i=0;i<nOutput/sizeof(USHORT);i++)
		{

			strText.Format(TEXT("No.%d"), i);

			// Insert the item, select every other item.
			pmyListCtrl->InsertItem(LVIF_TEXT|LVIF_STATE, i, strText, 
					(i%2)==0 ? LVIS_SELECTED : 0, LVIS_SELECTED,0, 0);
		
			// Initialize the text of the subitems.
			for (int j=1;j<3;j++)
			{
				strText.Format(TEXT("%d"), bufOutput[i]);
				pmyListCtrl->SetItemText(i, j, strText);
			}
		}


	} //调用DeviceIoControl()函数结束

	
}

⌨️ 快捷键说明

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