📄 winusbdevice.vb
字号:
Option Strict On
Option Explicit On
Imports System.Runtime.InteropServices
'''<summary>
''' Routines for the WinUsb driver supported by Windows Vista and Windows XP.
''' </summary>
'''
Friend Class WinUsbDevice
Friend Structure devInfo
Dim winUsbHandle As Integer
Dim bulkInPipe As Integer
Dim bulkOutPipe As Integer
Dim interruptInPipe As Integer
Dim interruptOutPipe As Integer
Dim devicespeed As UInteger
End Structure
Friend deviceHandle As Integer
Friend myDevInfo As New devInfo
''' <summary>
''' Closes the device handle obtained with CreateFile and frees resources.
''' </summary>
'''
Private Sub CloseDeviceHandle()
Try
If (deviceHandle <> 0) Then
CloseHandle(deviceHandle)
End If
' Free resources.
WinUsb_Free(myDevInfo.winUsbHandle)
Catch ex As Exception
Throw
End Try
End Sub
''' <summary>
''' Initiates a Control Read transfer. Data stage is device to host.
''' </summary>
'''
''' <param name="dataStage"> The received data. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
Friend Function Do_Control_Read_Transfer(ByRef dataStage As Byte()) As Boolean
Dim bResult As Boolean
Dim bytesReturned As UInt32
Dim setupPacket As WINUSB_SETUP_PACKET
Try
' Vendor-specific request to an interface with device-to-host Data stage.
setupPacket.RequestType = &HC1
' The request number that identifies the specific request.
setupPacket.Request = 2
' Command-specific value to send to the device.
setupPacket.Index = 0
' Number of bytes in the request's Data stage.
setupPacket.Length = CUShort(dataStage.Length)
' Command-specific value to send to the device.
setupPacket.Value = 0
'***
' winusb function
' summary
' Initiates a control transfer.
' paramaters
' Device handle returned by WinUsb_Initialize.
' WINUSB_SETUP_PACKET structure
' Buffer to hold the returned Data-stage data.
' Number of data bytes to read in the Data stage.
' Number of bytes read in the Data stage.
' Null pointer for non-overlapped.
' returns
' True on success.
' ***
bResult = WinUsb_ControlTransfer(myDevInfo.winUsbHandle, _
setupPacket, _
dataStage(0), _
CUShort(dataStage.Length), _
bytesReturned, _
IntPtr.Zero)
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Initiates a Control Write transfer. Data stage is host to device.
''' </summary>
'''
''' <param name="dataStage"> The data to send. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
Friend Function Do_Control_Write_Transfer(ByVal dataStage As Byte()) As Boolean
Dim bResult As Boolean
Dim bytesReturned As UInt32
Dim index As UShort = 0
Dim setupPacket As WINUSB_SETUP_PACKET
Dim value As UShort = 0
Try
' Vendor-specific request to an interface with host-to-device Data stage.
setupPacket.RequestType = &H41
' The request number that identifies the specific request.
setupPacket.Request = 1
' Command-specific value to send to the device.
setupPacket.Index = index
' Number of bytes in the request's Data stage.
setupPacket.Length = CUShort(dataStage.Length)
' Command-specific value to send to the device.
setupPacket.Value = value
'***
' winusb function
' summary
' Initiates a control transfer.
' parameters
' Device handle returned by WinUsb_Initialize.
' WINUSB_SETUP_PACKET structure
' Buffer containing the Data-stage data.
' Number of data bytes to send in the Data stage.
' Number of bytes sent in the Data stage.
' Null pointer for non-overlapped.
' Returns
' True on success.
' ***
bResult = WinUsb_ControlTransfer(myDevInfo.winUsbHandle, _
setupPacket, _
dataStage(0), _
CUShort(dataStage.Length), _
bytesReturned, _
IntPtr.Zero)
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Requests a handle with CreateFile.
''' </summary>
'''
''' <param name="devicePathName"> Returned by SetupDiGetDeviceInterfaceDetail
''' in an SP_DEVICE_INTERFACE_DETAIL_DATA structure. </param>
'''
''' <returns>
''' The handle.
''' </returns>
Friend Function GetDeviceHandle(ByVal devicePathName As String) As Boolean
Dim security As SECURITY_ATTRIBUTES
security.lpSecurityDescriptor = 0
security.bInheritHandle = CInt(True)
security.nLength = Len(security)
'***
'API function
' summary
' Retrieves a handle to a device.
' parameters
' Device path name returned by SetupDiGetDeviceInterfaceDetail
' Type of access requested (read/write).
' FILE_SHARE attributes to allow other processes to access the device while this handle is open.
' Security structure. Using Null for this may cause problems under Windows XP.
' Creation disposition value. Use OPEN_EXISTING for devices.
' Flags and attributes for files. The winsub driver requires FILE_FLAG_OVERLAPPED.
' Handle to a template file. Not used.
' Returns
' Handle
'***
deviceHandle = CreateFile _
(devicePathName, _
GENERIC_WRITE Or GENERIC_READ, _
FILE_SHARE_READ Or FILE_SHARE_WRITE, _
security, _
OPEN_EXISTING, _
FILE_ATTRIBUTE_NORMAL Or FILE_FLAG_OVERLAPPED, _
0)
End Function
''' <summary>
''' Initializes a device interface and obtains information about it.
''' Calls these winusb API functions:
''' WinUsb_Initialize
''' WinUsb_QueryInterfaceSettings
''' WinUsb_QueryPipe
''' </summary>
'''
''' <param name="deviceHandle"> A handle obtained in a call to winusb_initialize. </param>
'''
''' <returns>
''' True on success, False on failure.
''' </returns>
Function InitializeDevice(ByVal deviceHandle As Integer) As Boolean
Dim bResult As Boolean
Dim ifaceDescriptor As USB_INTERFACE_DESCRIPTOR
Dim pipeInfo As WINUSB_PIPE_INFORMATION
Dim pipeTimeout As UInt32 = 2000
Try
'***
' winusb function
' summary
' get a handle for communications with a winusb device '
' parameters
' Handle returned by CreateFile.
' Device handle to be returned.
' returns
' True on success.
' ***
bResult = WinUsb_Initialize(deviceHandle, myDevInfo.winUsbHandle)
If bResult Then
'***
' winusb function
' summary
' Get a structure with information about the device interface.
' parameters
' handle returned by WinUsb_Initialize
' alternate interface setting number
' USB_INTERFACE_DESCRIPTOR structure to be returned.
' returns
' True on success.
bResult = WinUsb_QueryInterfaceSettings _
(myDevInfo.winUsbHandle, _
0, _
ifaceDescriptor)
If bResult Then
' Get the transfer type, endpoint number, and direction for the interface's
' bulk and interrupt endpoints. Set pipe policies.
'***
' winusb function
' summary
' returns information about a USB pipe (endpoint address)
' parameters
' Handle returned by WinUsb_Initialize
' Alternate interface setting number
' Number of an endpoint address associated with the interface.
' (The values count up from zero and are NOT the same as the endpoint address
' in the endpoint descriptor.)
' WINUSB_PIPE_INFORMATION structure to be returned
' returns
' True on success
'***
For i As Integer = 0 To ifaceDescriptor.bNumEndpoints - 1
WinUsb_QueryPipe(myDevInfo.winUsbHandle, 0, CByte(i), pipeInfo)
If ((pipeInfo.PipeType = USBD_PIPE_TYPE.UsbdPipeTypeBulk) And _
UsbEndpointDirectionIn(pipeInfo.PipeId)) Then
myDevInfo.bulkInPipe = pipeInfo.PipeId
SetPipePolicy _
(CByte(myDevInfo.bulkInPipe), _
CUInt(POLICY_TYPE.IGNORE_SHORT_PACKETS), _
False)
SetPipePolicy _
(CByte(myDevInfo.bulkInPipe), _
CUInt(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), _
pipeTimeout)
ElseIf ((pipeInfo.PipeType = USBD_PIPE_TYPE.UsbdPipeTypeBulk) And _
UsbEndpointDirectionOut(pipeInfo.PipeId)) Then
myDevInfo.bulkOutPipe = pipeInfo.PipeId
SetPipePolicy _
(CByte(myDevInfo.bulkOutPipe), _
CUInt(POLICY_TYPE.IGNORE_SHORT_PACKETS), _
False)
SetPipePolicy _
(CByte(myDevInfo.bulkOutPipe), _
CUInt(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), _
pipeTimeout)
ElseIf (pipeInfo.PipeType = USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) And _
UsbEndpointDirectionIn(pipeInfo.PipeId) Then
myDevInfo.interruptInPipe = pipeInfo.PipeId
SetPipePolicy _
(CByte(myDevInfo.interruptInPipe), _
CUInt(POLICY_TYPE.IGNORE_SHORT_PACKETS), _
False)
SetPipePolicy _
(CByte(myDevInfo.interruptInPipe), _
CUInt(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), _
pipeTimeout)
ElseIf (pipeInfo.PipeType = USBD_PIPE_TYPE.UsbdPipeTypeInterrupt) And _
UsbEndpointDirectionOut(pipeInfo.PipeId) Then
myDevInfo.interruptOutPipe = pipeInfo.PipeId
SetPipePolicy _
(CByte(myDevInfo.interruptOutPipe), _
CUInt(POLICY_TYPE.IGNORE_SHORT_PACKETS), _
False)
SetPipePolicy _
(CByte(myDevInfo.interruptOutPipe), _
CUInt(POLICY_TYPE.PIPE_TRANSFER_TIMEOUT), _
pipeTimeout)
End If
Next i
Else
bResult = False
End If
End If
Return bResult
Catch ex As Exception
Throw
End Try
End Function
''' <summary>
''' Is the current operating system is Windows XP or later?
''' The WinUSB driver requires Windows XP or later.
''' </summary>
'''
''' <returns>
''' True if Windows XP or later, False if not.
''' </returns>
Friend Function IsWindowsXpOrLater() As Boolean
Try
Dim myEnvironment As OperatingSystem = Environment.OSVersion
' Windows XP is version 5.1.
Dim versionXP As New System.Version(5, 1)
If (Version.op_GreaterThanOrEqual(myEnvironment.Version, versionXP) = True) Then
Return True
Else
Return False
End If
Catch ex As Exception
Throw
End Try
End Function
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -