STM32与VB上位机的USB通信
在毕业设计中涉及USB通信的问题,结果周围的老师都没涉及这块,自己做的很吃力,不知哪位大神可以提供我一个可以使用的USB通信的DLL,跪谢了QAQ
[最优解释]
读写数据可以用API函数:DeviceIoControl
Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, lpInBuffer As Any, ByVal nInBufferSize As Long, lpOutBuffer As Any, ByVal nOutBufferSize As Long, lpBytesReturned As Long, lpOverlapped As OVERLAPPED) As Long
你可以向这方面研究下
此外给你一本经典的USB编程书籍:http://download.csdn.net/detail/veron_04/1673828
[其他解释]
这是这个非常复杂的课题,先搞清楚USB协议,还要搞清楚IO控制,还要搞清楚驱动程序的编写
当然,你可以去寻找某些已经有现成接口的,直接访问接口吧
[其他解释]
我自己已经写过一个HID的模块,只能找到指定的USB设备,读取数据那块做不出来,我现在只是想尽快完成这个毕业设计。
[其他解释]
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
我参考了大家给我推荐的资料,然后写了如上读取数据的函数,为何始终接收不到下位机发出的数据呢?
[其他解释]
我用BUS HOUND能检测到数据的,但是自己写的程序就是接收不到,哪位前辈指点一下啊