阅读:2169回复:5
用VC编写与HID设备通信程序遇到问题!
大家好,现在情况是这样的,HID设备可以正常枚举并且Windows提示可以使用,我也基本知道了在 VC中如何访问HID设备,但是现在的问题是,CreateFile之后可以得到正确的句炳,并且HidD_GetAttributes,HidD_GetPreparsedData以及HidP_GetCaps都正确得到了预期数据,但是我试图用WriteFile()或者HidD_SetOutputReport()时就提示出错,我查了出错代码是"invalid function",请问这是什么原因啊,实现搞不明白?PS:我上MSDN看了,步骤都是这样的,怎么会invalid呢?这是应该跟固件没有关系吧?是跟系统有关吗,我的是XP?
|
|
沙发#
发布于:2007-06-26 20:05
源代码如下图:
|
|
板凳#
发布于:2007-06-28 14:56
难道这里就没有人知道吗,我在网上搜了很多贴基本上都没有这个问题的正确答案,高手来帮帮忙吧,急啊!
|
|
地板#
发布于:2007-06-29 14:28
前面看都是对的,试试这么读呢
。。。。 PrepareForOverlappedTransfer(); ReadReport() 。。。。 /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// void CUsbhidiocDlg::PrepareForOverlappedTransfer() { //Get another handle to the device for the overlapped ReadFiles. ReadHandle=CreateFile (detailData->DevicePath, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, (LPSECURITY_ATTRIBUTES)NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); DisplayLastError("CreateFile (ReadHandle): "); //Get an event object for the overlapped structure. /*API function: CreateEvent Requires: Security attributes or Null Manual reset (true). Use ResetEvent to set the event object's state to non-signaled. Initial state (true = signaled) Event object name (optional) Returns: a handle to the event object */ if (hEventObject == 0) { hEventObject = CreateEvent (NULL, TRUE, TRUE, ""); DisplayLastError("CreateEvent: ") ; //Set the members of the overlapped structure. HIDOverlapped.hEvent = hEventObject; HIDOverlapped.Offset = 0; HIDOverlapped.OffsetHigh = 0; } } ///////////////////////////////////////////////////////////////////////////////////////// void CUsbhidiocDlg::ReadReport() { CString ByteToDisplay = ""; CString MessageToDisplay = ""; DWORD Result; //Read a report from the device. /*API call:ReadFile 'Returns: the report in InputReport. 'Requires: a device handle returned by CreateFile '(for overlapped I/O, CreateFile must be called with FILE_FLAG_OVERLAPPED), 'the Input report length in bytes returned by HidP_GetCaps, 'and an overlapped structure whose hEvent member is set to an event object. */ Result = ReadFile (ReadHandle, InputReport, Capabilities.InputReportByteLength, &NumberOfBytesRead, (LPOVERLAPPED) &HIDOverlapped); /*API call:WaitForSingleObject 'Used with overlapped ReadFile. 'Returns when ReadFile has received the requested amount of data or on timeout. 'Requires an event object created with CreateEvent 'and a timeout value in milliseconds. */ Result = WaitForSingleObject (hEventObject, 20);//?600 switch (Result) { case WAIT_OBJECT_0: { break; } case WAIT_TIMEOUT: { //Cancel the Read operation. /*API call: CancelIo Cancels the ReadFile Requires the device handle. Returns non-zero on success. */ Result = CancelIo(ReadHandle); //A timeout may mean that the device has been removed. //Close the device handles and set DeviceDetected = False //so the next access attempt will search for the device. break; default: ValueToDisplay.Format("%s", "Undefined error"); break; } } /* API call: ResetEvent Sets the event object to non-signaled. Requires a handle to the event object. Returns non-zero on success. */ ResetEvent(hEventObject); //Display the report data. DisplayInputReport(); } |
|
地下室#
发布于:2007-06-29 14:30
写
Result = WriteFile (DeviceHandle, OutputReport, Capabilities.OutputReportByteLength, &BytesWritten, NULL); if (Result == 0) { //The WriteFile failed, so close the handle, display a message, //and set DeviceDetected to FALSE so the next attempt will look for the device. CloseHandle(DeviceHandle); CloseHandle(ReadHandle); DisplayData("Can't write to device"); DeviceDetected = FALSE; } |
|
5楼#
发布于:2007-06-29 14:32
上面是改网上找的HID VC例子,用来读HID遥控器,正常工作。
|
|