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

📄 frmmain.vb

📁 visual basic 2005 express 写的上位机
💻 VB
📖 第 1 页 / 共 4 页
字号:
        '    Call ReadAndWriteToDevice()
        'Else
        '    cmdContinuous.Text = "Continuous"
        '    tmrContinuousDataCollect.Enabled = False
        'End If

        
    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.

        Call SetInputReportBufferSize()

    End Sub


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

        'Search for a specific device.

        'Call FindTheHid()

        '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.

        '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


    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

        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



    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

        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()


    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

            InputReportBuffer = Nothing

            'Define a delegate using the IAsyncResult object.

            Dim deleg As ReadInputReportDelegate = _
                DirectCast(ar.AsyncState, ReadInputReportDelegate)

            'Get the IAsyncResult object and the values of other paramaters that the
            'BeginInvoke method passed ByRef.

            deleg.EndInvoke(MyDeviceDetected, InputReportBuffer, Success, ar)

            'Display the received report data in the form's list box.

            If (ar.IsCompleted And Success) Then


                For Count = 1 To 7 '(MyHID.Capabilities.InputReportByteLength - 1)

                    InBuffer(Count) = InputReportBuffer(Count)

                Next Count

                MyMarshalToForm("AddItemToListBox", "An Input report has been read.")


                MyMarshalToForm("AddItemToListBox", " Input Report ID: " & InputReportBuffer(0))

                MyMarshalToForm("AddItemToListBox", " Input Report Data:")

                For Count = 1 To UBound(InputReportBuffer)

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

⌨️ 快捷键说明

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