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

📄 frmmain.vb

📁 visual basic 2005 express 写的上位机
💻 VB
📖 第 1 页 / 共 4 页
字号:
        '           : a different thread.
        '           : The routine performs various application-specific functions that
        '           : involve accessing the application's form.

        'Accepts    : action - a string that names the action to perform on the form
        '           : formText - text that the form displays or the code uses for 
        '           : another purpose. Actions that don't use text ignore this parameter.  

        Try

            ' Select an action to perform on the form:

            Select Case action


                Case "EnableCmdOnce"

                    cmdOnce.Enabled = True


                Case Else

            End Select

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

    End Sub


    Private Sub cmdContinuous_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdContinuous.Click

        'Start or stop a series of periodic transfers.
        Try
            If cmdContinuous.Text = "Continuous" Then

                'Start doing periodic transfers.

                'Change the command button to "Cancel Continuous"

                cmdContinuous.Text = "Cancel Continuous"

                'Enable the timer event to trigger a set of transfers.

                tmrContinuousDataCollect.Enabled = True
                Call ReadAndWriteToDevice()
            Else

                'Stop doing continuous transfers.

                'Change the command button to "Continuous"

                cmdContinuous.Text = "Continuous"

                'Disable the timer that triggers the transfers.

                tmrContinuousDataCollect.Enabled = False
            End If

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try
    End Sub


    Private Sub cmdInputReportBufferSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdInputReportBufferSize.Click

        'Set the number of Input reports the host will store.
        Try
            Call SetInputReportBufferSize()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try
    End Sub


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

        'Search for a specific device.
        Try
            Call FindTheHid()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

        ProgressBar1.Enabled = True
    End Sub


    Private Sub cmdOnce_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdOnce.Click

        'Attempt to write a report and read a report.

        OutBuffer(0) = 0
        OutBuffer(1) = 0
        OutBuffer(2) = LEDCommand
        OutBuffer(3) = &H77

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

            cmdOnce.Enabled = False

            Call ReadAndWriteToDevice()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

        ProgressBar1.Value = CInt(InBuffer(3) / 2.56)


    End Sub

    Private Sub DeviceHasChanged()

        'Called if the user changes the Vendor ID or Product ID in the text box.
        Try
            'If a device was previously detected, stop receiving notifications about it.

            If MyDeviceDetected Then
                Call MyDeviceManagement.StopReceivingDeviceNotifications(DeviceNotificationHandle)
            End If

            'Search for the device the next time FindTheHid is called.

            MyDeviceDetected = False

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try
    End Sub

    Private Sub ExchangeFeatureReports()

        'Purpose    : Sends a Feature report, then retrieves one.
        '           : Assumes report ID = 0 for both reports.

        Dim ByteValue As String
        Dim Count As Integer
        Dim InFeatureReportBuffer() As Byte
        Dim OutFeatureReportBuffer() As Byte
        Dim Success As Boolean

        Try
            Dim myInFeatureReport As New Hid.InFeatureReport()
            Dim myOutFeatureReport As New Hid.OutFeatureReport()

            InFeatureReportBuffer = Nothing

            If (MyHID.Capabilities.FeatureReportByteLength > 0) Then

                'The HID has a Feature report.

                'Set the size of the Feature report buffer. 
                'Subtract 1 from the value in the Capabilities structure because 
                'the array begins at index 0.

                ReDim OutFeatureReportBuffer(MyHID.Capabilities.FeatureReportByteLength - 1)


                'Write a report to the device

                Success = myOutFeatureReport.Write(OutFeatureReportBuffer, HIDHandle)

                If Success Then


                    For Count = 1 To UBound(OutFeatureReportBuffer)

                        'Add a leading zero to values from 0 to F.

                        If Len(Hex(OutFeatureReportBuffer(Count))) < 2 Then
                            ByteValue = "0" & Hex(OutFeatureReportBuffer(Count))
                        Else
                            ByteValue = Hex(OutFeatureReportBuffer(Count))
                        End If

                    Next Count

                End If

                'Read a report from the device.

                'Set the size of the Feature report buffer. 
                'Subtract 1 from the value in the Capabilities structure because 
                'the array begins at index 0.

                If (MyHID.Capabilities.FeatureReportByteLength > 0) Then
                    ReDim InFeatureReportBuffer(MyHID.Capabilities.FeatureReportByteLength - 1)
                End If

                'Read a report.
                myInFeatureReport.Read _
                    (ReadHandle, _
                    HIDHandle, _
                    WriteHandle, _
                    MyDeviceDetected, _
                    InFeatureReportBuffer, _
                    Success)

            End If

            cmdOnce.Enabled = True

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

    End Sub


    Private Sub ExchangeInputAndOutputReports()

        'Purpose    : Sends an Outputreport, then retrieves an Input report.
        '           : Assumes report ID = 0 for both reports.

        Dim ByteValue As String
        Dim Count As Integer
        Dim Success As Boolean
        Dim InputReportBuffer() As Byte
        Dim OutputReportBuffer() As Byte

        Try

            Success = False

            'Don't attempt to exchange reports if valid handles aren't available
            '(as for a mouse or keyboard under Windows 2000/XP.)

            If ((ReadHandle <> INVALID_HANDLE_VALUE) And (WriteHandle <> INVALID_HANDLE_VALUE)) Then

                'Don't attempt to send an Output report if the HID has no Output report.

                If (MyHID.Capabilities.OutputReportByteLength > 0) Then

                    'Set the size of the Output report buffer. 
                    'Subtract 1 from the value in the Capabilities structure because 
                    'the array begins at index 0.

                    ReDim OutputReportBuffer(MyHID.Capabilities.OutputReportByteLength - 1)

                    For Count = 0 To (MyHID.Capabilities.OutputReportByteLength - 1)
                        OutputReportBuffer(Count) = OutBuffer(Count)
                    Next

                    'Write a report.

                    Dim myOutputReport As New Hid.OutputReport
                    Success = myOutputReport.Write(OutputReportBuffer, WriteHandle)

                    If Success Then

                        For Count = 1 To (MyHID.Capabilities.OutputReportByteLength - 1)

                            'Add a leading zero to values from 0 to F.

                            If Len(Hex(OutputReportBuffer(Count))) < 2 Then

                                ByteValue = "0" & Hex(OutputReportBuffer(Count))
                            Else
                                ByteValue = Hex(OutputReportBuffer(Count))
                            End If


                        Next Count

                    End If

                End If

                'Read an Input report.

                Success = False

                'Don't attempt to send an Input report if the HID has no Input report.
                '(The HID spec requires all HIDs to have an interrupt IN endpoint,
                'which suggests that all HIDs must support Input reports.)

                If (MyHID.Capabilities.InputReportByteLength > 0) Then

                    'Set the size of the Input report buffer. 
                    'Subtract 1 from the value in the Capabilities structure because 
                    'the array begins at index 0.

                    ReDim InputReportBuffer(MyHID.Capabilities.InputReportByteLength - 1)



                    'Read a report using interrupt transfers.                
                    'To enable reading a report without blocking the main thread, this
                    'application uses an asynchronous delegate.

                    Dim ar As IAsyncResult
                    Dim myInputReport As New Hid.InputReport

                    'Define a delegate for the Read method of myInputReport.

                    Dim MyReadInputReportDelegate As _
                        New ReadInputReportDelegate(AddressOf myInputReport.Read)



                    'The BeginInvoke method calls myInputReport.Read to attempt to read a report.
                    'The method has the same parameters as the Read function,
                    'plus two additional parameters:
                    'GetInputReportData is the callback procedure that executes when the Read function returns.
                    'MyReadInputReportDelegate is the asynchronous delegate object.
                    'The last parameter can optionally be an object passed to the callback.

                    ar = MyReadInputReportDelegate.BeginInvoke _
                        (ReadHandle, _
                        HIDHandle, _
                        WriteHandle, _
                        MyDeviceDetected, _
                        InputReportBuffer, _
                        Success, _
                        New AsyncCallback(AddressOf GetInputReportData), _
                        MyReadInputReportDelegate)


                End If

            End If

            Call ScrollToBottomOfListBox()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

    End Sub


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

        Try
            'Perform shutdown operations.

            Call Shutdown()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try
    End Sub


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

        'Perform startup operations.

        Try
            frmMy = Me
            frmMain.DefInstance.Show()
            Call Startup()

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

    End Sub


    Private Sub GetInputReportBufferSize()

        'Purpose    : Finds and displays the number of Input buffers
        '           : (the number of Input reports the host will store). 

        Dim NumberOfInputBuffers As Integer

        Try
            'Get the number of input buffers.

            MyHID.GetNumberOfInputBuffers _
                (HIDHandle, _
                NumberOfInputBuffers)

            'Display the result in the text box.

            txtInputReportBufferSize.Text = CStr(NumberOfInputBuffers)

        Catch ex As Exception
            Call HandleException(Me.Name, ex)
        End Try

    End Sub


    Private Sub GetInputReportData(ByVal ar As IAsyncResult)

        'Purpose    : Retrieves Input report data and status information.
        '           : This routine is called automatically when myInputReport.Read
        '           : returns.
        '           : Calls several marshaling routines to access the main form.

        'Accepts    : ar - an object containing status information about 
        '           : the asynchronous operation.    


        Dim ByteValue As String
        Dim Count As Integer
        Dim InputReportBuffer As Byte()
        Dim Success As Boolean

        Try

⌨️ 快捷键说明

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