⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 frmmain.vb

📁 microsoft winusb interface for vb
💻 VB
📖 第 1 页 / 共 4 页
字号:
            ' The BeginInvoke method calls MyWinUsbDevice.ReadViaInterruptTransfer to attempt 
            ' to read data. The method has the same parameters as ReadViaInterruptTransfer,
            ' plus two additional parameters:
            ' GetReceivedInterruptData is the callback routine that executes when 
            ' ReadViaInterruptTransfer returns.
            ' MyReadFromDeviceDelegate is the asynchronous delegate object.

            ar = MyReadFromDeviceDelegate.BeginInvoke _
                (CByte(myWinUsbDevice.myDevInfo.interruptInPipe), _
                bytesToRead, _
                buffer, _
                bytesRead, _
                success, _
                New AsyncCallback(AddressOf GetReceivedInterruptData), _
                MyReadFromDeviceDelegate)

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Scroll to the bottom of the list box and trim if needed.
    ''' </summary>

    Private Sub ScrollToBottomOfListBox()

        Try
            Dim count As Integer

            lstResults.SelectedIndex = lstResults.Items.Count - 1

            'If the list box is getting too large, trim its contents.

            If lstResults.Items.Count > 1000 Then

                For count = 1 To 800
                    lstResults.Items.RemoveAt(count)
                Next count

            End If

        Catch ex As Exception
            Throw
        End Try
    End Sub

    ''' <summary>
    ''' Initiates sending data via a bulk transfer, then receiving data via a bulk transfer.
    ''' </summary>

    Private Sub SendAndReceiveViaBulkTransfers()

        Try
            Dim bResult As Boolean
            Dim bytesToSend As UInt32
            Dim dataToSend As String
            Dim databuffer() As Byte
            Dim formText As String
            Dim myEncoder As New System.Text.ASCIIEncoding()

            ' Get data to send from the textbox.

            dataToSend = txtBulkDataToSend.Text

            ' Convert the string to a byte array.

            databuffer = myEncoder.GetBytes(dataToSend)
            bytesToSend = Convert.ToUInt32(databuffer.GetLength(0))

            'If the device hasn't been detected, was removed, or timed out on a previous attempt
            'to access it, look for the device.

            myDeviceDetected = DetectDevice()

            If myDeviceDetected Then

                bResult = myWinUsbDevice.SendViaBulkTransfer(databuffer, bytesToSend)

                If bResult Then
                    formText = "Data sent via bulk transfer."
                Else
                    formText = "Bulk OUT transfer failed."

                End If

                AccessForm("AddItemToListBox", formText)

                ReadDataViaBulkTransfer()

            End If

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Initiates sending data via an interrupt transfer, then retrieving data 
    ''' via an interrupt transfer.
    ''' </summary>

    Private Sub SendAndReceiveViaInterruptTransfers()

        Dim bresult As Boolean
        Dim bytesToSend As UInt32 ' winusb_readpipe requires this parameter to be a UINT32.
        Dim comboBoxText As String
        Dim dataBuffer(1) As Byte
        Dim formText As String

        Try
            ' Get bytes to send from comboboxes.

            ' Get the value to send as a hex string.

            comboBoxText = _
                System.Convert.ToString(cboInterruptOutByte0.SelectedItem).TrimEnd(Nothing)

            ' Convert the string to a byte.

            dataBuffer(0) = _
                    Convert.ToByte(String.Format("{0:X2}", comboBoxText), 16)

            ' Get the value to send as a hex string.

            comboBoxText = _
                System.Convert.ToString(cboInterruptOutByte1.SelectedItem).TrimEnd(Nothing)

            ' Convert the string to a byte.

            dataBuffer(1) = _
                    Convert.ToByte(String.Format("{0:X2}", comboBoxText), 16)

            bytesToSend = Convert.ToUInt32(dataBuffer.GetLength(0))

            'If the device hasn't been detected, was removed, or timed out on a previous attempt
            'to access it, look for the device.

            myDeviceDetected = DetectDevice()

            If myDeviceDetected Then

                bresult = myWinUsbDevice.SendViaInterruptTransfer(dataBuffer, bytesToSend)

                If bresult Then
                    formText = "Data sent via interrupt transfer."
                Else
                    formText = "Interrupt OUT transfer failed."
                End If

                AccessForm("AddItemToListBox", formText)
                ReadDataViaInterruptTransfer()

            End If

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Perform actions that must execute when the program ends.
    ''' </summary>

    Private Sub Shutdown()

        Try
            ' Close an open handle to the device.

            If (myWinUsbDevice.deviceHandle <> 0) Then
                CloseHandle(myWinUsbDevice.deviceHandle)
            End If

            'Stop receiving notifications.

            myDeviceManagement.StopReceivingDeviceNotifications(deviceNotificationHandle)

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Perform actions that must execute when the program starts.
    ''' </summary>

    Private Sub Startup()

        Try
            myWinUsbDevice = New WinUsbDevice()
            InitializeDisplay()

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Calls a routine to initiate a Control Read transfer. (Data stage is device to host.)
    ''' </summary>

    Private Sub cmdControlReadTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdControlReadTransfer.Click

        InitiateControlInTransfer()
    End Sub

    ''' <summary>
    ''' Calls a routine to initiate a Control Write transfer. (Data stage is host to device.)
    ''' </summary>
    '''  
    Private Sub cmdControlWriteTransfer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdControlWriteTransfer.Click

        InitiateControlOutTransfer()

    End Sub

    ''' <summary>
    ''' Search for a specific device.
    ''' </summary>

    Private Sub cmdFindDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFindDevice.Click

        Try
            FindMyDevice()

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Calls a routine to exchange data via bulk transfers.
    ''' </summary>

    Private Sub cmdSendAndReceiveViaBulkTransfers_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSendandRecieveViaBulkTransfers.Click

        Try
            'Don't allow another transfer request until this one completes.

            cmdSendandRecieveViaBulkTransfers.Enabled = False

            SendAndReceiveViaBulkTransfers()

        Catch ex As Exception
            Throw
        End Try
    End Sub

    ''' <summary>
    ''' Calls a routine to exchange data via interrupt transfers.
    ''' </summary>
    ''' 
    Private Sub cmdSendAndReceiveViaInterruptTransfers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSendAndReceiveViaInterruptTransfers.Click

        Try
            'Don't allow another transfer request until this one completes.

            cmdSendAndReceiveViaInterruptTransfers.Enabled = False

            SendAndReceiveViaInterruptTransfers()

        Catch ex As Exception
            Throw
        End Try
    End Sub

    ''' <summary>
    ''' Perform shutdown operations.
    ''' </summary>

    Private Sub frmMain_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed

        Try
            Shutdown()

        Catch ex As Exception
            Throw
        End Try
    End Sub

    ''' <summary>
    ''' Perform startup operations.
    ''' </summary>

    Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load

        Try
            frmMy = Me
            Startup()

        Catch ex As Exception
            Throw
        End Try

    End Sub

    ''' <summary>
    ''' Overrides WndProc to enable checking for and handling
    ''' WM_DEVICECHANGE messages.
    ''' </summary>
    ''' 
    ''' <param name="m"> A Windows message.
    ''' </param> 
    ''' 
    Protected Overrides Sub WndProc(ByRef m As Message)

        Try
            'The OnDeviceChange routine processes WM_DEVICECHANGE messages.

            If m.Msg = WM_DEVICECHANGE Then
                OnDeviceChange(m)
            End If

            'Let the base form process the message.

            MyBase.WndProc(m)

        Catch ex As Exception
            Throw
        End Try

    End Sub

End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -