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

📄 frmmain.vb

📁 visual basic 2005 express 写的上位机
💻 VB
📖 第 1 页 / 共 4 页
字号:
            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.")

                Debug.WriteLine(" Input Report ID: " & InputReportBuffer(0))
                Debug.WriteLine(" Input Report Data:")

                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.

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

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

                    Debug.WriteLine(" " & ByteValue)
                    MyMarshalToForm("AddItemToListBox", " " & ByteValue)

                    'Display the report data in the "Received Data" text box.

                    MyMarshalToForm("AddItemToTextBox", ByteValue)

                Next Count

            Else
                MyMarshalToForm("AddItemToListBox", "The attempt to read an Input report has failed.")
                Debug.Write("The attempt to read an Input report has failed")
            End If

            MyMarshalToForm("ScrollToBottomOfListBox", "")

            'Enable requesting another transfer.

            MyMarshalToForm("EnableCmdOnce", "")

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

    End Sub


    Private Sub GetVendorAndProductIDsFromTextBoxes _
        (ByRef myVendorID As Short, _
        ByRef myProductID As Short)

        'Purpose    : Retrieves a Vendor ID and Product ID in hexadecimal 
        '           : from the form's text boxes and converts the text to Shorts.

        'Accepts    : myVendorID - the Vendor ID as a Short.
        '           : myProductID - the Product ID as a Short.                    

        Try
            myVendorID = CShort(Val("&h" & txtVendorID.Text))
            myProductID = CShort(Val("&h" & txtProductID.Text))

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

    End Sub


    Private Sub InitializeDisplay()

        'Purpose    : Initialize the elements on the form.

        Try

            'Don't allow the user to select an input report buffer size until there is
            'a handle to a HID.

            cmdInputReportBufferSize.Enabled = False


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

    End Sub


    Private Sub MyMarshalToForm _
        (ByVal action As String, _
        ByVal textToDisplay As String)

        'Purpose    : Enables accessing a form's controls from another thread 

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

        Dim args() As Object = {action, textToDisplay}
        Dim MarshalToFormDelegate As MarshalToForm

        ' The AccessForm routine contains the code that accesses the form.

        MarshalToFormDelegate = New MarshalToForm(AddressOf AccessForm)

        ' Execute AccessForm, passing the parameters in args.

        MyBase.Invoke(MarshalToFormDelegate, args)

    End Sub


    Private Sub ReadAndWriteToDevice()

        'Purpose    : Initiates the exchanging of reports. 
        '           : The application sends a report and requests to read a report.

        'Report header for the debug display:
        Debug.WriteLine("")
        Debug.WriteLine("***** HID Test Report *****")
        Debug.WriteLine(Today & ": " & TimeOfDay)

        Try
            'If the device hasn't been detected, was removed, or timed out on a previous attempt
            'to access it, look for the device.
            If (MyDeviceDetected = False) Then

                MyDeviceDetected = FindTheHid()
            End If

            If (MyDeviceDetected = True) Then

                Call ExchangeInputAndOutputReports()

            End If


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

    End Sub

    Private Sub ScrollToBottomOfListBox()


    End Sub

    Private Sub SetInputReportBufferSize()

        'Purpose    : Set the number of Input buffers (the number of Input reports 
        '           : the host will store) from the value in the text box.

        Dim NumberOfInputBuffers As Integer

        Try
            'Get the number of buffers from the text box.

            NumberOfInputBuffers = CInt(Val(txtInputReportBufferSize.Text))

            'Set the number of buffers.

            MyHID.SetNumberOfInputBuffers _
                (HIDHandle, _
                NumberOfInputBuffers)

            'Verify and display the result.

            GetInputReportBufferSize()

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


    Private Sub Shutdown()

        'Purpose    : Perform actions that must execute when the program ends.

        Try
            'Close open handles to the device.

            If (HIDHandle <> 0) Then
                CloseHandle(HIDHandle)
                Debug.WriteLine(MyDebugging.ResultOfAPICall("CloseHandle (HIDHandle)"))
            End If

            If (ReadHandle <> 0) Then
                CloseHandle(ReadHandle)
                Debug.WriteLine(MyDebugging.ResultOfAPICall("CloseHandle (ReadHandle)"))
            End If

            If (WriteHandle <> 0) Then
                CloseHandle(WriteHandle)
                Debug.WriteLine(MyDebugging.ResultOfAPICall("CloseHandle (WriteHandle)"))
            End If

            'Stop receiving notifications.

            Call MyDeviceManagement.StopReceivingDeviceNotifications(DeviceNotificationHandle)

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

    End Sub


    Private Sub Startup()

        'Purpose    : Perform actions that must execute when the program starts.

        Try
            MyHID = New Hid()
            Call InitializeDisplay()
            tmrContinuousDataCollect.Enabled = False
            tmrContinuousDataCollect.Interval = 50

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

    End Sub

    Sub AddItemsToLstResults(ByRef ItemsToAdd As String)

    End Sub

    Private Sub tmrContinuousDataCollect_Tick _
        (ByVal eventSender As System.Object, _
        ByVal eventArgs As System.EventArgs) _
        Handles tmrContinuousDataCollect.Tick

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

        Try
            'Exchange data with the device.
            'The timer is enabled only if cmdContinous has been clicked, 
            'selecting continous (periodic) transfers.

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

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

        If InBuffer(1) = 0 Then
            SWStatus.BackColor = Color.Black
        Else
            SWStatus.BackColor = Color.RosyBrown
        End If
        If InBuffer(2) = 0 Then
            LEDStatus.BackColor = Color.Black
        Else
            LEDStatus.BackColor = Color.Green
        End If

        TextofTemperature.Text = ((InBuffer(6) * 256 + InBuffer(7)) >> 3) * 0.0625

    End Sub


    Private Sub txtProductID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtProductID.TextChanged

        'The Product ID has changed in the text box.

        Try
            Call DeviceHasChanged()

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


    Private Sub txtVendorID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVendorID.TextChanged

        'The Vendor ID has changed in the text box.

        Try
            Call DeviceHasChanged()

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

    End Sub


    Protected Overrides Sub Finalize()
        MyBase.Finalize()
    End Sub


    Protected Overrides Sub WndProc(ByRef m As Message)

        'Purpose    : Overrides WndProc to enable checking for and handling
        '           : WM_DEVICECHANGE(messages)

        'Accepts    : m - a Windows 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
            Call HandleException(Me.Name, ex)
        End Try

    End Sub


    Shared Sub HandleException(ByVal moduleName As String, ByVal e As Exception)

        'Purpose    : Provides a central mechanism for exception handling.
        '           : Displays a message box that describes the exception.

        'Accepts    : moduleName - the module where the exception occurred.
        '           : e - the exception

        Dim Message As String
        Dim Caption As String

        Try
            'Create an error message.

            Message = "Exception: " & e.Message & ControlChars.CrLf & _
            "Module: " & moduleName & ControlChars.CrLf & _
            "Method: " & e.TargetSite.Name

            'Specify a caption.

            Caption = "Unexpected Exception"

            'Display the message in a message box.

            MessageBox.Show(Message, Caption, MessageBoxButtons.OK)
            Debug.Write(Message)

        Finally
        End Try

    End Sub


    Private Sub tmrDelay_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrDelay.Tick

    End Sub

    Private Sub Button_LEDOnOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_LEDOnOff.Click

        If Button_LEDOnOff.Text = "LED_ON" Then

            LEDCommand = 1
            Button_LEDOnOff.Text = "LED_OFF"
        Else
            LEDCommand = 0
            Button_LEDOnOff.Text = "LED_ON"
        End If
    End Sub

    Private Sub lblProductID_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblProductID.Click

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextofTemperature.TextChanged


    End Sub

    Private Sub ToolTip1_Popup(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PopupEventArgs)

    End Sub
End Class

⌨️ 快捷键说明

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