分类: C/C++
2008-03-17 13:42:22
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的方式。