STM32与VB上位机的USB通信,无法收到下位机的数据
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下位机发出的四字节数据呢