阅读:950回复:1
打开设备 怎么无法通讯?
我正在做串口设备的驱动开发,类似于一个鼠标,所以我的adddevice 例程中IoCreateDevice(Driver,
sizeof(DEVICE_EXTENSION), NULL, // no name for thisFilter DO FILE_DEVICE_SERIAL_MOUSE_PORT, 0, FALSE, &device); 然后注册设备接口 IoRegisterDeviceInterface(PDO,&Touch_GUID,NULL,&deviceExtension->ifSymLinkName); 启动接口 IoSetDeviceInterfaceState(&deviceExtension->ifSymLinkName,TRUE); 在驱动安装完后我打开设备 HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance); int main(int argc, char* argv[]) { int TestNo = 1; printf("\nWdm1Test\n"); ///////////////////////////////////////////////////////////////////////// // Open device printf("\nTest %d\n",TestNo++); HANDLE hTouch = GetDeviceViaInterface((LPGUID)&Touch_GUID,0); if( hTouch==NULL) { printf("XXX Could not find open Wdm1 device\n"); return 1; } printf(" Opened OK\n"); if( !DeviceIoControl( hTouch, IOCTL_SETB_RECTIFY_FLAG, NULL, 0, // Input NULL, 0, // Output NULL, NULL)) printf("XXX SET THE RECTIFY FLAG FAILURE \n"); else printf(" SET THE RECTIFY FLAG SUCEED "); ///////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////// // Close device printf("\nTest %d\n",TestNo++); if( !CloseHandle(hTouch)) printf("XXX CloseHandle failed %d\n",GetLastError()); else printf(" CloseHandle worked\n"); ///////////////////////////////////////////////////////////////////////// return 0; } ///////////////////////////////////////////////////////////////////////////// // GetDeviceViaInterface: Open a handle via a device interface HANDLE GetDeviceViaInterface( GUID* pGuid, DWORD instance) { // Get handle to relevant device information set HDEVINFO info = SetupDiGetClassDevs(pGuid, NULL, NULL, DIGCF_PRESENT | DIGCF_INTERFACEDEVICE); if(info==INVALID_HANDLE_VALUE) { printf("No HDEVINFO available for this GUID\n"); return NULL; } // Get interface data for the requested instance SP_INTERFACE_DEVICE_DATA ifdata; ifdata.cbSize = sizeof(ifdata); if(!SetupDiEnumDeviceInterfaces(info, NULL, pGuid, instance, &ifdata)) { printf("No SP_INTERFACE_DEVICE_DATA available for this GUID instance\n"); SetupDiDestroyDeviceInfoList(info); return NULL; } // Get size of symbolic link name DWORD ReqLen; SetupDiGetDeviceInterfaceDetail(info, &ifdata, NULL, 0, &ReqLen, NULL); PSP_INTERFACE_DEVICE_DETAIL_DATA ifDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA)(new char[ReqLen]); if( ifDetail==NULL) { SetupDiDestroyDeviceInfoList(info); return NULL; } // Get symbolic link name ifDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA); if( !SetupDiGetDeviceInterfaceDetail(info, &ifdata, ifDetail, ReqLen, NULL, NULL)) { SetupDiDestroyDeviceInfoList(info); delete ifDetail; return NULL; } printf("Symbolic link is %s\n",ifDetail->DevicePath); // Open file HANDLE rv = CreateFile( ifDetail->DevicePath, //GENERIC_READ, GENERIC_WRITE, //0, //0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if( rv==INVALID_HANDLE_VALUE) rv = NULL; delete ifDetail; SetupDiDestroyDeviceInfoList(info); return rv; } 我发现了一个怪问题,再用createdevice打开设备时,不能包括GENERIC_READ,一旦包括了它就无法打开设备,档案上面的程序打开设备后,利用DeviceIoControl( hTouch, IOCTL_SETB_RECTIFY_FLAG,NULL, 0,//input NULL, 0, // Output NULL, NULL))跟设备驱动通讯时却]发现驱动没有姐收到 IRP_MJ_DEVICE_CONTrol消息 NTSTATUS DeviceControl( IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp ) { PIO_STACK_LOCATION irpSp; PDEVICE_EXTENSION deviceExtension; NTSTATUS status; deviceExtension = DeviceObject->DeviceExtension; Irp->IoStatus.Information = 0; DebugPrint("Device io control entered"); irpSp=IoGetCurrentIrpStackLocation(Irp); status = IoAcquireRemoveLock(&deviceExtension->RemoveLock, Irp); if (!NT_SUCCESS(status)) { Irp->IoStatus.Status = status; IoCompleteRequest(Irp, IO_NO_INCREMENT); return status; } switch(irpSp->Parameters.DeviceIoControl.IoControlCode){ case IOCTL_SETB_RECTIFY_FLAG: deviceExtension->CalibrateFlag=InRectify ; DebugPrint("the device is in calibrateflag procession"); status = STATUS_SUCCESS; break; default: status = STATUS_INVALID_DEVICE_REQUEST; break; } Irp->IoStatus.Status = status; IoCompleteRequest(Irp, IO_NO_INCREMENT); IoReleaseRemoveLock(&deviceExtension->RemoveLock, Irp); DebugPrint("device io control exit"); return status; } 不知道原因在那里,那位大虾能给小弟指条明路啊 emai; tianyakeqing@sohu.com qq 147316279 |
|
沙发#
发布于:2004-08-01 12:55
你的驱动是什么?
|
|