📄 winusbdevice.vb
字号:
''' <summary>
''' Gets a value that corresponds to a USB_DEVICE_SPEED.
''' </summary>
Friend Function QueryDeviceSpeed() As Boolean
Dim bResult As Boolean
Dim length As UInt32 = 1
Dim speed As Byte
'***
' winusb function
' summary
' Get the device speed.
' (Normally not required but can be nice to know.)
' parameters
' Handle returned by WinUsb_Initialize
' Requested information type.
' Number of bytes to read.
' Information to be returned.
' returns
' True on success.
'***
bResult = WinUsb_QueryDeviceInformation _
(myDevInfo.winUsbHandle, _
DEVICE_SPEED, _
length, _
speed)
If bResult Then
myDevInfo.devicespeed = speed
End If
Return bResult
End Function
''' <summary>
''' Attempts to read data from a bulk IN endpoint.
''' </summary>
'''
''' <param name="PipeID"> Endpoint address. </param>
''' <param name="bytesToRead"> Number of bytes to read. </param>
''' <param name="Buffer"> Buffer for storing the bytes read. </param>
''' <param name="bytesRead"> Number of bytes read. </param>
''' <param name="success"> Success or failure status. </param>
'''
Friend Sub ReadViaBulkTransfer _
(ByVal pipeID As Byte, _
ByVal bytesToRead As UInt32, _
ByRef buffer() As Byte, _
ByRef bytesRead As UInt32, _
ByRef success As Boolean)
Try
'***
' winusb function
' summary
' Attempts to read data from a device interface.
' parameters
' Device handle returned by WinUsb_Initialize.
' Endpoint address.
' Buffer to store the data.
' Maximum number of bytes to return.
' Number of bytes read.
' Null pointer for non-overlapped.
' Returns
' True on success.
'***
success = WinUsb_ReadPipe _
(myDevInfo.winUsbHandle, _
pipeID, _
buffer(0), _
bytesToRead, _
bytesRead, _
IntPtr.Zero)
If Not success Then
CloseDeviceHandle()
End If
Catch ex As Exception
Throw
End Try
End Sub
''' <summary>
''' Attempts to read data from an interrupt IN endpoint.
''' </summary>
'''
''' <param name="PipeID"> Endpoint address. </param>
''' <param name="bytesToRead"> Number of bytes to read. </param>
''' <param name="Buffer"> Buffer for storing the bytes read. </param>
''' <param name="bytesRead"> Number of bytes read. </param>
''' <param name="success"> Success or failure status. </param>
'''
Friend Sub ReadViaInterruptTransfer _
(ByVal pipeID As Byte, _
ByVal bytesToRead As UInt32, _
ByRef buffer() As Byte, _
ByRef bytesRead As UInt32, _
ByRef success As Boolean)
Try
'***
' winusb function
' summary
' Attempts to read data from a device interface.
' parameters
' Device handle returned by WinUsb_Initialize.
' Endpoint address.
' Buffer to store the data.
' Maximum number of bytes to return.
' Number of bytes read.
' Null pointer for non-overlapped.
' Returns
' True on success.
'***
success = WinUsb_ReadPipe _
(myDevInfo.winUsbHandle, _
pipeID, _
buffer(0), _
bytesToRead, _
bytesRead, _
IntPtr.Zero)
If Not success Then
CloseDeviceHandle()
End If
Catch ex As Exception
Throw
End Try
End Sub
''' <summary>
''' Attempts to send data via a bulk OUT endpoint.
''' </summary>
'''
''' <param name="buffer"> Buffer containing the bytes to write. </param>
''' <param name="bytesToWrite"> Number of bytes to write. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
Function SendViaBulkTransfer(ByVal buffer As Byte(), ByVal bytesToWrite As UInt32) As Boolean
Dim bResult As Boolean
Dim bytesWritten As UInt32
Try
'***
' winusb function
' summary
' Attempts to write data to a device interface.
' parameters
' Device handle returned by WinUsb_Initialize.
' Endpoint address.
' Buffer with data to write.
' Number of bytes to write.
' Number of bytes written.
' IntPtr.Zero for non-overlapped I/O.
' Returns
' True on success.
' ***
bResult = WinUsb_WritePipe _
(myDevInfo.winUsbHandle, _
CByte(myDevInfo.bulkOutPipe), _
buffer(0), _
bytesToWrite, _
bytesWritten, _
IntPtr.Zero)
If Not bResult Then
CloseDeviceHandle()
End If
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Attempts to send data via an interrupt OUT endpoint.
''' </summary>
'''
''' <param name="buffer"> Buffer containing the bytes to write. </param>
''' <param name="bytesToWrite"> Number of bytes to write. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
Friend Function SendViaInterruptTransfer(ByVal buffer As Byte(), ByVal bytesToWrite As UInt32) As Boolean
Dim bResult As Boolean
Dim bytesWritten As UInt32
Try
'***
' winusb function
' summary
' Attempts to write data to a device interface.
' parameters
' Device handle returned by WinUsb_Initialize.
' Endpoint address.
' Buffer with data to write.
' Number of bytes to write.
' Number of bytes written.
' IntPtr.Zero for non-overlapped I/O.
' Returns
' True on success.
' ***
bResult = WinUsb_WritePipe _
(myDevInfo.winUsbHandle, _
CByte(myDevInfo.interruptOutPipe), _
buffer(0), _
bytesToWrite, _
bytesWritten, _
IntPtr.Zero)
If Not bResult Then
CloseDeviceHandle()
End If
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Sets pipe policy.
''' Used when the value parameter is a byte (all except PIPE_TRANSFER_TIMEOUT).
''' </summary>
'''
''' <param name="pipeId"> Pipe to set a policy for. </param>
''' <param name="policyType"> POLICY_TYPE member. </param>
''' <param name="value"> Policy value. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
'''
Private Function SetPipePolicy(ByVal pipeId As Byte, ByVal policyType As UInt32, ByVal value As Boolean) _
As Boolean
Dim bResult As Boolean
Try
'***
' winusb function
' summary
' sets a pipe policy
' parameters
' handle returned by WinUsb_Initialize
' identifies the pipe
' POLICY_TYPE member.
' length of value in bytes
' value to set for the policy.
' returns
' True on success
'***
bResult = WinUsb_SetPipePolicy _
(myDevInfo.winUsbHandle, _
pipeId, _
policyType, _
1, _
CByte(value))
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Sets pipe policy.
''' Used when the value parameter is a UInt32 (PIPE_TRANSFER_TIMEOUT only).
''' </summary>
'''
''' <param name="pipeId"> Pipe to set a policy for. </param>
''' <param name="policyType"> POLICY_TYPE member. </param>
''' <param name="value"> Policy value. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
'''
Friend Function SetPipePolicy(ByVal pipeId As Byte, ByVal policyType As UInt32, ByVal value As UInt32) _
As Boolean
Dim bResult As Boolean
Try
'***
' winusb function
' summary
' sets a pipe policy
' parameters
' handle returned by WinUsb_Initialize
' identifies the pipe
' POLICY_TYPE member.
' length of value in bytes
' value to set for the policy.
' returns
' True on success
'***
bResult = WinUsb_SetPipePolicy1 _
(myDevInfo.winUsbHandle, _
pipeId, _
policyType, _
4, _
value)
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Is the endpoint's direction IN (device to host)?
''' </summary>
'''
''' <param name="addr"> The endpoint address. </param>
''' <returns>
''' True if IN (device to host), False if OUT (host to device)
''' </returns>
'''
Private Function UsbEndpointDirectionIn(ByVal addr As Integer) As Boolean
Try
If ((addr And &H80) = &H80) Then
UsbEndpointDirectionIn = True
Else
UsbEndpointDirectionIn = False
End If
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Is the endpoint's direction OUT (host to device)?
''' </summary>
'''
''' <param name="addr"> The endpoint address. </param>
'''
''' <returns>
''' True if OUT (host to device, False if IN (device to host)
''' </returns>
Private Function UsbEndpointDirectionOut(ByVal addr As Integer) As Boolean
Try
If ((addr And &H80) = 0) Then
UsbEndpointDirectionOut = True
Else
UsbEndpointDirectionOut = False
End If
Catch ex As Exception
Throw
End Try
End Function
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -