STM32与VB上位机的USB通信,无法收到下位机的数据

hyperionnokaze 2012-04-18 03:44:42
Private Sub ReadReport()

'Read data from the device.

Dim Count
Dim NumberOfBytesRead As Long

'Allocate a buffer for the report.
'Byte 0 is the report ID.

Dim ReadBuffer() As Byte
Dim UBoundReadBuffer As Integer

'******************************************************************************
'ReadFile
'Returns: the report in ReadBuffer.
'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.
'******************************************************************************

Dim ByteValue As String

If MyDeviceDetected = False And DriveContion = False Then
lstResults.AddItem "读出失败请先联接"
Exit Sub
End If



'The ReadBuffer array begins at 0, so subtract 1 from the number of bytes.

ReDim ReadBuffer(Capabilities.InputReportByteLength - 1)

'Scroll to the bottom of the list box.

lstResults.ListIndex = lstResults.ListCount - 1

'Do an overlapped ReadFile.
'The function returns immediately, even if the data hasn't been received yet.

Result = ReadFile _
(ReadHandle, _
ReadBuffer(0), _
CLng(Capabilities.InputReportByteLength), _
NumberOfBytesRead, _
HIDOverlapped)
If Result <> 0 Then
Call DisplayResultOfAPICall("ReadFile")

lstResults.AddItem "waiting for ReadFile"

'Scroll to the bottom of the list box.

lstResults.ListIndex = lstResults.ListCount - 1
bAlertable = True

'******************************************************************************
'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 _
(EventObject, _
100)
Call DisplayResultOfAPICall("WaitForSingleObject")

'Find out if ReadFile completed or timeout.

Select Case Result
Case WAIT_OBJECT_0

'ReadFile has completed

lstResults.AddItem "ReadFile completed successfully."
Case WAIT_TIMEOUT

'Timeout

lstResults.AddItem "Readfile timeout"

'Cancel the operation

'*************************************************************
'CancelIo
'Cancels the ReadFile
'Requires the device handle.
'Returns non-zero on success.
'*************************************************************
Result = CancelIo _
(ReadHandle)
lstResults.AddItem "************ReadFile timeout*************"
lstResults.AddItem "CancelIO"
Call DisplayResultOfAPICall("CancelIo")

'The timeout may have been because the device was removed,
'so close any open handles and
'set MyDeviceDetected=False to cause the application to
'look for the device on the next attempt.

'CloseHandle (HIDHandle)
'Call DisplayResultOfAPICall("CloseHandle (HIDHandle)")
'CloseHandle (ReadHandle)
'Call DisplayResultOfAPICall("CloseHandle (ReadHandle)")
'MyDeviceDetected = False
Case Else
lstResults.AddItem "Readfile undefined error"
MyDeviceDetected = False
End Select

lstResults.AddItem " Report ID: " & ReadBuffer(0)
lstResults.AddItem " Report Data:"

lstResults.AddItem ""
For Count = 1 To UBound(ReadBuffer)

'Add a leading 0 to values 0 - Fh.

If Len(Hex$(ReadBuffer(Count))) < 2 Then
ByteValue = "0" & Hex$(ReadBuffer(Count))
Else
ByteValue = Hex$(ReadBuffer(Count))
End If

lstResults.AddItem " " & ByteValue

'Display the received bytes in the text box.

'TextIR.SelStart = Len(TextIR.Text)
'TextIR.SelText = ByteValue & vbCrLf
lstResults.AddItem "" & ByteValue

Next Count

'******************************************************************************
'ResetEvent
'Sets the event object in the overlapped structure to non-signaled.
'Requires a handle to the event object.
'Returns non-zero on success.
'******************************************************************************

'Call ResetEvent(EventObject)
'Call DisplayResultOfAPICall("ResetEvent")
End If
End Sub

参考各种资料写了如上读取数据的函数,为何始终接收不到STM32下位机发出的四字节数据呢
...全文
385 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2012-04-19
  • 打赏
  • 举报
回复
我一般不用 VB 直接写访问 USB HID 设备的代码,很难调通。而是通过一个 VC++ 写的 DLL 来做。

而且,我不用 ReadFile 命令组,而是用 HidD_GetFeature 和 HidD_SetFeature API。
hyperionnokaze 2012-04-19
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
搞个抓usb通信数据包的小软件看看发送和接受是否成功
[/Quote]
用BUS HOUND测试过了,下位机的数据发送是正常的
lilin0503 2012-04-19
  • 打赏
  • 举报
回复
搞个抓usb通信数据包的小软件看看发送和接受是否成功
hyperionnokaze 2012-04-18
  • 打赏
  • 举报
回复
纠正一下,是连驱动都不用另外写...
hyperionnokaze 2012-04-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
很想试一试,怎奈手里没有实物啊。。。
[/Quote]
有人跟我说需要过滤IRP,我看了一下要用C++编程,我记得我是用HID类设备的,连驱动都不需要,有这么纠结么QAQ
贝隆 2012-04-18
  • 打赏
  • 举报
回复
很想试一试,怎奈手里没有实物啊。。。

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧