全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:3791
推到 Plurk!
推到 Facebook!

如何讀取USB的EP1及EP2資料!

尚未結案
chaojoe
一般會員


發表:1
回覆:5
積分:1
註冊:2009-03-11

發送簡訊給我
#1 引用回覆 回覆 發表時間:2009-03-11 14:01:09 IP:59.124.xxx.xxx 訂閱
我現在用HIDREPORT的方式,而EP1讀取的到資料但EP2資料讀到的值是空值,EP2資料確定有送出來,因有用機器抓到資料,
但PC讀到的是空值。
編輯記錄
taishyang 重新編輯於 2009-03-11 14:22:07, 註解 分類成[問題]‧
taishyang
站務副站長


發表:377
回覆:5485
積分:4552
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2009-03-11 15:13:13 IP:118.169.xxx.xxx 訂閱
ReadFile的handle/length對嗎?

===================引 用 chaojoe 文 章===================
我現在用HIDREPORT的方式,而EP1讀取的到資料但EP2資料讀到的值是空值,EP2資料確定有送出來,因有用機器抓到資料,
但PC讀到的是空值。
chaojoe
一般會員


發表:1
回覆:5
積分:1
註冊:2009-03-11

發送簡訊給我
#3 引用回覆 回覆 發表時間:2009-03-11 15:42:19 IP:59.124.xxx.xxx 訂閱
Length確定是對的,但handle有什麼特別的嗎!
taishyang
站務副站長


發表:377
回覆:5485
積分:4552
註冊:2002-10-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2009-03-11 15:51:14 IP:118.169.xxx.xxx 訂閱
不知道你handle怎麼獲得的
你ReadFile可以用Overlap的方式做error check

Help裡面有範例
chaojoe
一般會員


發表:1
回覆:5
積分:1
註冊:2009-03-11

發送簡訊給我
#5 引用回覆 回覆 發表時間:2009-03-11 16:44:36 IP:59.124.xxx.xxx 訂閱
這是部份程式,會不會DevicePath指錯的可能!


void CUsbhidioc::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);
//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,
"");
//Set the members of the overlapped structure.
HIDOverlapped.hEvent = hEventObject;
HIDOverlapped.Offset = 0;
HIDOverlapped.OffsetHigh = 0;
}
}

bool CUsbhidioc::ReadReport(char InBuffer[])
{
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.
*/

if (DeviceDetected == false)
FindTheHID ();
if (DeviceDetected)
{
Result = ReadFile
(ReadHandle,
InputReport,
5,//65, //Capabilities.InputReportByteLength, 08Mar2005 - Capabilities call fails on WinXP
&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,
1000);
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.
CloseHandle(ReadHandle);
CloseHandle(DeviceHandle);
DeviceDetected = FALSE;
break;
}
default:
{
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);
// copy array to destination buffer, stripping off the report ID.
unsigned int i;
for (i=0; i < 5 /*Capabilities.InputReportByteLength*/; i)
InBuffer[i] = InputReport [i 1];
Result = true;
}
else // could not find the device
Result = false;
return Result;
}
chaojoe
一般會員


發表:1
回覆:5
積分:1
註冊:2009-03-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2009-03-11 19:24:31 IP:60.251.xxx.xxx 訂閱
我找了一下發現很有可能&NumberOfBytesRead這個有問題,每次讀EP2的時候這個值都是"0",但EP1就是正常的數值!
會不會我Fireware連續宣告兩個中斷型IN_Read在HID情況下是有問題的。

Result = ReadFile
(ReadHandle,
InputReport,
5,//65, //Capabilities.InputReportByteLength, 08Mar2005 - Capabilities call fails on WinXP
&NumberOfBytesRead,
(LPOVERLAPPED) &HIDOverlapped);

===================引 用 chaojoe 文 章===================
這是部份程式,會不會DevicePath指錯的可能!


void CUsbhidioc::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);
//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,
"");
//Set the members of the overlapped structure.
HIDOverlapped.hEvent = hEventObject;
HIDOverlapped.Offset = 0;
HIDOverlapped.OffsetHigh = 0;
}
}

bool CUsbhidioc::ReadReport(char InBuffer[])
{
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.
*/

if (DeviceDetected == false)
FindTheHID ();
if (DeviceDetected)
{
Result = ReadFile
(ReadHandle,
InputReport,
5,//65, //Capabilities.InputReportByteLength, 08Mar2005 - Capabilities call fails on WinXP
&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,
1000);
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.
CloseHandle(ReadHandle);
CloseHandle(DeviceHandle);
DeviceDetected = FALSE;
break;
}
default:
{
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);
// copy array to destination buffer, stripping off the report ID.
unsigned int i;
for (i=0; i < 5 /*Capabilities.InputReportByteLength*/; i)
InBuffer[i] = InputReport [i 1];
Result = true;
}
else // could not find the device
Result = false;
return Result;
}
taishyang
站務副站長


發表:377
回覆:5485
積分:4552
註冊:2002-10-08

發送簡訊給我
#7 引用回覆 回覆 發表時間:2009-03-12 14:15:15 IP:118.169.xxx.xxx 訂閱
NumberOfBytesRead只是一個結果
0表示你讀回來的資料長度為0
你ReadFile回傳True還是False?




===================引 用 chaojoe 文 章===================
我找了一下發現很有可能&NumberOfBytesRead這個有問題,每次讀EP2的時候這個值都是"0",但EP1就是正常的數值!
會不會我Fireware連續宣告兩個中斷型IN_Read在HID情況下是有問題的。

chaojoe
一般會員


發表:1
回覆:5
積分:1
註冊:2009-03-11

發送簡訊給我
#8 引用回覆 回覆 發表時間:2009-03-12 14:22:19 IP:59.124.xxx.xxx 訂閱
ReadFile回傳False.


===================引 用 taishyang 文 章===================
NumberOfBytesRead只是一個結果
0表示你讀回來的資料長度為0
你ReadFile回傳True還是False?




===================引 用 chaojoe 文 章===================
我找了一下發現很有可能&NumberOfBytesRead這個有問題,每次讀EP2的時候這個值都是"0",但EP1就是正常的數值!
會不會我Fireware連續宣告兩個中斷型IN_Read在HID情況下是有問題的。

taishyang
站務副站長


發表:377
回覆:5485
積分:4552
註冊:2002-10-08

發送簡訊給我
#9 引用回覆 回覆 發表時間:2009-03-12 14:41:03 IP:118.169.xxx.xxx 訂閱
個人推測你的handle取錯了,你該不會跟另外一個endpoint用同一個handle讀資料吧?
chaojoe
一般會員


發表:1
回覆:5
積分:1
註冊:2009-03-11

發送簡訊給我
#10 引用回覆 回覆 發表時間:2009-03-12 15:00:35 IP:60.251.xxx.xxx 訂閱
我endpoint 1及endpoint 2 都是抓取ReadHandle這會發生什麼問題嗎!

ReadHandle=CreateFile
(detailData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,// FILE_FLAG_OVERLAPPED,
NULL);
===================引 用 taishyang 文 章===================
個人推測你的handle取錯了,你該不會跟另外一個endpoint用同一個handle讀資料吧?
HUNK999
初階會員


發表:9
回覆:35
積分:28
註冊:2009-07-23

發送簡訊給我
#11 引用回覆 回覆 發表時間:2009-07-27 17:32:09 IP:220.130.xxx.xxx 未訂閱

程式後面加入 --> int ErrorCode = GetLastError(); 用單步執行 , Check ErrorCode , 看看問題出在哪?

ErrorCode List:

http://blog.csdn.net/psongchao/archive/2007/04/04/1551594.aspx

http://sites.google.com/site/hidlibrary/

===================引 用 chaojoe 文 章===================
我endpoint 1及endpoint 2 都是抓取ReadHandle這會發生什麼問題嗎!

ReadHandle=CreateFile
(detailData->DevicePath,
GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,// FILE_FLAG_OVERLAPPED,
NULL);
===================引 用 taishyang 文 章===================
個人推測你的handle取錯了,你該不會跟另外一個endpoint用同一個handle讀資料吧?
編輯記錄
HUNK999 重新編輯於 2009-08-14 14:36:28, 註解 無‧
系統時間:2017-10-19 15:21:28
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!