Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6048674
  • 博文数量: 1227
  • 博客积分: 10026
  • 博客等级: 上将
  • 技术积分: 20273
  • 用 户 组: 普通用户
  • 注册时间: 2008-01-16 12:40
文章分类

全部博文(1227)

文章存档

2010年(1)

2008年(1226)

我的朋友

分类: C/C++

2008-04-23 21:53:39

如何建立应用程序和驱动程序间的通信

作者:

  驱动程序是为设备的硬件层编程服务的,但同样需要提供和应用程序进行通信的能力,从而最终达到应用程序控制设备的目的。本文主要讨论应用程序与驱动程序的通信和驱动程序与应用程序的通信。
  在Windows中,应用程序实现与WDM通信的过程是:应用程序先用CreateFile函数打开设备,然后用DeviceIoControl和WDM进行通信,包括从WDM读数据和写数据给WDM两种情况,也可用ReadFile从WDM中读数据或用WriteFile写数据给WDM。当应用程序退出时,用CloseHandle关闭设备。

以下是用DeviceIoControl写数据给WDM的源代码:

void Test_P9052_IOCTL_805_WriteBase3(void)

{

	ULONG	nOutput;						// Count written to bufOutput



	// 传给驱动程序要写入的参数和数据

	// 数组的第一个元素为写入的偏移地址,第二个元素为数据的个数,其它元素为写入的数据

	ULONG	bufInput[IOCTL_INBUF_SIZE 2];	

	ULONG   offset;       // 写入的偏移地址

	ULONG   num;          // 写入的初始数据,以此来产生一个数组

	

	printf("\n----------- ready for writing to Base3 -----------");

	

	//获取写入的偏移地址

	printf("\nPlease input the offset of the write operation(Hex):");

	scanf("%x",&offset);

//	//获取写入的数据个数

	bufInput[0]=offset;

	bufInput[1]=IOCTL_INBUF_SIZE;

	//获取写入的数据

	printf("\nPlease input the initial data to write(Hex):");

	scanf("%x",&num);

	for(ULONG j=0;j

以下是用DeviceIoControl从WDM中读数据的源代码:


void Test_P9052_IOCTL_804_ReadBase3(void)

{

	ULONG	bufOutput[IOCTL_OUTBUF_SIZE];	// 传出读取的数据缓冲区

	ULONG	nOutput;						// 实际读取的数据个数

        ULONG   bufInput[2];	 	            // 传入读取的参数

	ULONG   offset;                         // 要读取的偏移地址

	printf("\n----------- ready for reading from Base3 -----------");

	//获取读取的偏移地址:

	printf("\nPlease input the offset of read operation(Hex):");

	scanf("%x",&offset);

//	//获取读取的数据个数:

//	printf("\nPlease input the number of data to read(Dec):");

//	scanf("%d",&number);

	bufInput[0]=offset;

	bufInput[1]=IOCTL_OUTBUF_SIZE;

	// Call device IO Control interface (PCI9054_IOCTL_804_ReadBase3) in driver

	if (!DeviceIoControl(hDevice,

						 P9052_IOCTL_804_ReadBase3,

						 bufInput,

                         2*4,     // 字节

						 bufOutput,

						 IOCTL_OUTBUF_SIZE*4,//sizeof(bufOutput),

						 &nOutput,

						 NULL)

	   )

	{

		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());

		Exit(1);

	}

    printf("\n---->>>>> data read <<<<<<------");

	for(ULONG i=0;i


驱动程序与应用程序的通信:
与此相对应,当驱动程序捕捉到特点事件(如中断)发生时,应当可以与应用程序进行通信。 以下是通过I/O请求包(IRP),驱动程序的读数据函数:

VOID P9052Device::Serial_P9052_IOCTL_804_ReadBase3_Handler(KIrp I)

{

	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering P9052Device::Serial_P9052_IOCTL_804_ReadBase3_Handler, " << I << EOL;

	KMemory Mem(I.Mdl());     

	// Use the memory object to create a pointer to the caller''s buffer

	PULONG	pOutBuffer	= (PULONG) Mem.MapToSystemSpace();  //输出缓冲区指针,传出读取的数据

	PULONG  pInBuffer       = (PULONG) I.IoctlBuffer();     //输入缓冲区指针

	ULONG   Offset;                     //读取的偏移地址

	Offset   = *pInBuffer;

	ULONG   count;                      //读取的数据个数

	count    = *(pInBuffer 1);

	m_IoPortRange1_ForBase3.ind(Offset,pOutBuffer,count);

	I.Information() = count;

	I.Status() = status;

	m_DriverManagedQueue.PnpNextIrp(I);

}

以下是通过IRP,驱动程序的写数据函数:

VOID P9052Device::Serial_P9052_IOCTL_805_WriteBase3_Handler(KIrp I)

{

	NTSTATUS status = STATUS_SUCCESS;

	t << "Entering P9052Device::Serial_P9052_IOCTL_805_WriteBase3_Handler, " << I << EOL;

	PULONG  pInBuffer  = (PULONG) I.IoctlBuffer(); //输入缓冲区指针

	ULONG   count;                        //从输入缓冲区要写入的数据个数

	count=*(pInBuffer 1);

	ULONG   offset;                       //偏移地址

	offset=*pInBuffer;

	PULONG  pBuffer  = pInBuffer 2;       //指向要写入的数据

	m_IoPortRange1_ForBase3.outd(offset,pBuffer,count);

	I.Information() = count;

	I.Status() = status;

	m_DriverManagedQueue.PnpNextIrp(I);

}

下图是运行时的界面:


图一 VC运行界面

  本文通过DriverWorks实现对PCI9052的数据写入、读出。读写方式是DirectIO方式。这是一种简单的方式,速度只能达到几M,如果是大批量的数据写入可以采用buffer的方式。
阅读(246) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~